Filesystem navigation and file management
This domain, Filesystem navigation and file management, checks shell fluency as a file tool: moving through the hierarchy, locating files by attribute, copying and archiving safely, linking, and reading or filtering text streams.
Core commands
- Navigation:
pwd,cd,ls -la, absolute versus relative paths,.and... - Finding:
findwalks the tree and filters on name, type, size, time, owner, and permissions;locatequeries a database refreshed byupdatedband is fast but can be stale. - Text processing:
cat,less,head,tail -f,grep,cut,sort,uniq,wc,sed,awk. - Copying and archiving:
cp -apreserves mode, ownership, timestamps, and symlinks;rsync -acopies incrementally;tar -czfandtar -xzfcreate and extract archives. - Links: a hard link is another name for the same inode and cannot cross filesystems; a symlink is its own inode holding a path and can cross filesystems or point to a directory.
Worked example
Compress logs older than seven days with find /var/log -type f -name '*.log' -mtime +7 -exec gzip {} \;. Reasoning: -type f excludes directories and devices, -name limits the pattern, -mtime +7 means modified more than seven 24-hour periods ago, and -exec ... \; runs one command per match (-exec ... + batches arguments for speed). Test first by replacing the action with -print.
Traps
- Running
rm -rfwith an unset or mistyped variable; quote and use${var:?}. - Assuming
cp -rpreserves ownership and timestamps; it does not. - Deleting a symlink with a trailing slash, which can affect the target.
- Feeding
findoutput into a loop without-print0andxargs -0, so spaces break it.