The command line does not have a Trash folder. When you delete a file from the terminal, it is gone immediately. There is no undo and no recovery confirmation by default.
This guide explains how to remove files and directories in Linux using rm, shred, unlink, and rmdir — and when to reach for each one.
The rm command is the standard tool for deleting files. To delete a single file:
bashrm filename
rm requires write permission on the parent directory, not just the file itself. If you lack that permission, the command fails with “Operation not permitted” regardless of the file’s own permissions.
Write-protected files trigger a confirmation prompt before deletion. To skip prompts entirely — including for write-protected files — use -f (force):
bashrm -f filename
To confirm each file interactively before deleting, use -i. This is the safer choice when working with wildcards:
bashrm -i *.pdf
Wildcard safety. Before running rm *.ext, preview the matches with ls first:
bashls *.pdf # confirm what would be deletedrm *.pdf # then remove
Combine options for verbose output:
bashrm -fv *.txt # force delete + print each filename removed
Filenames starting with - are misread as flags. Prefix with -- or ./ to work around it:
bashrm -- -filenamerm ./-filename
unlink removes a single file by calling the kernel’s unlink(2) system call directly. It accepts no flags, no wildcards, and only one filename at a time:
bashunlink filename
Use unlink when you want minimal interface and no accidental multi-file behavior. Use rm for everything else.
Standard rm does not erase the actual data on disk. It removes the inode reference — the metadata pointer to the file — and marks the underlying data blocks as free for reuse. The file’s content stays on disk until something else writes over those blocks.
That means deleted files can often be recovered with forensic tools if the disk blocks have not been overwritten yet.
shred -u closes this gap:
bashshred -u filename
It runs in three steps: overwrite the file’s data blocks (multiple passes by default), truncate the file to zero bytes, then remove it. The overwrite step makes the original content unrecoverable.
bashshred -u filename1 filename2 # multiple files
Use shred when deleting sensitive data — credentials, private keys, confidential documents — or before decommissioning a drive. For routine file deletion, rm is sufficient.
rmdir deletes empty directories only:
bashrmdir dirname
If the directory contains any files or subdirectories, rmdir fails with an error and leaves the directory untouched. This makes it the safe choice in scripts when you want to remove a directory only if it is truly empty.
rm -r recursively deletes a directory and everything inside it:
bashrm -r dirname
Write-protected files inside the directory will trigger prompts. To suppress all prompts:
bashrm -rf dirname
rm -rf is irreversible and will delete everything at the path with no confirmation. Never run it with /, ~, or a variable that might be empty or unset. Always double-check the path.
To delete multiple directories at once:
bashrm -r dirname1 dirname2 dirname3
To delete only the contents of a directory while keeping the directory itself:
bashrm -r dirname/* # removes all contents recursively
Use rm for files, rm -r for directories, and rmdir when you want safe deletion of empty directories only. Add -i or preview with ls before using wildcards, and reach for shred -u only when the file’s content must not survive deletion. Leave a comment below if you run into any issues.
Systemd is the init system on most modern Linux distributions. A service file (also called…
Linux is a multi-user system. Each person or service that needs access should have its…
The watch command in Linux runs a command at regular intervals and refreshes the terminal with the…
The xargs command in Linux reads items from standard input and passes them as arguments to another…
The locate command in Linux searches for files and directories by name. It queries a pre-built database…
Most modern Linux distributions use systemd as the default service manager. Knowing how to list…