Linux/Unix – Listing Text Files.

The most basic method to display the contents of a text file is with the cat command. The cat command concatenates (in other words, outputs as a string of characters) the contents of a text file to your display (by default). You can then use different shell metacharacters to direct the contents of that file in different ways. For example:

$ cat myfile.txt Send entire file to the screen

$ cat myfile.txt > copy.txt Direct file contents to another file

$ cat myfile.txt >> myotherfile.txt Append file contents to another file

$ cat -s myfile.txt Display consecutive blank lines as one

$ cat -n myfile.txt Show line numbers with output

$ cat -b myfile.txt Show line numbers only on non-blank lines

 

However, if your block of text is more than a few lines long, using cat by itself becomes impractical. That’s when you need better tools to look at the beginning or the end, or page through the entire text. To view the top of a file, use head:

$ head myfile.txt

$ cat myfile.txt | head

 

Both of these command lines use the head command to output the top 10 lines of the file. You can specify the line count as a parameter to display any number of lines from the beginning of a file. For example:

$ head -n 50 myfile.txt Show the first 50 lines of a file

$ ps auwx | head -n 15 Show the first 15 lines of ps output

 

This can also be done using this obsolete (but shorter) syntax:

$ head -50 myfile.txt

$ ps auwx | head -15

 

You can use the tail command in a similar way to view the end of a file:

$ tail -n 15 myfile.txt Display the last 15 lines in a file

$ tail -15 myfile.txt Display the last 15 lines in a file

$ ps auwx | tail -n 15 Display the last 15 lines of ps output

 

The tail command can also be used to continuously watch the end of a file as the file is written to by another program. This is very useful for reading live log files when troubleshooting apache, sendmail, or many other system services:

# tail -f /var/log/messages Watch system messages live

# tail -f /var/log/maillog Watch mail server messages live

# tail -f /var/log/httpd/access_log Watch web server messages live

Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: