Chapter 3: Files and directories

Linux files and directories cheat sheet

The chapter on one page: what to remember, the commands and patterns to have at your fingertips, and flashcards for quick revision.

The chapter in eight sentences

Cheat sheet

Navigate

CommandDoes
pwdPrint the current directory
cd DIR, cd, cd -, cd ..Go to DIR / home / back / up one level
ls -l, -a, -h, -R, -d, -t, -iLong, all, human sizes, recursive, the directory itself, newest first, inode numbers
touch FILECreate an empty file, or update its time stamp

Manage

CommandDoes
mkdir DIR…, mkdir -p A/B/CCreate directories; with all missing parents
cp SRC DST, cp -r DIR DSTCopy files; directories need -r
cp -p, -i, -vKeep times and permissions; ask before overwriting; report
mv SRC DSTMove, or rename
rm FILE, rm -r DIR, rm -iRemove files; directories; ask first
rmdir DIRRemove an empty directory
ln FILE NAME / ln -s TARGET NAMEHard link / symbolic link
ln -sfn TARGET NAMERepoint an existing symbolic link
readlink -f NAMEWhere a link finally leads

Expansions

WriteExpands to
* ? [abc] [!abc] [[:digit:]]Matching file names (a pattern that matches nothing stays as written)
{a,b,c} {1..10} {01..10} {a..e}Generated words, files or not
~, ~userHome directories
$VAR, ${VAR}A variable's value
$(command)A command's output
"…" / '…' / \xOnly $ expands / nothing expands / take the next character literally

Flashcards

Where does system-wide configuration live?
`/etc`, almost all as text files.
Which directory is emptied at every boot?
`/run`, which holds runtime data for running programs.
What makes a path absolute?
It starts with `/`. Anything else is relative to the current directory.
What do `.`, `..` and `~` mean in a path?
This directory, its parent, and your home directory.
Go back to the previous directory?
`cd -`
Create a/b/c when none of them exist?
`mkdir -p a/b/c`
Copy a directory and everything in it?
`cp -r SRC DST`
Rename report.txt to final.txt?
`mv report.txt final.txt`
What does a link count of 2 mean?
The file has **two names** (hard links) for the same inode.
Two things a symbolic link can do that a hard link cannot?
Point to a **directory**, and point **across file systems**.
What happens to a symbolic link when its target is deleted?
It **dangles**: it still exists, but opening it fails.
Why doesn't `*` match `.bashrc`?
Patterns don't match a leading dot unless they start with one, such as `.*`.
What does Bash do with a pattern that matches nothing?
Passes it on **unchanged** to the command.
Double quotes or single quotes to keep `$HOME` literal?
**Single** quotes. Double quotes still expand ` Linux files and directories Cheat Sheet (RHCSA) .