pwd
Print the absolute path of the current working directory.
/home/user
-P- Print the physical path with every symlink resolved.
-L- Print the logical path, keeping symlinks as typed (default).
Move around the filesystem, inspect files and find what changed. Each entry has a worked example and the flags worth knowing, and where one is easy to get wrong, the gotcha.
cat -n <file>Print a file with line numbers, handy when a config error reports a line.
cat -n notes.txt
1 This is a sample notes file.
2 Learn Linux commands step by step.
-n-A-sGotcha A config that looks right but fails to parse often has Windows line endings. cat -A shows them as ^M$.
head -n <N> <file>Print the first N lines of a file or stream (10 by default).
head -n 5 /etc/passwd
-n N-c Ntail -n <N> <file>Print the last N lines of a file, where the newest log lines live.
tail -n 20 /var/log/syslog
-n N-c Ntail -F <file>Follow a file as it grows, and keep following across log rotation.
tail -F /var/log/nginx/error.log
-f-F--pid=PIDGotcha tail -f keeps reading the old, rotated file forever. Use -F on anything logrotate manages.
less <file>Page through a large file without loading it all into memory.
less +G /var/log/syslog
+G+F/pattern-Stac <file>Print a file with its lines in reverse order, so the newest log entries come first.
tac notes.txt
wc -l <file>Count lines, words or bytes. Pipe into it to count matches.
grep ' 500 ' access.log | wc -l
-l-w-c-mGotcha wc -l counts newlines, so a last line without a trailing newline is not counted.
stat <file>Show a file's inode metadata: size, mode, owner, and access, modify and change times.
stat -c '%a %U:%G %s %n' /etc/nginx/nginx.conf
644 root:root 2412 /etc/nginx/nginx.conf
-c FORMAT-Lfile <path>Identify a file's type from its contents rather than its extension.
file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB pie executable, x86-64, dynamically linked
-i-Lhexdump -C <path>Show the bytes of a file next to the characters they print as. It finds what an editor hides: a carriage return, a tab, a byte order mark, binary data.
printf 'key: value\r\n' | hexdump -C
00000000 6b 65 79 3a 20 76 61 6c 75 65 0d 0a |key: value..|
0000000c
-n 64xxdod -cGotcha 0d 0a at the end of a line is a Windows line ending. A script saved with those fails with "bad interpreter" or "command not found"; sed -i 's/\r$//' removes them.
diff -u <old> <new>Compare two files and print a unified diff, the same format code review tools use.
diff -u nginx.conf.bak nginx.conf
-u-r-q-wmkdir -p <dir>Create a directory and any missing parents. Succeeds quietly if it already exists.
mkdir -p projects/api/logs
-p-m MODE-vtouch <file>Create an empty file, or update the timestamps of an existing one.
touch deploy.lock
-c-d DATE-mecho <text> > <file>Write text to a file. > truncates and replaces; >> appends to the end.
echo "maintenance until 14:00" >> notes.txt
>>>2>&1Gotcha One > instead of >> wipes the file. set -o noclobber makes > refuse to overwrite existing files.
<cmd> | sudo tee -a <file>Write a stream to a file and to stdout. With sudo, it is the correct way to append to a root-owned file.
echo "10.0.0.5 api.internal" | sudo tee -a /etc/hosts
-a> /dev/nullGotcha sudo echo text > /etc/hosts fails with permission denied: the redirect is performed by your unprivileged shell, not by sudo.
cp -a <src> <dest>Copy files or trees while preserving mode, ownership, timestamps and symlinks.
cp -a /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
-a-r-i-vGotcha Take a .bak copy before editing a live config. It turns a bad edit into a one-command rollback.
mv <src> <dest>Move or rename a file. Within one filesystem this is an atomic rename.
mv app.conf.new app.conf
-i-n-vGotcha Write a new config to a temp file in the same directory and mv it into place, so readers never see a half-written file.
rm <file>Delete files. There is no trash can; removal is immediate.
rm build.lock
-r-f-i-IGotcha rm -rf "$DIR"/* with an empty $DIR expands to /*. Use ${DIR:?} so the shell aborts when the variable is unset.
ln -s <target> <link>Create a symbolic link that points at another path.
ln -sfn /opt/app/releases/42 /opt/app/current
-s-f-nGotcha Target first, link name second, the same order as cp. Switching a current symlink is a common zero-downtime release trick.
find <dir> -name <pattern>Walk a directory tree and print paths that match name, type, age or size tests.
find /var/log -name "*.log" -mtime +7
-name / -iname-type f|d|l-mtime +N-size +100M-maxdepth NGotcha Quote the pattern. Unquoted *.log is expanded by your shell before find ever sees it.
find <dir> -mmin -<N>List files changed in the last N minutes. The first question in many incidents: what changed?
find /etc -type f -mmin -60
-mmin -N-newer FILE-cmin -Nfind <dir> -xdev -size +<N>Find the largest files on one filesystem when a disk is filling up.
find / -xdev -type f -size +500M
-xdev-size +N[kMG]-printf '%s %p\n'find <dir> ... -deleteDelete every file that matches the tests. Always run the same command without -delete first.
find /tmp -type f -name "*.tmp" -mtime +3 -delete
-delete-exec cmd {} +-print0Gotcha Put -delete last. find evaluates left to right, so find . -delete -name '*.tmp' deletes everything before the name test runs.
<list> | xargs <cmd>Turn lines of input into arguments for another command.
find . -name "*.bak" -print0 | xargs -0 rm -v
-0-n N-P N-I {}Gotcha Without -0, a file name with a space becomes two arguments and xargs acts on the wrong paths.
historyList the commands you ran in this shell. Pipe it to grep to find the one that worked last time.
history | grep systemctl
!!!$Ctrl-Rhistory -cGotcha A leading space keeps a command out of history when HISTCONTROL includes ignorespace. Use it before typing anything secret, then rotate the secret anyway.
watch -n <sec> <cmd>Re-run a command every few seconds and redraw the output. Good for watching a recovery happen.
watch -n 2 'ss -tln'
-n SEC-d-gGotcha Quote the command when it has a pipe or a variable; otherwise your own shell runs that part once, before watch starts.
Free, instant, and it works on your phone. No signup: start as a guest and save your progress later.