02
File & Directory Operations
- Create project tree for a service
- Copy config with backup; move/rename safely
- Avoid destructive rm mistakes
Must-know cold
mkdir -p path/to/dir · touch file · cp -a src dest · mv old new
rm file · rm -r dir · never casual rm -rf /
ln -s target linkname (symlink)
Commands
touch
Definition: Create empty files or update file timestamps.
| Option | Argument | Meaning | Example |
|---|
| (none) | FILE... | Create empty file(s) or update mtime | touch a.txt |
-c | FILE | Do not create if missing; only update time | touch -c a.txt |
-a | FILE | Change access time only | touch -a a.txt |
-m | FILE | Change modification time only | touch -m a.txt |
-t | STAMP FILE | Set time [[CC]YY]MMDDhhmm[.ss] | touch -t 202608011200 a.txt |
-r | REF FILE | Use REF’s timestamps | touch -r old.txt new.txt |
Flag combos
| Combo | Meaning | Example |
|---|
touch a b c | Create multiple empties | touch f1 f2 f3 |
mkdir
Definition: Create directories (optionally with parent paths).
| Option | Argument | Meaning | Example |
|---|
| (none) | DIR | Create directory (fails if parent missing) | mkdir logs |
-p | DIR | Create parents as needed; no error if exists | mkdir -p /opt/app/bin |
-m | MODE DIR | Set mode (e.g. 750) at creation | mkdir -m 750 secret |
-v | DIR | Print each created directory | mkdir -pv a/b/c |
Flag combos
| Combo | Meaning | Example |
|---|
mkdir -p a/b/c | Nested tree in one shot | mkdir -p /var/app/{log,data} |
cp
Definition: Copy files or directory trees to a new location.
| Option | Argument | Meaning | Example |
|---|
| (none) | SRC DEST | Copy file to DEST | cp a.txt b.txt |
| (none) | SRC... DIR | Copy multiple into directory | cp a b /tmp/ |
-r / -R | SRC DEST | Recursive (required for directories) | cp -r conf/ conf.bak/ |
-a | SRC DEST | Archive: recursive + preserve attrs (-dR --preserve=all) | cp -a /etc/nginx /backup/ |
-p | SRC DEST | Preserve mode, ownership, timestamps | cp -p conf conf.bak |
-i | SRC DEST | Interactive; prompt before overwrite | cp -i a b |
-n | SRC DEST | No clobber; never overwrite | cp -n a b |
-u | SRC DEST | Copy only if SRC newer than DEST | cp -u a b |
-v | SRC DEST | Verbose | cp -v a b |
-L | SRC DEST | Follow symlinks in SRC | cp -L link dest |
-P | SRC DEST | Never follow symlinks (copy link itself) | cp -P link dest |
Flag combos
| Combo | Meaning | Example |
|---|
cp -av src/ dest/ | Safe recursive backup style | cp -av /etc/myapp /backup/myapp |
cp -i config config.bak | Prompt if bak exists | cp -i f f.bak |
mv
Definition: Move or rename files and directories.
| Option | Argument | Meaning | Example |
|---|
| (none) | SRC DEST | Rename or move | mv old.txt new.txt |
| (none) | SRC... DIR | Move items into directory | mv a b /tmp/ |
-i | SRC DEST | Prompt before overwrite | mv -i a b |
-n | SRC DEST | No clobber | mv -n a b |
-v | SRC DEST | Verbose | mv -v a b |
-u | SRC DEST | Move only if SRC newer | mv -u a b |
-b | SRC DEST | Backup existing DEST (suffix ~) | mv -b a b |
Flag combos
| Combo | Meaning | Example |
|---|
mv -i file dir/ | Safe move into dir | mv -i app.conf /etc/app/ |
rm
Definition: Remove files or recursively delete directory trees (destructive).
| Option | Argument | Meaning | Example |
|---|
| (none) | FILE | Delete file | rm file.txt |
-r / -R | DIR | Recursive delete directory tree | rm -r olddir/ |
-f | PATH | Force; ignore missing, no prompt | rm -f temp |
-i | PATH | Prompt each delete | rm -i important |
-I | PATH | Prompt once if many/recursive | rm -I *.log |
-v | PATH | Verbose | rm -v a |
-d | DIR | Remove empty directory (like rmdir) | rm -d emptydir |
Flag combos
| Combo | Meaning | Example |
|---|
rm -rf path | Force recursive — double-check path | rm -rf /tmp/work/ only |
rm -ri path | Recursive with prompts | rm -ri staging/ |
rmdir
Definition: Remove empty directories only.
| Option | Argument | Meaning | Example |
|---|
| (none) | DIR | Remove empty directory only | rmdir emptydir |
-p | DIR | Remove DIR and empty parents | rmdir -p a/b/c |
-v | DIR | Verbose | rmdir -v emptydir |
Flag combos
| Combo | Meaning | Example |
|---|
rmdir -p a/b/c | Clean empty chain | After emptying leaves |
ln
Definition: Create hard or symbolic links to files/directories.
| Option | Argument | Meaning | Example |
|---|
| (none) | TARGET LINK | Hard link LINK → TARGET (same inode) | ln file hardlink |
-s | TARGET LINK | Symbolic link | ln -s /opt/app/bin/tool tool |
-f | TARGET LINK | Force replace existing LINK | ln -sf /new/path link |
-n | — | Treat LINK as file if symlink to dir | ln -snf target link |
-v | — | Verbose | ln -sv target link |
-r | TARGET LINK | Relative symlink (GNU) | ln -sr ../bin/x x |
Flag combos
| Combo | Meaning | Example |
|---|
ln -sfn target link | Update symlink in place (common deploy) | ln -sfn /opt/app-1.2 /opt/app |
Common recipes
| Goal | Command |
|---|
| Nested dirs | mkdir -p /opt/app/{bin,conf,logs} |
| Backup file | cp -a config.yml config.yml.bak |
| Rename | mv oldname newname |
| Empty file | touch /tmp/flag |
| Current version symlink | ln -sfn /opt/app-1.3 /opt/app |
| Delete only if empty | rmdir staging |
| Copy tree preserving attrs | cp -a src_dir/ dest_dir/ |
Pitfalls
cp dir dest without -r fails for directories.
rm -rf has no trash — verify pwd and path; avoid globs like rm -rf $VAR/* if VAR is empty.
- Hard links cannot cross filesystems; cannot link directories (normally).
- Symlink order:
ln -s TARGET LINKNAME — easy to reverse by mistake.
mv across filesystems = copy + delete (attrs may differ).
For more details, try man <command> in your terminal.