Reading text files

Contents:

UNIX (and by extension, Linux) is a text-based operating system. This creates many possibilities when it comes to interacting with the system. This page deals with different ways to read text files. This may include logs or configuration files.

For examples for how to write to text files, see the awk and sed pages.

Reading

To read the top (head) of a file, use "head". To read the bottom (tail) of a file, use "tail". Use the -n parameter to specify the number of lines to read.

You can 'follow' a file as it is being written to with "tail -f". This is very useful for tracking logs in real-time. For example:

$ tail -f catalina.out

Redirecting output

If an application is logging to the terminal, you can write that to a file by using a redirection arrow. For example:

$ ./start.sh > logfile.log

You can use a double redirection arrow to append to an existing file instead of overwriting it:

$ ./start.sh >> logfile.log

If you want the output to go to both the terminal (standard out, or stdout) and a file, use "tee":

$ ./start.sh | tee logfile.log

To append to the chosen file instead of overwriting it, use the -a flag.

Name a log file according to the date and time

You can call the "date" command inside another command to give it the date and time.

This example will output to a new file with a name like "logfile-20091103@0947.log":

$ ./start.sh > logfile-$(date +%Y%m%d@%H%M).log

You may also want to include the seconds:

$ ./start.sh > logfile-$(date +%Y%m%d@%H%M%S).log

The embedded command is launched in a subshell. For more information on the "date" syntax, type "man date" at a terminal.

It is best to name files with the date going from the largest unit (years) to the smallest (minutes/seconds). This allows an "ls" command to automatically sort the files by date (since it displays in alphanumerical order).

Writing errors to a log file

With a redirection arrow, you can direct stdout messages to another location (such as a log file). Sometimes when you do this you will still see output on the console. These are being written to a different standard stream, standard error (stderr).

To redirect stderr into the same file as stdout, append "2>&1" to your command:

$ ./start.sh > logfile-$(date +%Y%m%d@%H%M).log 2>&1

This can be useful to collect more detailed logs.

Suppressing output

Sometimes you don't want to see any output at all, for example for an automated process. To achieve this, redirect your output to the special file /dev/null.

Resources

last modified by sd on 10/11/2009 at 12:28

Creator: sd on 2009/10/19 16:17
XWiki Enterprise 1.7.2.16857 - Documentation