Listing (ls)
ls
List the current directory
ls -l
Long format: mode, owner, size, date
ls -a
Include hidden dotfiles
ls -lh
Human-readable sizes
ls -ltr
Oldest first, newest at the bottom
ls -d */
List only directories
Without -d, ls <dir> lists the directory contents
instead of the entry itself.
ls -R
Recurse into subdirectories
ls -i
Show inode numbers
Same inode number means the same file (hard links).
ls -lA
All entries except . and .. dirs
ls --group-directories-first
Directories before files
ls -F
Mark types: / dir, @ link, * exec
ls -ld <path>
Show the directory itself, not its contents
Copying (cp)
cp <src> <dst>
Copy a file
cp -r <dir> <dst>
Copy a directory recursively
cp -a <src> <dst>
Archive: recurse, keep links, mode, times
Also keeps ownership when run as root. The right choice for
backups.
cp -i <src> <dst>
Ask before overwriting
cp -n <src> <dst>
Never overwrite existing files
cp -u <src> <dst>
Only copy newer or missing files
cp -v <src> <dst>
Show each copy
cp -p <src> <dst>
Keep mode, owner and times
cp -t <dir> <files>
Target dir first, files after (xargs-friendly)
cp --backup=numbered <src> <dst>
Keep numbered backups of overwritten files
cp --reflink=auto <src> <dst>
Copy-on-write clone where supported
Instant on btrfs and XFS. auto falls back to a normal copy.
cp -L <src> <dst>
Follow symlinks and copy their targets
cp <src>/. <dst>
Copy directory contents, including dotfiles
Removing (rm, rmdir)
rm -i <file>
Confirm each deletion
rm -r <dir>
Delete a directory tree
rm -rf <dir>
Recursive, no prompts, ignore missing
Check the path with ls first. An empty variable or a stray
space in rm -rf $d/ can erase far more than intended.
rm -I <files>
One prompt for 4+ files or recursion
rm -v <file>
Print each removal
rm -- -<name>
Delete a file whose name starts with -
rm -d <dir>
Remove an empty directory
rmdir <dir>
Remove an empty directory only
rm --one-file-system -r <dir>
Do not cross mount points
Inspecting paths
stat <file>
Size, inode, mode, owner, times
stat -c '%a %U %s %n' <file>
Custom format: octal mode, owner, size, name
stat -c %y <file>
Last modification time
realpath <path>
Absolute path, symlinks resolved
realpath --relative-to=<dir> <path>
Path relative to a directory
basename <path>
Last path component
basename -s .txt <path>
Strip a suffix as well
dirname <path>
Everything but the last component
file <path>
Detect the file type
du -sh <path>
Total size of a path