Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

02

File & Directory Operations

  • Create project tree for a service
  • Copy config with backup; move/rename safely
  • Avoid destructive rm mistakes
touchmkdircpmvrmrmdirln

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.

OptionArgumentMeaningExample
(none)FILE...Create empty file(s) or update mtimetouch a.txt
-cFILEDo not create if missing; only update timetouch -c a.txt
-aFILEChange access time onlytouch -a a.txt
-mFILEChange modification time onlytouch -m a.txt
-tSTAMP FILESet time [[CC]YY]MMDDhhmm[.ss]touch -t 202608011200 a.txt
-rREF FILEUse REF’s timestampstouch -r old.txt new.txt

Flag combos

ComboMeaningExample
touch a b cCreate multiple emptiestouch f1 f2 f3

mkdir

Definition: Create directories (optionally with parent paths).

OptionArgumentMeaningExample
(none)DIRCreate directory (fails if parent missing)mkdir logs
-pDIRCreate parents as needed; no error if existsmkdir -p /opt/app/bin
-mMODE DIRSet mode (e.g. 750) at creationmkdir -m 750 secret
-vDIRPrint each created directorymkdir -pv a/b/c

Flag combos

ComboMeaningExample
mkdir -p a/b/cNested tree in one shotmkdir -p /var/app/{log,data}

cp

Definition: Copy files or directory trees to a new location.

OptionArgumentMeaningExample
(none)SRC DESTCopy file to DESTcp a.txt b.txt
(none)SRC... DIRCopy multiple into directorycp a b /tmp/
-r / -RSRC DESTRecursive (required for directories)cp -r conf/ conf.bak/
-aSRC DESTArchive: recursive + preserve attrs (-dR --preserve=all)cp -a /etc/nginx /backup/
-pSRC DESTPreserve mode, ownership, timestampscp -p conf conf.bak
-iSRC DESTInteractive; prompt before overwritecp -i a b
-nSRC DESTNo clobber; never overwritecp -n a b
-uSRC DESTCopy only if SRC newer than DESTcp -u a b
-vSRC DESTVerbosecp -v a b
-LSRC DESTFollow symlinks in SRCcp -L link dest
-PSRC DESTNever follow symlinks (copy link itself)cp -P link dest

Flag combos

ComboMeaningExample
cp -av src/ dest/Safe recursive backup stylecp -av /etc/myapp /backup/myapp
cp -i config config.bakPrompt if bak existscp -i f f.bak

mv

Definition: Move or rename files and directories.

OptionArgumentMeaningExample
(none)SRC DESTRename or movemv old.txt new.txt
(none)SRC... DIRMove items into directorymv a b /tmp/
-iSRC DESTPrompt before overwritemv -i a b
-nSRC DESTNo clobbermv -n a b
-vSRC DESTVerbosemv -v a b
-uSRC DESTMove only if SRC newermv -u a b
-bSRC DESTBackup existing DEST (suffix ~)mv -b a b

Flag combos

ComboMeaningExample
mv -i file dir/Safe move into dirmv -i app.conf /etc/app/

rm

Definition: Remove files or recursively delete directory trees (destructive).

OptionArgumentMeaningExample
(none)FILEDelete filerm file.txt
-r / -RDIRRecursive delete directory treerm -r olddir/
-fPATHForce; ignore missing, no promptrm -f temp
-iPATHPrompt each deleterm -i important
-IPATHPrompt once if many/recursiverm -I *.log
-vPATHVerboserm -v a
-dDIRRemove empty directory (like rmdir)rm -d emptydir

Flag combos

ComboMeaningExample
rm -rf pathForce recursive — double-check pathrm -rf /tmp/work/ only
rm -ri pathRecursive with promptsrm -ri staging/

rmdir

Definition: Remove empty directories only.

OptionArgumentMeaningExample
(none)DIRRemove empty directory onlyrmdir emptydir
-pDIRRemove DIR and empty parentsrmdir -p a/b/c
-vDIRVerbosermdir -v emptydir

Flag combos

ComboMeaningExample
rmdir -p a/b/cClean empty chainAfter emptying leaves

ln

Definition: Create hard or symbolic links to files/directories.

OptionArgumentMeaningExample
(none)TARGET LINKHard link LINK → TARGET (same inode)ln file hardlink
-sTARGET LINKSymbolic linkln -s /opt/app/bin/tool tool
-fTARGET LINKForce replace existing LINKln -sf /new/path link
-n—Treat LINK as file if symlink to dirln -snf target link
-v—Verboseln -sv target link
-rTARGET LINKRelative symlink (GNU)ln -sr ../bin/x x

Flag combos

ComboMeaningExample
ln -sfn target linkUpdate symlink in place (common deploy)ln -sfn /opt/app-1.2 /opt/app

Common recipes

GoalCommand
Nested dirsmkdir -p /opt/app/{bin,conf,logs}
Backup filecp -a config.yml config.yml.bak
Renamemv oldname newname
Empty filetouch /tmp/flag
Current version symlinkln -sfn /opt/app-1.3 /opt/app
Delete only if emptyrmdir staging
Copy tree preserving attrscp -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.

Previous01 Navigation & PathsNext03 View & Inspect Files