What rm actually does
The command rm calls the unlink system call for each name it is given. Unlink removes one directory entry and decrements the link count of the inode. When the count reaches zero and no process holds the file open, the kernel frees the blocks and the inode. Until then, the data may still be readable through another name or through an open descriptor, which is the reason a running process can keep writing to a log file that has been removed.
Freed blocks are reused by the filesystem, and the old bytes may remain on the disk until they are overwritten. That is why a deleted file is not reliably recoverable with ordinary tools, and why shredding a file does not guarantee erasure on journaled filesystems or on solid state drives. The right defense is to avoid the deletion or to have a backup, not to count on recovery.
cd /workspace
mkdir -p trash/full
touch trash/a.txt
rm trash/a.txt
ls trashThe file is gone from the listing at once. The space is freed as soon as the link count reaches zero, which here is immediately.
rmdir removes only empty directories
The command rmdir removes a directory, but only when the directory is empty. It refuses to remove a directory that holds any entry, including hidden ones, and it reports "Directory not empty". This is a useful safety net. A script that uses rmdir instead of rm -r cannot delete a tree that contains files it does not know about. Use rmdir in the common case of removing a directory you created and expect to be empty.
rm -r and the options that protect you
The option -r removes a directory and everything under it. It is the most dangerous option in common use, because one wrong path deletes a tree. The option -i prompts before each removal, and -I prompts once when more than three files are removed or when the operation is recursive. The option -f suppresses prompts and errors for missing files, which is useful in scripts that must succeed even when a file is absent, but it also hides mistakes.
The option -v prints each name as it is removed. In a new directory, run the command with -v first to see what it would do, or use find to list the same names. A list you have read is far safer than a command you have only imagined.
cd /workspace
touch -- -start-with-dash.txt
rm -v -- -start-with-dash.txtThe double dash ends option parsing, so the name is treated as a file. Without it, rm would read the name as an option and fail.
Protecting the root and other important paths
GNU rm refuses to recurse into the root directory by default, which is a guard against a command like rm -rf / with a stray space. Keep that protection on, and be wary of variables that may expand to nothing. A command such as rm -rf "$dir/"* deletes everything in the current directory if $dir is empty, because the path becomes the root. Quoting the variable does not help when the variable is empty. Check the variable first, and use the shell option set -u to make an unset variable an error.
Do not run rm with a glob and no check. A pattern such as rm * in the wrong directory removes every file there, and a hidden file is only removed if you name it. Run ls with the same pattern first, and read the result.
A safer habit: move before you remove
A practical alternative is to move the files to a holding directory first, and to delete that directory after a delay. A move inside one filesystem is only a rename, so it is as fast as a delete and fully reversible until you empty the holding area. Many desktop trash systems do exactly this. On a server, the same pattern is a cheap insurance policy.
Deleting in a loop
A common script removes files older than a given age. It is tempting to write a loop over ls output, but a safer structure lets find select the files and performs the deletion with -delete or with -exec rm plus a batch. Use find -mtime with a clear limit, run the find without the action first to see the list, and only then add the action. A dry run with -print costs nothing and catches the mistake you did not expect.
Deleting a file that is open in another program does not stop that program. The program keeps writing to the unlinked inode until it closes the file, and the space appears only after it does. A tool that expects a file to disappear immediately can be confused by this, so restart the program or truncate the file if the space matters more than the program state.
Recovering what was lost
If the data matters, the defense is a copy made before the deletion, not a recovery tool after it. Snapshots on a filesystem that supports them, regular backups to another disk, and version control for text files are all reliable. Forensic recovery tools can sometimes find the bytes of a deleted file, but they cannot promise the whole file or its name, and they do not work on every storage device. Plan so that you never need them.
Practice in the lab
The lab removes an empty directory, shows that rmdir refuses a directory with contents, and removes a file in verbose mode. Each result is saved, so the checker can confirm both what was removed and what was refused.