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>
109 lines
4 KiB
Markdown
109 lines
4 KiB
Markdown
# 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`, and `less`
|
|
- 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 file
|
|
- `head` — 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 time
|
|
- `less` — "Opposite of more." (macOS `more` actually runs `less`.)
|
|
- `wc` — word count — count lines, words, and characters in a file
|
|
- `grep` — 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`. Use `head` to see the first 10 lines. Use `tail` to see the last 10 lines. Then page through the file using `less`. 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 `grep` to search `lsR_filesystem.txt` for 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 cd` to find all the `cd` commands you've used. Try `ls /usr/bin | less` to 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
|
|
```
|