Guide
Linux
Beginner
File & Directory Commands
The essential Linux commands for navigating, creating, copying, moving, and searching files and directories, with real flags and use cases.
Navigation
pwd # print current working directory
cd /var/log # change directory
cd - # go back to previous directory
cd ~ # go to home directory
ls -la # list all files, including hidden, long format
ls -lh # human-readable sizes (K, M, G)
ls -lt # sort by modification time, newest first
Creating & Removing
mkdir project # create a directory
mkdir -p a/b/c # create nested directories in one shot
touch file.txt # create an empty file / update its timestamp
rm file.txt # remove a file
rm -r directory/ # remove a directory recursively
rm -rf directory/ # force, no confirmation — no undo, use with care
rmdir empty_dir/ # remove only if empty (safer than rm -r)
Copying & Moving
cp file.txt backup.txt # copy a file
cp -r src/ dst/ # copy a directory recursively
cp -p file.txt backup.txt # preserve permissions/timestamps
cp -a src/ dst/ # archive mode: recursive + preserve everything
mv old.txt new.txt # rename (or move if paths differ)
mv file.txt /tmp/ # move into another directory
mv -i file.txt dst/ # prompt before overwriting
Finding Files
find /var/log -name "*.log" # by name (glob pattern)
find . -type f -size +100M # files larger than 100MB
find . -type d -empty # empty directories
find /tmp -mtime +7 -delete # delete files older than 7 days
find . -name "*.tmp" -exec rm {} \; # run a command on every match
locate nginx.conf # fast, index-based search (needs updatedb)
which python3 # find an executable in $PATH
whereis python3 # binary, source, and man page locations
Comparing & Inspecting
diff file1.txt file2.txt # line-by-line differences
diff -u file1.txt file2.txt # unified diff (patch-friendly format)
wc -l file.txt # count lines
wc -c file.txt # count bytes
file report.pdf # identify file type from content, not extension
stat file.txt # detailed metadata: size, inode, timestamps, permissions
du -sh directory/ # total size of a directory
du -sh * | sort -rh | head -10 # top 10 largest items in current directory
Common Flags Cheat Sheet
| Flag | Meaning | Works With |
|---|---|---|
-r / -R | Recursive | cp, rm, chmod, chown |
-f | Force, no prompts | rm, cp, mv |
-i | Interactive, confirm before overwrite | cp, mv, rm |
-v | Verbose output | most commands |
-p | Preserve attributes | cp |
-a | Archive (recursive + preserve everything) | cp, rsync |
Production Considerations
- Always run destructive commands (
rm -rf, bulkmv) with-ior a dry run (find ... -printbefore adding-exec ... -delete) in production. duanddfcan disagree —dusums file sizes,dfreports actual disk block usage; a large gap often means deleted-but-open files (see the Files & Directories fundamentals article).- Prefer
find -exec {} +(batches arguments) over-exec {} \;(spawns one process per file) when running commands over large file sets — it’s significantly faster.
Quick Interview Answer
“Everyday file operations map to a small, consistent command set:
ls/cd/pwdfor navigation,cp/mv/rmfor manipulation, andfindfor search by name, size, age, or type. Flags like-r(recursive),-i(interactive/confirm), and-v(verbose) apply consistently across most of them, which is why learning the flag conventions once pays off everywhere.”
Common Mistakes
- Running
rm -rfwithout-ior double-checking the path withpwdfirst. - Using
find ... -exec rm {} \;on thousands of files instead of the much faster-exec rm {} +or| xargs rm. - Trusting file extensions instead of
fileto determine actual content type.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form