Six-module walkthrough covering navigation, files, reading/searching, processes/editors, scripting, and advanced tools (ssh, regex, tar, etc.). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
4 KiB
CLI Part III: Reading and Searching
CHEG 667-013 — Chemical Engineering with Computers
Department of Chemical and Biomolecular Engineering, University of Delaware
Key idea
Read, search, and filter files using command line tools, and combine them with pipes and redirects.
Key goals
- Page through files with
cat,more, andless - Search for patterns in files with
grep - Connect commands together using pipes (
|) and redirects (>,>>,<)
1. Reading files
We've already used the cat command to print a file. Several other programs are useful for scanning through files. These include:
cat— print a filehead— show the first lines of a file (default: 10)tail— show the last lines of a file (default: 10)more— page through a file one screen at a timeless— "Opposite of more." (macOSmoreactually runsless.)wc— word count — count lines, words, and characters in a filegrep— file pattern searcher
head and tail are handy for quickly peeking at a file without printing the whole thing:
$ head -5 lsR_filesystem.txt # first 5 lines
$ tail -20 lsR_filesystem.txt # last 20 lines
wc tells you how big a file is. With the -l option, it counts just the lines:
$ wc lsR_filesystem.txt
14328 112849 893752 lsR_filesystem.txt
$ wc -l lsR_filesystem.txt
14328 lsR_filesystem.txt
The three numbers are lines, words, and characters (bytes).
Exercise 1: Make a directory listing of the root file system with
ls -lR / > lsR_filesystem.txt. Useheadto see the first 10 lines. Usetailto see the last 10 lines. Then page through the file usingless. Move up and down. Can you find a particular file using a search?
2. Searching with grep
Of the programs above, grep is a little different. It is used to find files with matching strings. Its basic use looks something like this:
$ grep 'string' filename
which will look for string in the file filename.
More help with less
Incidentally, in addition to the man pages, many commands have built-in help. Try running less --help. This also shows us that some options are preceded by a long dash. You might also try less -?.
3. Pipes and redirects
Pipes and redirects enable you to control the flow of information to and from processes.
|— a pipe>— redirect output to a file (this will always overwrite the file)>>— redirect and append output to a file<— use a file as input
One of the design philosophies of Unix centers around providing small, focused program tools that can be used together. Pipes send the output of one program to the input of another. Try it! Type:
$ ps aux | less
The ps aux command will generate a long list of processes that scroll past quickly. By "piping" the command to less, it is easier to page through the output.
In Exercise 1, we redirected the output of ls to a file.
Exercise 2: Use
grepto searchlsR_filesystem.txtfor a program you have used (e.g.,grep ls lsR_filesystem.txt). Try searching for other programs.
Exercise 3: Combine commands with pipes. For example, try
history | grep cdto find all thecdcommands you've used. Tryls /usr/bin | lessto page through the programs in/usr/bin.
4. Running commands as the administrator with sudo
Some commands require administrator (or superuser) privileges — things like installing software, modifying system files, or changing other users' settings. The sudo command ("superuser do") lets you run a single command with these elevated privileges:
$ sudo apt update
[sudo] password for ef1j:
You'll be prompted for your password (not the root password). If your account has been granted sudo access, the command will run as the administrator.
Use sudo only when you need to. Running everyday commands with sudo is unnecessary and can accidentally modify system files. A common pattern you'll see is using sudo for package management:
$ sudo apt update && sudo apt upgrade