CoursesLinux essentialsThe shell & filesystem

Working with files & text

view, find, grep, pipes, redirection.

Beginner14 min · lesson 2 of 25

Most Linux work is reading and manipulating text — config files, logs, output. A handful of commands cover almost everything: cat dumps a whole file, less pages through a big one, head and tail show the start or end, and tail -f follows a file live as it grows (invaluable for watching logs). Learn these five and you can inspect anything on a server without a graphical editor.

terminal
$ cat /etc/hostname # print a short file
$ less /var/log/syslog # page through a big one (q to quit, / to search)
$ tail -n 50 /var/log/auth.log # last 50 lines
$ tail -f /var/log/nginx/access.log # follow live as new lines arrive

find and grep: locate anything

Two search tools do enormous work. find walks the filesystem looking for files by name, type, time, or permission. grep searches inside files for text (a pattern), and is how you locate the one relevant line among thousands. Together they answer "where is this config?" and "which file mentions this error?" — questions you will ask constantly. grep -r searches a whole directory tree recursively.

terminal
$ find /etc -name "*.conf" -type f # all .conf files under /etc
$ find / -perm -4000 -type f 2>/dev/null # all SUID binaries (a security check!)
$ grep -rn "listen" /etc/nginx/ # every "listen" line, with line numbers
$ grep -i error /var/log/syslog | tail # case-insensitive, last matches

Pipes and redirection: connect commands

The real power of the shell is composition. A pipe (|) sends one command’s output into the next, so you build a chain: list, filter, count. Redirection sends output to a file instead of the screen: > overwrites, >> appends, and 2> captures errors. This is why Linux tools are small and single-purpose — they are meant to be combined, and a three-command pipeline often replaces a whole script.

terminal
# who is connected, on which ports, sorted and counted — in one line
$ ss -tn | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn
$ dmesg > boot.txt # save output to a file (overwrite)
$ echo "note" >> log.txt # append a line
$ some-command 2> errors.txt # send only errors to a file
> overwrites without warning
A single > truncates the target file to empty before writing — command > important.conf destroys important.conf if the command produces nothing. Use >> to append when you mean to add, double-check the target of any >, and never redirect into a file you are also reading from in the same pipeline. This is one of the most common ways people lose a config file.