Saltar a contenido

04 - Reading Files

What this session is

About 30 minutes. You'll learn to read text files in the terminal - without opening an editor.

cat: show a whole file

cat ~/.bashrc

cat (short for "concatenate") prints a file's contents to your terminal. For small files, perfect. For large ones, it scrolls off the top - useful tools for that below.

You can cat multiple files at once:

cat file1.txt file2.txt          # both, in order

For files with binary content (images, executables), cat produces gibberish. Stick to text.

less: page through a file

For files too big to view all at once:

less /etc/services

Inside less:

  • Space - next page.
  • b - previous page.
  • Arrow keys - line by line.
  • /pattern - search forward.
  • ?pattern - search backward.
  • n / N - next/previous match.
  • g - top.
  • G - bottom.
  • q - quit.

less is the file viewer in Linux. Use it for anything over ~50 lines.

(Memory note: there's also more, which is older and inferior. Use less.)

head and tail: first/last N lines

head file.txt            # first 10 lines
head -n 5 file.txt       # first 5 lines

tail file.txt            # last 10 lines
tail -n 20 file.txt      # last 20 lines
tail -f file.log         # follow - print new lines as they appear (great for log files)

tail -f is one of the most-used commands for watching log files in real time. Ctrl-C to stop.

wc: count lines/words/bytes

wc file.txt          # lines, words, bytes
wc -l file.txt       # just lines
wc -w file.txt       # just words
wc -c file.txt       # just bytes

Useful in combinations: "how many lines in this log?" wc -l app.log. We'll see it piped with other commands in page 07.

file: what type is this?

file mystery.bin

Tells you what kind of file it is - ASCII text, PNG image, ELF 64-bit LSB executable, etc. Useful when you've downloaded something and aren't sure what you got.

xxd / hexdump: see bytes

For looking at binary files:

xxd file.bin | head

Shows hex + ASCII side by side. Rarely needed at this level; mentioned for recognition.

A note on encoding

Most text files are UTF-8. Occasionally you'll meet files with different encodings (Windows-1252, etc.) that look corrupted in your terminal. Use iconv to convert, or set LANG / LC_ALL for your shell session. Beyond beginner scope.

Exercise

  1. Open a long config file with less:

    less /etc/services
    
    Scroll, search (type /tcp and press Enter), navigate with arrows, quit with q.

  2. Show the last 20 lines of your bash history:

    tail -n 20 ~/.bash_history       # may not exist if you've never used bash
    

  3. Count the lines in /etc/services:

    wc -l /etc/services
    

  4. What kind of file is /usr/bin/ls?

    file /usr/bin/ls
    

  5. Show the first 5 lines of /etc/passwd:

    head -n 5 /etc/passwd
    
    What's in that file? (Each line is a user account.)

  6. Bonus: if you have a system log accessible (/var/log/syslog on Debian/Ubuntu, /var/log/system.log on macOS), follow it with tail -f. Watch it for a minute (sometimes you'll see new log lines appear). Ctrl-C to stop.

What you might wonder

"Why use less instead of an editor?" - Faster - opens instantly for huge files. - Doesn't accidentally modify anything. - Available on every Linux/Unix system.

You'll meet less constantly in real work - man (page 05) uses it; git log uses it; many tools pipe through it.

"What's the difference between tail -f and just tail?" Plain tail shows the last N lines and exits. tail -f keeps the terminal open and shows new lines as they're added. Use -f for live monitoring.

"cat file | less vs less file - which?" less file directly. The piped form works but creates a useless extra process. However, command | less is common when paging through any command's output that's too long.

Done

  • Show files with cat (small) or less (any size).
  • Navigate less (Space, /, q).
  • Grab first/last lines with head and tail.
  • Follow logs with tail -f.
  • Count with wc.
  • Identify file types with file.

Next: Searching →

Comments