Skip to content

Commit

Permalink
Add content for finding things section
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpiper committed Jul 30, 2020
1 parent e3de42a commit c7e44ed
Showing 1 changed file with 117 additions and 6 deletions.
123 changes: 117 additions & 6 deletions lessons/shell/finding-things.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,121 @@

# Finding things

The `grep` command is a powerful tool for matching patterns in files.
Let's use it to look for unicorns in our files:
```
$ grep -r unicorn .
./data-shell/creatures/unicorn.dat:1:COMMON NAME: unicorn
Binary file ./data-shell.zip matches
```
The `r` option performs a recursive search,
starting at the current directory,
specified by the dot `.`.
Note that `grep` searched both text and binary files for the matching pattern.
To omit binary files from the search,
use the `I` option:
```
$ grep -rI unicorn .
./data-shell/creatures/unicorn.dat:1:COMMON NAME: unicorn
```

To search only within the current directory,
the syntax of `grep` is slightly different.
Let's find all instances of the word "octane" in the files
in the current directory:
```
$ grep octane *
grep: data-shell: Is a directory
Binary file data-shell.zip matches
molecule_pdb_lengths:4: 30 octane.txt
molecule_pdb_lengths.bak:4: 30 octane.pdb
```
Note that I used the wildcard to denote all files in the current directory.

The `find` command recursively locates all files that match a search pattern.
Let's find all the files with the extension **.txt**:
```
$ find . -iname "*.txt"
./data-shell/data/amino-acids.txt
./data-shell/data/animal-counts/animals.txt
./data-shell/data/animals.txt
./data-shell/data/morse.txt
./data-shell/data/planets.txt
...
```
The search started at the current directory, `.`
The `iname` option directs `find` to ignore case in the search.

In another example,
let's use `find` to recursively locate all the directories
under the current directory:
```
$ find . -type d
.
./data-shell
./data-shell/creatures
./data-shell/data
./data-shell/data/animal-counts
...
```

## Command documentation

In the examples above (and in previous sections),
options for commands are frequently demonstrated.
To view a full list of options for any command,
use the `man` command to pull up its manual page.
For example, to get information about `find`, type:
```
$ man find
```
The `man` command displays documentation for `find`,
incuding all options and several examples,
in a pager.
The man page for `find` is quite long.
Press `q` in the pager to quit and return to the terminal.


## Command history

Every command you enter is logged.
To see the record of commands,
use `history`:
```
$ history
```
The entire command history is dumped to the terminal.
To view only the last five commands (for example),
pipe the output of `history` through `tail`:
```
$ history | tail -5
631 find . -type d
632 man find
633 man man
634 man find
635 history
```

How many times have you referenced `find`?
```
$ history | grep find | wc -l
32
```

The exclamation point, or bang, `!` can be used to match a command in history.
For example, to execute the last call to `man`, type:
```
$ !man
man find
```

To search history beyond the last instance of a command,
use the `Ctrl-r` keybinding.
At the prompt,
type `Ctrl-r`, then a search term.
Keep hitting `Ctrl-r` to move back through history.



## Summary

Expand All @@ -12,14 +127,10 @@ and special characters described in this section.
| ---------------- | -----------
| grep | match text in files
| find | match filenames
| man | show documentation for command
| history | record of commands
| ! | repeat command matched from history
| Ctrl-r | reverse search through history
| ! | repeat command from history
| man | show documentation for command
| env | show environment variables
| echo | print variables to terminal
| which | show location of command
| alias | make a shortcut for a command

___

Expand Down

0 comments on commit c7e44ed

Please sign in to comment.