Manage Files Effectively using head, tail and cat Commands in Linux
There are several commands and programs provided byLinuxfor 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.
Manage Files in Linux
Today, in this article we will be discussing the most popular commands called head, tail and cat, most of us already aware of such commands, but very few of us implement it when needed.
1. head Command
The head command reads the first ten lines of a any given file name. The basic syntax of head command is:
head [options] [file(s)]
For example, the following command will display the first ten lines of the file named ‘/etc/passwd‘.
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.
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.
# head -n5 /var/log/yum.log
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.
# head -5 /var/log/yum.log
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.
# head -c45 /var/log/yum.log
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:
# tail [options] [filenames]
For example, the following command will print the last ten lines of a file called ‘access.log‘.
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:
# cat [options] [filenames] [-] [filenames]
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.
# cat 5
Hi Tecmint-Team
Keep connected
Share your thought
connect us tecmint.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.
# cat > tecmint.txt
Tecmint is the only website fully dedicated to Linux.
We can have custom end maker for ‘cat’ command. Here it is implemented.
# cat > test.txt << end
I am Avishek
Here i am writing this post
Hope your are enjoying
end
# cat test.txt
I am Avishek
Here i am writing this post
Hope your are enjoying
Never underestimate the power of ‘cat’ command and can be useful for copying files.
# cat avi.txt
I am a Programmer by birth and Admin by profession
# cat avi.txt > avi1.txt
# cat avi1.txt
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.
# cat month
January
February
March
April
May
June
July
August
September
October
November
December
# tac month
December
November
October
September
August
July
June
May
April
March
February
January
For more examples of cat command usage, refer to the 13 cat Command Usage
That’s all for now. I’ll be here again with another Interesting Article, worth Knowing. Till then stay tuned and connected to Tecmint. Don’t forget to provide us with your valuable feedback in our comment section.
No comments:
Post a Comment