Working with Multiple Files
The head
command can also be used to view the beginning portions of multiple files at once. This is particularly useful when you need to quickly compare the headers or initial content of several data files.
- Let's create a second data file in the project directory:
echo -e "Time,Energy,Temperature\n0,100,25.5\n1,95,25.7\n2,90,26.0\n3,85,26.2\n4,80,26.5\n5,75,26.8\n6,70,27.0\n7,65,27.3\n8,60,27.5\n9,55,27.8" > ~/project/temperature_data.txt
- Now, let's create a third file with different content:
echo -e "ID,Name,Score\n1,Alice,95\n2,Bob,87\n3,Charlie,92\n4,David,78\n5,Eve,89" > ~/project/score_data.txt
- To view the first 2 lines of both files at once, run:
head -n 2 ~/project/quantum_data.txt ~/project/temperature_data.txt
This will produce output with headers indicating each file:
==> /home/labex/project/quantum_data.txt <==
Qubit1,Qubit2,Probability
00,01,0.25
==> /home/labex/project/temperature_data.txt <==
Time,Energy,Temperature
0,100,25.5
- You can also view the heads of all text files in the current directory using wildcards:
head -n 1 ~/project/*.txt
This will display the first line (usually the header) of each text file in the project directory:
==> /home/labex/project/quantum_data.txt <==
Qubit1,Qubit2,Probability
==> /home/labex/project/score_data.txt <==
ID,Name,Score
==> /home/labex/project/temperature_data.txt <==
Time,Energy,Temperature
The ability to examine multiple files simultaneously makes the head
command an efficient tool for managing and comparing datasets.