File Commands in Linux
Displaying the Content of Files
You use the cat (concatenate) command to display the contents of a specified file. The
cat command can vertically concatenate the contents of more than one file.
Example
[Steveelocalhost Steve]$ cat atal
A sample file
(Steveelocalhost Steve]$
When you type the above command, it is assumed that the file datal is in the current
directory. However, you can also specify the complete path to display a file in another
directory.
Youcan also use the cat command to display the contents of more than one file, as shown in
the following command:
Example
[SteveGloca l host steve) $ cat datal data2
A sample file
Another sample file
(Steve@localhost Steve]$
, The head and tail Commands
Vau use the head commandto display the specifiednumber of lincs from the
beginning of a
6le Youuse the tail command to display the specified number of lines from the end of a file.
Ifvou do not specity aparametcr, 10 lines are displayed by default. The syntax for both the
commands is:
Syntax
head [options] [file name]
tail [options] [file name]
Youuse the head or tailcommand to display a certain number of lines on the screen.
Erample
$ head -3 /etc/passwd
root:x:0:0:root:/root :/bin/bash
bin:x:l:l:bin:/bin:
daemon:x:2:2:daemon:/sbin:
This command displays the first three lines from the beginning of the file /etckpasswd.
Some of the options available with the head command are given below:
Table 3.4: Options of the head Command
Option Functlon
C,-bytes-SIZE Displays the first<size> bytes.
of the first 10lines.
1,ines-NUMBER Displays the first <number> of lines instead
4, verbose Displays headers with file names.
Example
$ l8 -1 | tail 2
Steve 38 Oct 19 22: 28 y
-W-W-r 1 Steve
y.bak
-IW-rW-r- 1 Steve Steve 48 Oct 12 05:09
ls -l command.
This command displays the last two files from the output
output of
of the
Displaying the Content of Files
You use the cat (concatenate) command to display the contents of a specified file. The
cat command can vertically concatenate the contents of more than one file.
Example
[Steveelocalhost Steve]$ cat atal
A sample file
(Steveelocalhost Steve]$
When you type the above command, it is assumed that the file datal is in the current
directory. However, you can also specify the complete path to display a file in another
directory.
Youcan also use the cat command to display the contents of more than one file, as shown in
the following command:
Example
[SteveGloca l host steve) $ cat datal data2
A sample file
Another sample file
(Steve@localhost Steve]$
, The head and tail Commands
Vau use the head commandto display the specifiednumber of lincs from the
beginning of a
6le Youuse the tail command to display the specified number of lines from the end of a file.
Ifvou do not specity aparametcr, 10 lines are displayed by default. The syntax for both the
commands is:
Syntax
head [options] [file name]
tail [options] [file name]
Youuse the head or tailcommand to display a certain number of lines on the screen.
Erample
$ head -3 /etc/passwd
root:x:0:0:root:/root :/bin/bash
bin:x:l:l:bin:/bin:
daemon:x:2:2:daemon:/sbin:
This command displays the first three lines from the beginning of the file /etckpasswd.
Some of the options available with the head command are given below:
Table 3.4: Options of the head Command
Option Functlon
C,-bytes-SIZE Displays the first<size> bytes.
of the first 10lines.
1,ines-NUMBER Displays the first <number> of lines instead
4, verbose Displays headers with file names.
Example
$ l8 -1 | tail 2
Steve 38 Oct 19 22: 28 y
-W-W-r 1 Steve
y.bak
-IW-rW-r- 1 Steve Steve 48 Oct 12 05:09
ls -l command.
This command displays the last two files from the output
output of
of the