File Content
Linux File Content Command
There are many commands which help to look at the contents
of a file
Commands Function
head It displays the beginning of a file.
tail It displays the last part of a file.
cat This command is versatile and multi worker.
tac Opposite of cat.
Command line displays contents in pager form that is
more
either in more format.
Command line displays contents in pager form that is
less
either in less format.
Head Command
The 'head' command displays the starting content of a file.
By default, it displays starting 10 lines of any file.
Syntax : head <filename>. Example head [Link]
The 'head -n' option displays specified number of lines.
Syntax head -n <filename>. Example head -20 [Link]
The 'head -c' command counts the number of bytes of a file.
Syntax head –c <number> <filename>. Example head –c 20
[Link]
20 byte content of file [Link] will be displayed from above
command
Head Command
If we use "head -c<number>k <file name>"then it will return the
result by multiplying the number by suffix. Suffix can be "b
(bytes=512), k(kilobytes=1024) and m (megabytes=1048576)“
head -c k filename
k stands for kilobytes (1 kilobyte = 1024 bytes)
Example: head -c 5k filename outputs the first 5120 bytes of the file
head -c m filename
m stands for megabytes (1 megabyte = 1024 × 1024 = 1,048,576 bytes)
Example: head -c 2m filename outputs the first 2,097,152 bytes of the file.
Tail Command
The 'tail' command displays the last lines of a file. By default, it
will also display the last ten lines of a file.
Syntax : tail <filename>. Example tail [Link]
'tail -n' option displays the specified number of [Link] : tail –
n <filename>
Example tail –n 2 [Link] or tail -2 [Link]
The 'tail -c' option displays the specified number of bytes from
the last.
Syntax: tail –c <number> <filename>
Example tail –c 10 [Link] will display last 10 bytes of the file
[Link]
Cat Command
It one of the most frequently used commands.
It can be used to display the content of a file, copy content
from one file to another, concatenate the contents of multiple
files, display the line number, display $ at the end of the line,
etc.
To display the content of the file syntax is cat <filename>
To display the content of multiple files at once, type file names
in one single line like "cat file1 file2 file3... fileN.
Option Function
cat > [fileName] To create a file.
To copy content from older to new
cat [oldfile] > [newfile]
file.
cat [file1 file2 and so on] > [new file To concatenate contents of
name] multiple files into one.
cat -n/cat -b [fileName] To display line numbers.
To display $ character at the end of
cat -e [fileName]
each line.
cat [fileName] <<EOF Used as page end marker.
The 'cat' command can be used to create a new file with greater than sign
(>).
Syntax cat > filename. Example cat > [Link]
Type the command "cat > [Link]" and press 'enter'. You will be directed to
the next line.
Press 'enter' after every line and you will be directed to the next line. To save
file, go to the next line, press 'ctrl+d' and file will be saved.
The 'cat' command with double greater than sign (>>) append (add
something in the last of a file) something in already existing file.
Example cat >> [Link]. This command helps to add line at the end of
[Link]
The 'cat' command can be used to copy the content of a file into another
file.
Syntax : cat old file > new file
The 'cat' command can be used to concatenate the contents of multiple
files in a single new file.
Syntax cat <filename1> <filename2>.... > <newFilename>
Example cat t1 t2 t3 > newt
A new line will be inserted while concatenating multiple files by using a
hyphen (-).
Syntax cat - <filename1> <filename2>. . . . > <new filename>
The 'cat -n' option displays line numbers in front of each line in a file.
Syntax cat -n <fileName>
The 'cat -b' option removes the empty lines.
The 'cat << EOF ' option displays an end marker at the end of a file.
The file can be saved with the help of 'ctrl + d ' keys also. It works like the
end marker.
Cat > t5 <<$
The 'tac' command is the reverse of the 'cat' command.
It is also known as 'cat' backward.
It will display the file content in reverse order.
It prints the last line first, then second last and so on. Such way, it
prints the first line at last.
Syntax tac <filename>
More Command
In case of larger files, 'cat' command output will scroll off your screen while
'more' command displays output one screenful at a time.
Following keys are used in 'more' command to scroll the page:
Enter key: To scroll down page line by line.
Space bar: To go to next page.
b key: To go to the backward page.
We force the more command to squeeze multiple blank lines into one by
using the -s command line option.
By default, more uses the complete screen to display output. However, we
can even customize this in terms of number of lines. This can be done by
explicitly specifying the number of lines you want more to use.
more -10 <filename>
Less command
The 'less' command is same as 'more' command but include some more
features.
Syntax less < filename>
A particular string is searched by typing forward slash (/) and then followed
by the string name, in the lower left corner of the terminal box.
The searched string will be automatically highlighted.
Search Navigation
search navigation keys will help you in forward and backward search.
Forward search
/ : search for a pattern for the next occurence
n : search for next match
N : search for previous match
Backward search
?: search for a pattern for the next occurrence
n : for next match in backward direction
N : for previous match in forward direction
Screen Navigation
Ctrl + f : forwards one window
Ctrl + d : forwards half window
Ctrl + b : backwards one window
Ctrl + u : backwards half window
Line Navigation
to move forward or backward line by line
j : forward by one line
k : backward by one line
G : used to go to end of the file
g : used to go to start of the file
q or ZZ : to exit
'cut' command is useful in selecting a specific column of a file. After (-d),
delimiter (from where you want to separate the columns) comes.
Delimiters can be a space (' '), a hyphen (-), a slash (/) or anything else.
After (-f), column number is mentioned.
Syntax: cut -d(delimiter) -f(columnNumber) <fileName>
Hyphen (-) As Delimiter
cut -d- -f(columnNumber) <fileName>
cut –d- -f1 [Link]
Space As Delimiter
If you want to use space as a delimiter then you have to quote the space
(' ‘).
Syntax : cut -d ' ' -f(columnNumber) <fileName>
Grep
The 'grep' command stands for "global regular expression print". grep
command filters the content of a file which makes our search easy.
The 'grep' command is generally used with pipe (|).
Syntax: command | grep <searchWord>
It can be used without pipe also.
Syntax: grep <searchWord> <file name>
The 'grep -v' command displays lines not matching to the specified word.
Syntax : grep -v <searchWord> <fileName>
The 'grep -i' command filters output in a case-insensitive way.
Syntax : grep -i <searchWord> <fileName>
The 'wc' command helps in counting the lines, words and characters in a
file.
Syntax: wc <fileName> (Counts lines, words, and characters)
wc -l <fileName> (Counts only lines)
wc -w <fileName> (Counts only words)
wc -c <fileName> (Counts only characters)
The 'sort' command sorts the file content in an alphabetical order.
Syntax: sort <fileName>
If a file has more than one column, column number is used to sort a
specific column
sort -k<columnNumber> <fileName>
Linux gzip
Gzip (GNU zip) is a compressing tool. By default original file will be
replaced by the compressed file ending with extension (.gz).
To decompress a file you can use gunzip command and original file will be
back.
Syntax: gzip <file1> <file2> <file3>. . . gunzip<file1> <file2> <file3>. . .
If you want to compress more than one file together, you can use 'cat' and
gzip command with pipe command.
Syntax:cat <file1> <file2>. . | gzip > <[Link]>
The gzip command will not be able to compress a directory because it
can only compress a single file. To compress a directory you have to use
'tar' command.
The tar command is used to archive (and optionally compress) multiple files
and directories into a single .tar,.[Link] or .tgz file.
Common tar Syntax : tar [options] [archive-name] [file/directory]
Hyphen (-) is not mandatory in 'tar' command.
'c' is to create,
'v' is for verbose, to display output,
'f’ to mention destination of your output file,
'z’ for specifying compress with gzip.
‘j’ Compress using bzip2
‘x’ for Extract files from the archive.
Create a .tar archive tar -cvf [Link] [Link] [Link]
Creates [Link] from the two files
Create a compressed .[Link] archive tar -czvf [Link] myfolder/
Compresses myfolder into [Link]
Example tar cvfz [Link] /path/to/directory-or-files
tar –czvf [Link] /root/test
We can use it to compress multiple directories, multiple individual files, or
both. Just provide a list of files or directories instead of a single one.
tar -czvf [Link] /home/ubuntu/Downloads /usr/local/stuff
/home/ubuntu/Documents/[Link]
Create a .tar archive tar -cvf [Link] [Link] [Link]
Creates [Link] from the two files
Create a compressed .[Link] archive tar -czvf [Link] myfolder/
Compresses myfolder into [Link]
Extract a .tar file
tar -xvf [Link]
Extract a .[Link] file
tar -xzvf [Link]
List contents of an archive
tar -tvf [Link]
This allows you to create bzip2-compressed files, often named .tar.bz2,
.[Link], or .tbz files.
To do so, just replace the -z for gzip in the commands with a -j for bzip2.
Example tar –cvfj [Link].bz2 test
To extract tar file we use replace –c with –x
tar –xvfz [Link]
If we want to extract the contents of the archive to a specific directory, we
can do so by appending the -C to the end of the command.
For example, the following command will extract the contents of the
[Link] file to the /tmp directory.
tar -xzvf [Link] -C /tmp
If the file is a bzip2-compressed file, replace the “z” in the above commands
with a “j”.