File & Directory Management
The daily bread of every Linux user: listing, navigating, creating, copying, moving and viewing files from the terminal. Master ls, cd, mkdir, cp, mv, rm, cat, less, tail and nano, plus redirection with > and >> — the foundation of every shell script.
What is this?
File & directory management is the daily bread of every Linux user — listing, navigating, creating, copying, moving and viewing files from the terminal. The commands are small and composable: ls to look, cd to move, mkdir to create, cp and mv to duplicate and relocate, and cat/less/tail to read. Redirection with > and >> turns any command's output into a file, which is the foundation of shell scripting and logging.
Why do you need it?
- Almost everything on a server is a file — configs, logs, sockets — so file commands double as debugging commands.
- These commands are identical on every distribution and inside every container, so fluency transfers everywhere.
- Output redirection with > and >> is how every real script, cron job and container entrypoint captures logs.
- Being fluent with cp, mv and rm prevents the classic accidents: overwriting the wrong file and deleting the wrong directory.
- Interviewers treat these as a baseline; fumbling ls -l or cp -r is an immediate red flag in any ops role.
Mental model
A workshop tool rack
Linux file commands
Each tool has one job — the file cabinet labels (ls), the ladders between floors (cd), the photocopier (cp), the trolley (mv), and the reading lamp (cat/less). Once you know which tool to grab, the whole workshop runs itself.
How it works
- Every file is tracked by the kernel as an inode; a directory is just a list of names pointing to inodes.
- ls reads the directory and prints names; with -l it adds permissions, owner, size and timestamps.
- cp duplicates a file by creating a new inode and copying the contents into it.
- mv on the same filesystem is a rename — it only updates the directory entry, which is why it is instant.
- Redirection happens at the shell level: > opens a file for writing (truncating it), >> opens it for appending.
The lifecycle of a file: create, copy, view, move, delete.
Key commands
ls -lList files with permissions, owner, size and timestamps.
total 12 drwxr-xr-x 2 root root 4096 Aug 8 10:15 docs -rw-r--r-- 1 root root 18 Aug 8 10:15 README.md
mkdir -p a/b/c && echo okCreate nested directories in one shot, then confirm success.
ok
cp -r docs backup && lsRecursively copy a directory and list the result.
backup docs
mv README.md docs/Move README.md into docs; exit status 0 confirms success.
0
tail -n 3 /var/log/syslogShow the last three log lines — the fastest way to see what just happened.
Aug 8 10:16:01 dev-host CRON[1234]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Examples
Scaffold a project directory in two minutes
# Build the skeletonmkdir -p ~/project/{src,docs,logs}cd ~/project # Create a README with redirectionecho "# My Project" > README.mdecho "First release" >> README.md # Copy and inspectcp README.md docs/readme.mdcat README.mdls -R ~/project # Move and renamemv docs/readme.md docs/README-copy.mdThis walks the whole lifecycle: mkdir -p builds nested directories with brace expansion, > creates and >> appends, cp duplicates, cat shows, and mv renames — every pattern you will repeat in daily work.
In production
- Ship artifacts and configs with cp -a to preserve permissions and symlinks.
- Audit changes with ls -la and stat before and after deploys.
- Run tail -f application logs during a rollout to catch errors live.
- Use echo and variables with redirection in provisioning scripts, cloud-init and entrypoints.
- mv is atomic on a single filesystem — use mv file.tmp file to swap configs without a gap.
Best practices
- Quote paths with spaces: cp "My File" target.
- Use -- and -i to stay safe: rm -i prompts before deleting anything.
- Dry-run destructive commands with --dry-run or -n where supported.
- Prefer mv over copy-plus-delete for same-filesystem moves — it is atomic and fast.
- Back up before rm -rf, and always list what you are about to delete first.
Common mistakes
- rm -rf on an empty variable, e.g. rm -rf $DIR/ becoming rm -rf /.
- Catting huge logs instead of paging with less or tailing the last lines.
- Using > to append, silently clobbering the file you meant to keep.
- Assuming mv is always instant — across filesystems it is copy-plus-delete and can fail halfway.
Common issues
mv: cannot move 'x' to 'y': Device or resource busyThe target file is held open by a running process.
Find the holder with lsof y, stop it, then retry the move.
cp: omitting directory 'src'cp was used without -r on a directory.
Use cp -r src dest, or cp -a dest to also preserve attributes.
Permission denied when writing a file you ownThe parent directory lacks write permission, so new entries cannot be created even though the file is yours.
Check the parent with ls -ld . and grant write with chmod u+w . or chown the directory to your user.
Debugging tips
- ls -la shows hidden files — 'missing' files are usually dotfiles.
- stat <file> reveals the exact inode, size and modification time.
- file <name> tells you what a file really is, regardless of its extension.
- diff -u before.txt after.txt shows exactly what changed.
- echo $? after cp or mv tells you whether it succeeded (0 = success).
Interview checkpoint
Hands-on lab
- Create a nested directory structure with mkdir -p
- Create files with echo redirection using > and >>
- Copy and move files and directories with cp and mv
- Inspect files with cat, head, tail and wc
Build a project tree and manage its files
15 min · beginner
Quick quiz
Which command deletes a non-empty directory and everything inside it?
What does pwd print?
What does 'echo hello >> notes.txt' do?
mv can be used to rename a file in place.
Which command shows the last lines of a file and can follow a growing log live?
You must copy an entire directory including hidden files while preserving permissions and timestamps. Which command?
Cheat sheet
pwdPrint the working directorylsList files and directoriesls -laList all, including hidden, long formatls -lS / -ltSort by size / by timecd ~Go to your home directorycd -Go back to the previous directorymkdir newCreate a directorymkdir -p a/b/cCreate nested parents toormdir emptyRemove an empty directoryrm fileDelete a filerm -r dirDelete a directory treerm -i / rm -fPrompt before each / forcecp src dstCopy a filecp -r src dirCopy a directory recursivelycp -a src dstCopy preserving permissions and symlinksmv src dstMove or rename (atomic on one filesystem)mv dir/* .Move everything from dir into herecat filePrint the whole filetac filePrint lines in reverse orderless filePage through, scroll both directionsmore filePage through, forward onlyhead -n 5 fileFirst 5 linestail -n 10 fileLast 10 linestail -f fileFollow a growing file livenano fileSimple terminal editorvi filePowerful modal editorecho text > fileOverwrite a file with textecho text >> fileAppend text to a filecmd > out.txt 2>&1Capture stdout and stderr to a file