There are several commands
and programs provided by Linux for viewing
the contents of file. Working with files is one of the daunting task, most of
the computer users be it newbie, regular user, advanced user, developer, admin,
etc performs. Working with files effectively and efficiently is an art.
1. head Command
The head command reads the
first ten lines of a any given file name. The basic syntax of head command is:
For
example, the following command will display the first ten lines of the file
named ‘/etc/passwd‘.
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
If more than one file is given,
head will show the first ten lines of each file separately. For example, the
following command will show ten lines of each file.
==> /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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin ==> /etc/shadow <==
root:$6$85e1:15740:0:99999:7:::
bin:*:15513:0:99999:7:::
daemon:*:15513:0:99999:7:::
adm:*:15513:0:99999:7:::
lp:*:15513:0:99999:7:::
sync:*:15513:0:99999:7:::
shutdown:*:15513:0:99999:7:::
halt:*:15513:0:99999:7:::
mail:*:15513:0:99999:7:::
uucp:*:15513:0:99999:7:::
If
it is desired to retrieve more number of lines than the default ten, then ‘-n‘ option is used along with an integer
telling the number of lines to be retrieved. For example, the following command
will display first 5 lines from the file ‘/var/log/yum.log‘ file.
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
In
fact, there is no need to use ‘-n‘ option. Just the hyphen and
specify the integer without spaces to get the same result as the above command.
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
The
head command can also display any desired number of bytes using ‘-c‘ option followed by the number of bytes to
be displayed. For example, the following command will display the first 45 bytes of given file.
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el
2. tail Command
The
tail command allows you to display last ten lines of any text file. Similar to
the head command above, tail command also support options ‘n‘ number of lines and ‘n‘ number of characters.
The basic syntax of tail
command is:
For
example, the following command will print the last ten lines of a file called ‘access.log‘.
1390288226.042 0 172.16.18.71 TCP_DENIED/407 1771 GET http://download.newnext.me/spark.bin? - NONE/- text/html
1390288226.198 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.210 1182 172.16.20.44 TCP_MISS/200 70872 GET http://mahavat.gov.in/Mahavat/index.jsp pg DIRECT/61.16.223.197 text/html
1390288226.284 70 172.16.20.44 TCP_MISS/304 269 GET http://mahavat.gov.in/Mahavat/i/i-19.gif pg DIRECT/61.16.223.197 -
1390288226.362 570 172.16.176.139 TCP_MISS/200 694 GET http://p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com/v6exp3/redir.html pg
1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
If more than one file is
provided, tail will print the last ten lines of each file as shown below.
==> access.log <== 1390288226.042 0 172.16.18.71 TCP_DENIED/407 1771 GET http://download.newnext.me/spark.bin? - NONE/- text/html 1390288226.198 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.210 1182 172.16.20.44 TCP_MISS/200 70872 GET http://mahavat.gov.in/Mahavat/index.jsp pg DIRECT/61.16.223.197 text/html 1390288226.284 70 172.16.20.44 TCP_MISS/304 269 GET http://mahavat.gov.in/Mahavat/i/i-19.gif pg DIRECT/61.16.223.197 - 1390288226.362 570 172.16.176.139 TCP_MISS/200 694 GET http://p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com/v6exp3/redir.html pg 1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html 1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html 1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html ==> error_log <==
[Sun Mar 30 03:16:03 2014] [notice] Digest: generating secret for digest authentication ...
[Sun Mar 30 03:16:03 2014] [notice] Digest: done
[Sun Mar 30 03:16:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips configured -- resuming normal operations
Similarly,
you can also print the last few lines using the ‘-n‘ option as shown below.
1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
You
can also print the number of characters using ‘-c’ argument
as shown below.
ymantec.com:443 - NONE/- text/html
3. cat Command
The
‘cat‘ command is most widely used,
universal tool. It copies standard input to standard output. The command
supports scrolling, if text file doesn’t fit the current screen.
The basic syntax of cat command
is:
The most frequent use of cat is
to read the contents of files. All that is required to open a file for reading
is to type cat followed by a space and the file name.
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
…
The cat command also used to
concatenate number of files together.
# echo 'Keep connected' > 2
# echo 'Share your thought' > 3
# echo 'connect us manish.com@gmail.com' > 4
Hi Manish-Team
Keep connected
Share your thought
connect us manish.com@gmail.com
It can be also used to create
files as well. It is achieved by executing cat followed by the output
redirection operator and the file name to be created.
Manish is the only website fully dedicated to Linux.
We can have custom end maker
for ‘cat’ command. Here it is implemented.
I am Manish
Here i am writing this post
Hope your are enjoying
end
I am Manish
Here i am writing this post
Hope your are enjoying
Never underestimate the power
of ‘cat’ command and can be useful for copying files.
I am a Programmer by birth and Admin by profession
I am a Programmer by birth and Admin by profession
Now
what’s the opposite of cat? Yeah it’s ‘tac‘. ‘tac‘ is a command under Linux. It
is better to show an example of ‘tac’ than to talk anything about it.
Create a text file with the
names of all the month, such that one word appears on a line.
January
February
March
April
May
June
July
August
September
October
November
December
December
November
October
September
August
July
June
May
April
March
February
January
No comments:
Post a Comment