English

How to Delete a File or Directory in Linux from the Command Line

4 min
VPS/VDS Hosting

By the end of this tutorial, you will be able to delete a single file, an empty directory, a whole directory tree, and files filtered by name or age, checking the target each time before you act. The Linux command line has no trash can: a deletion is permanent. The method matters as much as the command.

Prerequisites

  • Shell access to a Linux machine (local or over SSH), for example a Linux VPS
  • A user with write permission on the directory concerned (or sudo)
  • A recent backup of anything important

Check the target before deleting

Move into the right directory and list its contents. It takes two seconds and prevents most accidents.

Bash
pwd ls -la

For a pattern (extension, prefix), first display what it will match. rm will expand to exactly the same list.

Bash
ls -d *.log

Delete a file

The basic command is rm.

Bash
rm report.txt

Add -i to have it ask for confirmation, and -v to have it print what it removes.

Bash
rm -iv report.txt

Then check with ls report.txt: the message "No such file or directory" confirms the file is gone.

Delete several files

Shell wildcards let you target a batch. List first with ls, then delete.

Bash
rm -v *.tmp rm -v photo_{01..05}.jpg

Avoid a bare wildcard such as rm * in a directory you are not sure about. If a filename starts with a hyphen, use -- to mark the end of the options:

Bash
rm -- -strange-file.txt

Delete an empty directory

rmdir only removes empty directories. That is deliberate: it acts as a safety net.

Bash
rmdir old_folder

To also remove parent directories that become empty:

Bash
rmdir -p project/cache/tmp

Delete a directory and everything in it

This requires the recursive option -r. First, estimate what you are about to lose.

Bash
du -sh old_project find old_project -type f | wc -l

Then delete, with confirmation:

Bash
rm -ri old_project

With thousands of files, prompting for each one becomes impractical. The -f option turns it off:

Bash
rm -rf old_project

Never put an unchecked variable in this command. With rm -rf "$DIR"/, if $DIR is empty, you are targeting the root directory. In a script, add set -u or use "${DIR:?}", which aborts execution if the variable is empty.

Delete files by age

find filters on precise criteria (name, type, age, size). The principle: run it without any action first, then add -delete.

Bash
find /var/log/myapp -type f -name "*.log" -mtime +30

If the list matches what you expect:

Bash
find /var/log/myapp -type f -name "*.log" -mtime +30 -delete

-mtime +30 matches files modified more than 30 days ago. Put -delete last: find evaluates expressions from left to right, and a -delete placed too early removes everything.

Erase a sensitive file more securely

rm removes the file's directory entry, not necessarily the data on the disk. shred overwrites the content before deleting it.

Bash
shred -u -v secret.key

This approach has limits on SSDs, journaling file systems and volumes with snapshots, where older data may remain. For truly sensitive data, encrypting the disk from the start remains the reliable solution.

Troubleshooting

Permission denied

You do not have write permission on the parent directory (it is the directory that controls deletion, not the file). Check with ls -ld DIRECTORY, then use sudo if that is legitimate:

Bash
sudo rm -v /path/to/file

Operation not permitted, even with sudo

The file probably has the immutable attribute. Inspect it, remove the attribute, then delete.

Bash
lsattr file.txt sudo chattr -i file.txt sudo rm file.txt

Argument list too long

The wildcard expanded to more arguments than the system accepts. Use find instead, which handles files one at a time:

Bash
find . -maxdepth 1 -type f -name "*.tmp" -delete

Directory not empty or Device or resource busy

With rmdir, the first message is expected: the directory contains files, including hidden ones (ls -A). If a mount point or a process is using it, identify it with lsof +D DIRECTORY or findmnt, then release it before trying again.

To practise safely, a disposable instance is handy: on a VPS, you can create a test directory and make mistakes with no consequences. More guides of this kind are collected in the tutorials section.

FAQ

Can you recover a file deleted with rm?

Rarely, and with no guarantee. Tools such as extundelete or testdisk sometimes find data if you stop writing to the disk immediately. The only reliable protection is a backup.

What is the difference between rm -r and rm -rf?

rm -r deletes recursively but prompts for confirmation on write-protected files. rm -rf forces the deletion and silently ignores nonexistent files. Reserve it for cases where you have already checked the target.

How do you delete a directory whose name contains spaces?

Wrap the name in quotes or escape the spaces: rm -r "My folder" or rm -r My\ folder. Without that, the shell treats each word as a separate item.

Is there a trash can on the command line?

Yes, through the trash-cli package (command trash-put), which moves items to the user's trash instead of erasing them. On a server it is rarely installed by default, so get into the habit of listing before you delete.

Stay up to date with the latest offers, discounts and special promotions.

View promotions

We use cookies

We use analytics cookies to improve your experience on our site. No data is sold to third parties. Learn more in our privacy policy.