headcommand is one of the filtering commands used to display the first 10 lines by default.
head <option> <filename>
By giving the below command, you can print the data within a specified range. To print the data within a specified range, we have to use both the
head command and the
tail command. In the
head command, we are mentioning the starting line number, and in the
tail command, we are mentioning the ending line number. As a result, the data is printed in the specified range.
$ head -n 20 f1 | tail -10
In the below command, we can display the two most recently used files or folders. The
ls command provides the recently used files/folders. The output is then given to the
head command, which filters and prints the two recent files/folders as an output.
//To Display recently used files. ls -t //Cut three most recently used files. ls -t | head -n 2
head command can be piped with one or more filters for additional processing. In the below command, we can sort the three most recently used files or folders.
$ ls -t | head -n 3 | sort
tail command displays the last 10 lines of data in the specified file by default.
tail <option> <filename>
By giving the below command the last 3 lines of the data in the f3 file are displayed.
tail -n 3 f3
By giving the below command the data from the second line of the file f3 are displayed.
tail -n +2 f3
By giving the below command without using the -q option the data along with the filename are displayed.
tail f3 f2
By giving the below command along with the -q option, only the data of both the files are displayed.
tail -q f3 f2
By giving the below command with option -f, you can modify the data and you have to press ctrl+c to abort the editing wizard.
tail -f f3
By giving the below command with option -v, data of the given file along with the filename are displayed.
tail -v f3
To display the last two data of the file the below command is given,
tail -n 2 f3
To display the last two data of the file that should be sorted in reverse order, give the below command.
tail -n 2 f3 | sort -r
By giving the below command,
catcommand provides all the data present in the f3 file and sends the output to the head command
headcommand filters data from the 1st line to the 2nd line and sends the output to the tail command.
tailcommand provides the last 3 lines of the data.
cat f3 | head -n 2 | tail -n 3> f2 //To display the content in the f2 file cat f2
By using the below command you can know the version of the tail command currently you have installed in your operating system.
The collective output of all the
tail command examples: