The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Show the current directorypwdView examples
List directory contentsls -lahView examples
Change directorycd /var/logView examples
Move to the parentcd ..View examples
Return homecd ~View examples
Create nested directoriesmkdir -p project/src/componentsView examples
Create an empty filetouch notes.txtView examples
Identify a file typefile archive.tar.gzView examples
Print a text filecat notes.txtView examples
Read the first lineshead -n 20 app.logView examples
Read the last linestail -n 20 app.logView examples
Follow a growing logtail -f app.logView examples
Copy a file safelycp -i config.ini config.ini.bakView examples
Copy a directory treecp -a project project-backupView examples
Move or rename an itemmv -i draft.txt final.txtView examples
Find files by namefind . -type f -name '*.json'View examples
Find recently changed filesfind . -type f -mtime -7View examples
Search text recursivelygrep -RIn --exclude-dir=.git 'TODO' .View examples
Measure directory sizedu -sh projectView examples
Remove a file interactivelyrm -i obsolete.txtView examples
Remove an empty directoryrmdir empty-directoryView examples
Remove a directory treerm -rI build-outputView 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

01

Orient yourself before changing files

Use pwd to confirm the current location, ls to inspect entries, and cd to move through the filesystem. Absolute paths begin at the root directory, relative paths begin at the current directory, and the shell expands ~ to the current user's home directory.

Inspect a project from the home directory
cd ~
pwd
ls -lah
cd projects/cmdmemo
pwd
ls -lah

Note: Use cd -- '/path/with spaces' or quote the path so the shell passes it as one argument.

Move between a directory and its parent
cd /var/log
pwd
cd ..
pwd
Back to quick reference ↑
02

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.

Prepare a small project tree
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.

Back to quick reference ↑
03

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.

Inspect configuration and logs
cat config.ini
head -n 20 app.log
tail -n 20 app.log
Watch new log entries
tail -f app.log

Note: Following a file is a long-running command. Press Ctrl+C to return to the prompt.

Back to quick reference ↑
04

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.

Back up a configuration before renaming it
cp -i config.ini config.ini.bak
mv -i config.ini config.production.ini
ls -lah config*
Create a project backup
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.

Back to quick reference ↑
05

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 JSON files and recent changes
find . -type f -name '*.json'
find . -type f -mtime -7
Locate TODO comments
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.

Compare project directory sizes
du -sh project project-backup
Back to quick reference ↑
06

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.

Inspect and remove obsolete output
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.

Remove a file and an empty directory
rm -i -- obsolete.txt
rmdir -- empty-directory
Back to quick reference ↑

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. GNU ProjectGNU Coreutils Manualgnu.org
  2. GNU ProjectGNU Findutils Manualgnu.org
  3. GNU ProjectGNU Grep Manualgnu.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback