The essentials
Quick reference
One focused task per row. Jump to the related section for complete, working examples.
| Use | Syntax | Examples |
|---|---|---|
| Show the current directory | pwd | View examples |
| List directory contents | ls -lah | View examples |
| Change directory | cd /var/log | View examples |
| Move to the parent | cd .. | View examples |
| Return home | cd ~ | View examples |
| Create nested directories | mkdir -p project/src/components | View examples |
| Create an empty file | touch notes.txt | View examples |
| Identify a file type | file archive.tar.gz | View examples |
| Print a text file | cat notes.txt | View examples |
| Read the first lines | head -n 20 app.log | View examples |
| Read the last lines | tail -n 20 app.log | View examples |
| Follow a growing log | tail -f app.log | View examples |
| Copy a file safely | cp -i config.ini config.ini.bak | View examples |
| Copy a directory tree | cp -a project project-backup | View examples |
| Move or rename an item | mv -i draft.txt final.txt | View examples |
| Find files by name | find . -type f -name '*.json' | View examples |
| Find recently changed files | find . -type f -mtime -7 | View examples |
| Search text recursively | grep -RIn --exclude-dir=.git 'TODO' . | View examples |
| Measure directory size | du -sh project | View examples |
| Remove a file interactively | rm -i obsolete.txt | View examples |
| Remove an empty directory | rmdir empty-directory | View examples |
| Remove a directory tree | rm -rI build-output | View examples |
Linux paths are case-sensitive and commands usually act relative to the current directory. Inspect targets before copying, moving, or removing them, and quote paths that contain spaces or shell metacharacters.
Step by step
Detailed examples
Create paths and identify their contents
mkdir -p creates missing parents, while touch creates an empty file or refreshes timestamps. File extensions are conventions on Linux, so use file when you need a content-based type estimate.
mkdir -p project/src/components
touch project/README.md
touch project/src/main.js
ls -lah project
file project/README.md project/src/main.js Note: touch does not clear an existing file; it updates timestamps unless the path does not exist.
Read complete files or focused portions
cat is convenient for short text files. Head and tail avoid flooding the terminal with large files, and tail -f waits for appended log lines until you interrupt it with Ctrl+C.
cat config.ini
head -n 20 app.log
tail -n 20 app.log tail -f app.log Note: Following a file is a long-running command. Press Ctrl+C to return to the prompt.
Copy, rename, and move items deliberately
cp creates another filesystem entry, while mv changes an item's path and is also used for renaming. Interactive mode protects against silent replacement. Use archive mode when copying a directory tree and preserving its common attributes matters.
cp -i config.ini config.ini.bak
mv -i config.ini config.production.ini
ls -lah config* cp -a project project-backup
ls -ld project project-backup Note: When the destination directory already exists, cp may place the source inside it instead of creating the layout you expected. Inspect both paths first.
Find paths, matching text, and disk usage
find searches directory trees by metadata such as name, type, and modification time. Quote find patterns so the shell does not expand them early. Use grep for file contents and du when the question is how much storage a tree consumes.
find . -type f -name '*.json'
find . -type f -mtime -7 grep -RIn --exclude-dir=.git 'TODO' . Note: Recursive grep can traverse generated files and dependencies. Add exclusions for directories such as node_modules when they are not part of the search.
du -sh project project-backup Remove files with a recoverability plan
rm normally removes files without sending them to a desktop trash folder. Start with interactive options, confirm the resolved path, and use rmdir for empty directories. Recursive removal should be reserved for a target you have just inspected.
ls -lah -- build-output
du -sh -- build-output
rm -rI -- build-output Note: The -- marker ends option parsing, so a filename beginning with a hyphen is treated as a path. Removed data may not be recoverable.
rm -i -- obsolete.txt
rmdir -- empty-directory Sources and further reading
References
Authoritative documentation used to verify and expand this cheat sheet.
Help us improve
Found a typo or missing example?
Tell us what would make this cheat sheet clearer, more complete, or more useful.



