Reading text files
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$ 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
$ ./start.sh >> logfile.log
$ ./start.sh | tee logfile.log
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
$ ./start.sh > logfile-$(date +%Y%m%d@%H%M%S).log
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
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
on 10/11/2009 at 12:28