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.

3 min read
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

FlagMeaningWorks With
-r / -RRecursivecp, rm, chmod, chown
-fForce, no promptsrm, cp, mv
-iInteractive, confirm before overwritecp, mv, rm
-vVerbose outputmost commands
-pPreserve attributescp
-aArchive (recursive + preserve everything)cp, rsync

Production Considerations

  • Always run destructive commands (rm -rf, bulk mv) with -i or a dry run (find ... -print before adding -exec ... -delete) in production.
  • du and df can disagree — du sums file sizes, df reports 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/pwd for navigation, cp/mv/rm for manipulation, and find for 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 -rf without -i or double-checking the path with pwd first.
  • Using find ... -exec rm {} \; on thousands of files instead of the much faster -exec rm {} + or | xargs rm.
  • Trusting file extensions instead of file to 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