Working with Linux file content
In this chapter we will look at the contents of  linux files with cat, tac , head, tail, , more and less.We will also get a glimpse of the possibilities of tools like cat on the command line.

 

Note: Please check video at bottom of article

head
You can use head to display the first ten lines of a file.

[ali@rhel71 ~]$ head /etc/passwd
 root:x:0:0:root:/root:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 adm:x:3:4:adm:/var/adm:/sbin/nologin
 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 sync:x:5:0:sync:/sbin:/bin/sync
 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
 halt:x:7:0:halt:/sbin:/sbin/halt
 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
 operator:x:11:0:operator:/root:/sbin/nologin

The head command can also display the first n lines of a file.

[ali@rhel71 ~]$ head -2 /etc/passwd
 root:x:0:0:root:/root:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 [ali@rhel71 ~]$

tail
The tail command will display the last ten lines of a file.

[ali@rhel71 ~]$ tail /etc/passwd
 qemu:x:107:107:qemu user:/:/sbin/nologin
 chrony:x:994:993::/var/lib/chrony:/sbin/nologin
 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
 gdm:x:42:42::/var/lib/gdm:/sbin/nologin
 gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
 tcpdump:x:72:72::/:/sbin/nologin
ali:x:1000:1000:ali khan:/home/ali:/bin/bash
[ali@rhel71 ~]$
 You can give tail the number of lines you want to see.
 [ali@rhel71 ~]$ tail -6 /etc/passwd
 gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
 tcpdump:x:72:72::/:/sbin/nologin
 ali:x:1000:1000:ali khan:/home/ali:/bin/bash
 [ali@rhel71 ~]$

cat
The cat (short for “concatenate“) command is one of the most universal tools. All it does is copy standard input to standard output. In combination with the shell this can be very powerful and diverse. Some examples will give a glimpse into the possibilities. The first example is simple, you can use cat to display a file on the screen. If the file is longer than the screen, it will scroll to the end.

[ali@rhel71 ~]$ cat /etc/resolv.conf
 # Generated by NetworkManager
 search example.com
 nameserver 192.168.0.1
 [ali@rhel71 ~]$

concatenate
cat is short for concatenate. In this example We will concatenate contents of three files to standard output (terminal screen)

[ali@rhel71 ~]$ echo this is tutorial > file1
 [ali@rhel71 ~]$ echo about linux > file2
 [ali@rhel71 ~]$ echo enjoy > file3
 [ali@rhel71 ~]$ cat file1 file2 file3
this is tutorial
 about linux
 enjoy
 [ali@rhel71 ~]$

Create files
You can use cat to create file. In below example we create file by cat > filename, once we are done with writing all content hit enter. Then press CTRL+d and you are done. To display content type cat filename

[ali@rhel71 ~]$ cat > health.txt
 Good health is blessing
 [ali@rhel71 ~]$ cat health.txt
 Good health is blessing
 [ali@rhel71 ~]$

The Ctrl d key combination will send an EOF (End of File) to the running process ending
the cat command.
tac
tac is reverse of cat. Tac will display lines in reverse order .Just one example will show you the purpose of tac (as the opposite of cat).

[ali@rhel71 ~]$ cat > tac.txt
 1
 2
 3
 4
 this is line number 5
 6
 this is last line number 7
 [ali@rhel71 ~]$ tac tac.txt
 this is last line number 7
 6
 this is line number 5
 4
 3
 2
 1
 [ali@rhel71 ~]$

custom end marker
Instead of pressing CTRL d .You can choose an end marker for cat with << as is shown below. This construction is called a here directive and will end the cat command.

[ali@rhel71 ~]$ cat > ending_of_file.txt <<end
 > this file shows how to end file using here directive with marker end
 > end
 [ali@rhel71 ~]$ cat ending_of_file.txt
this file shows how to end file using here directive with marker end
 [ali@rhel71 ~]$
copy files
cat can also be used to copy files.
[ali@rhel71 ~]$ cat > original.txt
 this text is in original file
 [ali@rhel71 ~]$ touch copy.txt
 [ali@rhel71 ~]$ cat copy.txt
 [ali@rhel71 ~]$ cat original.txt
 this text is in original file
 [ali@rhel71 ~]$ cat original.txt >copy.txt
 [ali@rhel71 ~]$ cat copy.txt
 this text is in original file
 [ali@rhel71 ~]$

more and less
If contents of file are taking space on display more than a page then you can use either more or less. More will allow you to see the contents of the file page by page. Use the space bar to see the next page, or q to quit.
And now you can watch video tutorial

Thanks for visiting my post. I hope you enjoyed the tutorial. See you with our next, new and exciting tutorial

SUBSCRIBE OUR NEWSLETTER
I agree to have my personal information transfered to MailChimp ( more information )
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.