How to Remove (Delete) Files and Directories in Linux

The command line is a powerful tool that allows you to perform daily tasks more quickly and efficiently than a graphical interface. But it can also be dangerous if used incorrectly. This is especially true when removing unnecessary files or directories from the system.
Desktop users can use the GUI to easily delete files, and restore those files out of the Trash if needed. However, if you are working on a headless server, the command line utilities allow you to remove multiple files and directories at once using wildcards or other pattern matches.
In Linux, there are several tools that can be used to delete files and directories. Be extra careful when removing files or directories because once a file is deleted it cannot be easily recovered, even with recovery software.
This article goes through several different tools that allow you to remove files and directories in Linux. We will explain how to use the rm, unlink, shred, and rmdir commands.
How to Remove Files in Linux
To remove (or delete) a file in Linux from the command line, you can use the rm, shred, or unlink commands.
The unlink
command allows you to remove only a single file, while with rm and shred, you can remove multiple files at once.
File names with a space in them must be escaped with a backslash (\).
Using rm command
To delete a single file, invoke the rm
command followed by the file name:
rm filenameIf you do not have write permissions on the parent directory, you will get an “Operation not permitted” error.
You will be prompted for confirmation if the file is write-protected, as shown below. To remove the file, type y, and hit Enter. Otherwise, if the file is not write-protected, it will be deleted without prompting.
rm: remove write-protected regular empty file 'filename'?To delete multiple files at once, invoke the rm command followed by the names of the files you want to remove, separated by a space:
rm filename1 filename2 filename3You can also match multiple files with a wildcard (*) and regular expansions. For example, to remove all .pdf files in the current directory, you would use the following command:
rm *.pdfWhen using wildcards, first list the files with the ls
command so that you can see what files will be deleted before running the rm command.
Use the rm with the -i option to confirm each file before deleting it:
rm -i filename(s)To remove files without prompting, even if the files are write-protected, pass the -f (force) option to the rm command:
rm -f filename(s)You can also combine rm options. For example, to remove all .txt files in the current directory without a prompt in verbose mode, use the following command:
rm -fv *.txtUsing shred command
When a file is removed in Linux, the actual data is not deleted; only the reference inode (metadata about a file) is removed, and the corresponding data blocks are marked as available for reuse. The content of the file will stay on the disk until the space is needed for new data.
The shred command overwrites a file to hide its contents, making it more difficult or impossible to recover the deleted file. Use this command to protect your privacy and ensure no one can restore your removed files.
To overwrite and delete a file with shred, invoke the command with the -u option followed by the file name:
shred -u filenameThe file will be overwritten and then truncated and removed from the system.
You can also pass multiple files separated by a space:
shred -u filename1 filename2 filename3Using unlink command
To remove a file with unlink, invoke the command followed by the name of the file you want to remove:
unlink filenameOn success, the command does not produce any output and returns zero.
How to Remove Directories (Folders)
In Linux, you can remove/delete directories
with the rmdir and rm commands.
rmdir is a command-line utility for deleting empty directories, while with rm, you can remove directories and their contents recursively.
Using rmdir command
rmdir is a command-line utility used to delete empty directories. It comes in handy when you want to delete a directory only if it is empty, without checking whether the directory is empty or not.
To remove an empty directory, use the rmdir command with the directory name as argument:
rmdir dirnameIf the directory is not empty, you will get an error message.
Using rm command
By default, when used without any option, rm cannot remove directories.
To remove an empty directory with rm, invoke the command with the -d option:
rm -d dirnameTo remove non-empty directories and all the files within them, use the rm command with the -r (recursive) option:
rm -r dirnameIf a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.
To remove non-empty directories and all files without being prompted, use rm with the -r (recursive) and -f options:
rm -rf dirnameTo remove multiple directories at once, use rm -r followed by the directory names separated by a space:
rm -r dirname1 dirname2 dirname3As with files, you can also use a wildcard (*) and regular expansions to match multiple directories.
Quick Reference
| Command | Description |
|---|---|
rm filename | Delete a single file |
rm filename1 filename2 | Delete multiple files |
rm *.pdf | Delete all .pdf files in the current directory |
rm -i filename | Prompt before each deletion |
rm -f filename | Force delete without prompting |
rm -r dirname | Recursively delete a directory and its contents |
rm -rf dirname | Force recursive delete without prompting |
rmdir dirname | Delete an empty directory |
unlink filename | Delete a single file via the unlink system call |
shred -u filename | Overwrite and delete a file securely |
For a printable quick reference, see the rm cheatsheet .
Troubleshooting
“Operation not permitted” or “Permission denied”
You do not have write permissions on the file or its parent directory. Use ls -l to check permissions. If you own the file, use chmod to adjust permissions, or prefix the command with sudo if you need elevated privileges.
“Directory not empty” when using rmdir
rmdir only removes empty directories. To delete a directory and everything inside it, use rm -r dirname.
Accidentally deleted files with a wildcard
Files deleted with rm are not sent to the Trash and cannot be recovered easily. Before running rm *.ext, preview the matches with ls *.ext first. For critical operations, use rm -i to confirm each file individually.
File with a name starting with - cannot be deleted
If a filename begins with a dash, rm interprets it as an option. Use rm -- -filename or rm ./-filename to work around this.
FAQ
What is the difference between rm and unlink?
Both remove files. rm is the standard tool and supports multiple files, wildcards, and the -r/-f flags. unlink calls the underlying system call directly and can only remove one file at a time — no options, no wildcards.
Can I recover a file deleted with rm?
Not easily. rm does not move files to a Trash folder — it removes the directory entry and marks the disk blocks as free. Recovery may be possible with forensic tools if the blocks have not been overwritten, but it is not guaranteed. Always double-check before deleting.
When should I use shred instead of rm?
Use shred -u when you need to ensure the file’s content cannot be recovered, for example before decommissioning a drive or deleting sensitive data. For routine file deletion, rm is sufficient.
Is rm -rf dangerous?
Yes. rm -rf deletes everything in the target path immediately and without confirmation. Never run it with /, ~, or a variable that might be empty as the argument. Always double-check the path before pressing Enter.
How do I delete all files in a directory without deleting the directory itself?
Use rm -r dirname/* to delete the contents while keeping the directory, or rm dirname/* to delete only non-hidden files one level deep.
Conclusion
The rm command is the primary tool for deleting files and directories in Linux. Use rmdir when you want to safely remove only empty directories, unlink when removing a single file via the system call, and shred when secure deletion is required. For a complete reference on rm options and flags, see the rm command guide
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page