Memory hook: Quote paths; check status; know where output goes.
Must remember
pwd, ls -la, cd, mkdir, cp, mv and rm manipulate the filesystem. Quote expansions such as "$path" so spaces and wildcard characters are not interpreted unexpectedly. man, info and /usr/share/doc are local references; use man -k to find a topic.
| Syntax | Meaning |
|---|---|
command >file / >>file |
Replace / append standard output. |
2>errors |
Redirect standard error. |
>out 2>&1 |
Send both streams to out; order matters. |
| `a | b` |
| `grep -E '^(error | warn)' file` |
tar -czf backup.tar.gz directory |
Archive and gzip; list with tar -tf before extracting with -xf. |
Gzip and bzip2 compress streams; tar bundles files and metadata. A hard link is another name for the same inode on the same filesystem; a symbolic link stores a path and may cross filesystems or become dangling. ln source hard and ln -s target soft differ accordingly.
Scripts need an interpreter line and execute permission when run directly. $1 is the first argument, $? the previous exit status; zero conventionally means success. $(command) captures output. A simple pattern:
#!/bin/bash
if [ -f "$1" ]; then
for word in ready set go; do
printf '%s\n' "$word"
done
else
printf '%s\n' 'File not found' >&2
exit 1
fi
Use ssh user@host for a remote shell and su - user for a login-like user environment. Edit with a terminal editor and verify the saved content; an unsaved editor buffer is not configuration.
Choose under exam pressure
| Requirement | Choice and reason |
|---|---|
| Find matching text | grep with the appropriate basic or extended expression. |
| Preserve file identity under another name | Hard link, provided both names can share a filesystem. |
| Capture output in a script | Command substitution, with quoted use of the result. |
Traps
- A pipe does not automatically include stderr.
- A symlink does not keep its target alive after the target is removed.
Active recall
1. Why quote "$1"?
To preserve it as one argument rather than split words or expand wildcards.
2. What does >> do?
Appends stdout instead of truncating the destination.
3. Can a hard link cross filesystems?
No; it names the same inode within one filesystem.
4. How do you inspect an archive before extraction?
Use tar -tf and check its paths and contents.
5. What does exit status zero normally indicate?
Success; conditionals test status, not printed wording.