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
- Linux keeps every file in one tree under
/: configuration in/etc, users' files in/home, software in/usr, variable data in/var, runtime data in/run, and scratch space in/tmp. - An absolute path starts at
/; a relative path starts from the current directory, and.,..and~stand for this directory, its parent and your home. pwdshows where you are,cdmoves you (cdalone goes home,cd -goes back), andls -lalists everything, including hidden dot files.mkdir -pcreates whole paths;cp(with-rfor directories) copies,mvmoves and renames, and both put files inside a destination that is an existing directory.rm(-rfor directories,-ito confirm) removes for good: there is no wastebasket.rmdirremoves only empty directories.- A hard link is a second name for the same inode; a symbolic link is a small file that points to a name, can reach directories and other file systems, and dangles if its target goes.
- Bash expands patterns (
*,?,[…]), braces ({a,b},{1..9}),~,$VARIABLESand$(commands)before a command runs;echoshows the result. - Double quotes keep spaces and patterns but still expand
$; single quotes and backslashes stop expansion altogether.
Cheat sheet
Navigate
| Command | Does |
|---|---|
pwd | Print the current directory |
cd DIR, cd, cd -, cd .. | Go to DIR / home / back / up one level |
ls -l, -a, -h, -R, -d, -t, -i | Long, all, human sizes, recursive, the directory itself, newest first, inode numbers |
touch FILE | Create an empty file, or update its time stamp |
Manage
| Command | Does |
|---|---|
mkdir DIR…, mkdir -p A/B/C | Create directories; with all missing parents |
cp SRC DST, cp -r DIR DST | Copy files; directories need -r |
cp -p, -i, -v | Keep times and permissions; ask before overwriting; report |
mv SRC DST | Move, or rename |
rm FILE, rm -r DIR, rm -i | Remove files; directories; ask first |
rmdir DIR | Remove an empty directory |
ln FILE NAME / ln -s TARGET NAME | Hard link / symbolic link |
ln -sfn TARGET NAME | Repoint an existing symbolic link |
readlink -f NAME | Where a link finally leads |
Expansions
| Write | Expands 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 |
~, ~user | Home directories |
$VAR, ${VAR} | A variable's value |
$(command) | A command's output |
"…" / '…' / \x | Only $ 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) .