Viewing and Reading File Contents
Learn to read files right in the terminal using cat, less, head and tail, plus handy tools like wc, file and diff.
Why read files in the terminal?
Configuration files, logs and scripts are all plain text. On a server you often have no graphical editor, so you need commands that show the contents of a file directly in the terminal.
Make a practice file
First create a small file to experiment with:
printf "apple\nbanana\ncherry\ndate\nelderberry\n" > fruits.txt
printf prints text, and \n means "start a new line". The > symbol sends the output into a file instead of showing it on the screen. This is called redirection and has its own lesson later. After this command, fruits.txt contains five lines, one fruit on each.
cat: print the whole file
cat fruits.txt
cat -n fruits.txt
cat a.txt b.txt
cat prints the entire file. The name comes from "concatenate" because it can join several files one after another. -n adds line numbers. cat is ideal for short files. For long ones the text rushes past before you can read it.
less: read page by page
less /var/log/syslog
less opens a file in a viewer where you can move around. It does not load the whole file at once, so it handles very large logs easily. Keys inside less:
Space: next page.b: previous page.- Arrow keys: one line at a time.
/word: search forward for "word". Then pressnfor the next match.G: jump to the end.g: jump to the start.q: quit.
head and tail: the start and end
head fruits.txt
head -n 3 fruits.txt
tail fruits.txt
tail -n 2 fruits.txt
head prints the first 10 lines and tail prints the last 10. The -n option sets how many lines you want. These are useful for logs, where the newest entries are at the end.
tail -f: follow a file
tail -f /var/log/syslog
-f means follow. tail prints the last lines, then keeps running and prints any new line the moment it is added to the file. This is how you watch a log live. Press Ctrl + C to stop.
wc: count lines, words and bytes
wc fruits.txt
wc -l fruits.txt
wc ("word count") prints the number of lines, words and bytes. -l shows only the line count, which answers "how many lines are in this file?".
file: check what a file really is
On Linux a file extension is only a hint in the name. The file command looks at the content and tells you what the file really is.
file fruits.txt
file /bin/ls
file photo.jpg
Typical answers are ASCII text, ELF executable and JPEG image data.
diff: compare two files
diff old.txt new.txt
diff -u old.txt new.txt
diff shows the lines that differ. A line starting with < comes from the first file and a line starting with > comes from the second. If nothing is printed, the files are identical. -u prints the "unified" format, which shows a few lines of context and is easier to read.
Choosing the right tool
- Small file, quick look:
cat - Big file you want to scroll or search:
less - Only the beginning or the end:
headortail - Watch a log as it grows:
tail -f - How many lines:
wc -l - What kind of file is it:
file - How do two files differ:
diff
cat a binary file such as a program or an image. It prints unreadable characters and can leave your terminal showing garbage. If that happens, type reset and press Enter.