Ask once before deleting more than 3 files or recursing
rm -rI build/
rmdir DIR
Delete an empty directory only
rmdir empty/
rm has no undo
There's no trash can on the command line. Check a glob first with ls
(ls *.tmp, then rm *.tmp). Never run rm -rf on a path built from a
variable that might be empty.
A tarball bundles many files into one .tar file; compression shrinks
it. tar does both. Remember the letters: create, extract,
list, file, verbose, z gzip, j bzip2, J xz.
Task
Command
Create .tar.gz
tar -czf backup.tar.gz project/
Create .tar.xz (smaller, slower)
tar -cJf backup.tar.xz project/
Create .tar.zst (fast and small)
tar --zstd -cf backup.tar.zst project/
Pick compression from the name
tar -caf backup.tar.zst project/
List contents
tar -tf backup.tar.gz (add -v for details)
Extract here
tar -xf backup.tar.gz (compression is auto-detected)
Extract into a directory
tar -xf backup.tar.gz -C /tmp/restore
Extract one file
tar -xf backup.tar.gz project/config.ini
Print one file to stdout
tar -xOf backup.tar.gz project/config.ini
Drop the top-level directory
tar -xf app-1.2.tar.gz --strip-components=1 -C app/
Run tar -tf file.tar.gz | head first. If the archive doesn't have a
single top-level directory, extract it into a new empty directory with
-C, so it doesn't scatter files everywhere.
find~-typef-size+500M-execls-lh{}+# files over 500 MBfind/var/log-name'*.gz'-mtime+30# compressed logs older than 30 daysfind.-typef-printf'%s %p\n'|sort-rn|head# 10 largest files, in bytes
A hard link is a second name for the same inode (the same data). A
symbolic link (symlink) is a small file that points to a path.
Command
What it does
Example
ln -s TARGET LINK
Create a symlink
ln -s /opt/app-2.1 /opt/app
ln -sfn TARGET LINK
Repoint an existing symlink
ln -sfn /opt/app-2.2 /opt/app
ln TARGET LINK
Create a hard link (same filesystem, files only)
ln data.csv data-link.csv
readlink LINK
Show where a symlink points
readlink /opt/app
readlink -f PATH
Resolve all symlinks to a final absolute path
readlink -f /usr/bin/vi
ls -li
Show inode numbers (hard links share one)
ls -li data*.csv
stat -c %h FILE
Number of hard links
stat -c %h data.csv
find -samefile F
Find all hard links to a file
find ~ -samefile data.csv
find -xtype l
Find broken symlinks
find ~ -xtype l
-rw-rw-r-- 2 alex alex 6 Oct 2 10:45 data.csv ← link count 2: two names, one inode
lrwxrwxrwx 1 alex alex 8 Oct 2 10:45 latest -> data.csv ← symlink: type "l", shows its target
Hard link
Symbolic link
Points to
An inode (the data)
A path (a name)
Works across filesystems
No
Yes
Can link to directories
No
Yes
If the original is deleted
Data survives
Link breaks ("dangling")
Common mistake: symlink argument order
It's ln -s TARGET LINK_NAME, the same order as cp SOURCE DEST. A
relative target is resolved from the link's directory, not from where
you ran the command.