⚠ Attention: which commands need -r for directories?

While writing these topics I noticed that some commands need -r to work with a directory, and others don’t. -r means recursive: “go inside the directory and handle everything in it, level by level”.

Need -r: cp and rm

Don’t need it: mv, rmdir and ls

CommandWith a directoryWhy
cpneeds -r (or -a, which includes it)it must copy every file inside, one by one
rmneeds -rit must delete every file inside, one by one
mvno -rit moves the directory as one item, the content goes with it
rmdirno -rit removes only empty directories, so there is nothing inside to handle
lsno -rit lists one directory. With -R (capital) it also lists every sub-directory

Without -r, cp and rm refuse a directory:

$ cp somedir newcopy
cp: -r not specified; omitting directory 'somedir'

$ rm somedir
rm: cannot remove 'somedir': Is a directory

mv doesn’t need it. The whole directory moves, with its content:

$ mv somedir moved
$ ls -R moved
moved:
notes
sub

moved/sub:

Watch the letter case too: cp and rm accept -r (and -R), but for ls, -r means reverse order (like in ls -lrt) and only -R means recursive. There’s no logic to it, you just learn them.

Cheat sheet

GoalCommand
All files, including hiddenls -a
Newest file at the bottomls -lrt
Where am I?pwd
Copy a file into a directory, safelycp file dir/
Copy a directory with everything in itcp -r dir target/
Exact copy, keeping permissionscp -a dir target/
Copy only the content, hidden files toocp -a dir/. .
Move or renamemv old new
Delete a directory with its content, for everrm -rf dir
Files with one unknown letterls c?t

Based on: Red Hat RHCSA 10 Cert Guide, Chapter 3. The questions are the ones I wrote in the margins while reading, the explanations are my own, and every command was run with the output shown.