Copy, move, and link
cp makes a copy; a directory needs -r or -R. cp -a copies recursively while keeping permissions, owners, and timestamps, which suits backups. mv both moves and renames. If the destination exists, behavior depends on whether it is a file or directory, so inspect it before overwriting. -i asks before overwriting and -n never overwrites.
ln -s /opt/app/current app creates a symbolic link, a pointer to another path. Deleting the link leaves the target intact; deleting the target leaves a broken link.
flowchart LR F["find /var/log<br/>-name '*.log'"] --> X["filenames"] X --> G["grep -i error<br/>search content"] G --> P["sort, uniq -c<br/>pipe and count"] P --> S["sed 's/a/b/'<br/>transform text"] S --> O["output<br/>or file"] %% step F: find searches by name, size or time and prints matching paths. %% step X: The result is a list of filenames, not their content. %% step G: grep searches inside files for a pattern. %% step P: Pipes pass one command's output to the next: sort, uniq -c, wc -l, head. %% step S: sed edits text in a stream; add -i only after the preview looks right. %% step O: Print to the screen or redirect to a new file.
Search for the right thing
| Goal | Command |
|---|---|
| File name or attributes | find . -name '*.log' |
| Lines containing text | grep -i 'error' app.log |
| Replace text on stdout | sed 's/foo/bar/g' in.txt |
| Compare two files | diff a.txt b.txt |
| Identify file data | file artifact |
find can filter by more than names: -type f or -type d, -size +100M, and -mtime -1 (modified within the last day). find walks the current filesystem; locate consults a database that may be stale. Quote a glob so find receives the pattern instead of the shell expanding it early.
grep has a few flags worth memorising: -r searches a directory tree, -n prints line numbers, -v inverts the match, -c counts matches, and -E enables extended regular expressions such as 'error|fail'.
Pipes and small text tools
The pipe | sends one command's output into the next, so small tools combine into a quick report:
grep 'ERROR' app.log | cut -d' ' -f1 | sort | uniq -c | sort -rn | head
cut picks columns, sort orders lines, uniq -c counts adjacent duplicates (sort first), and awk '{print $2}' prints a field. tee writes output to a file while still showing it.
Edit with sed
sed does not edit a file without -i; preview output before choosing in-place editing. sed -i.bak 's/8080/9090/' app.conf edits in place and keeps app.conf.bak as a backup. sed -n '10,20p' file prints only lines 10 to 20.