Read in 3 minutes
How to Remove Files and Directories Using Linux Command Line
To remove (or delete) a file or directory in Linux from the command line, use the
rm (remove) command.
Be extra careful when removing files or directories with the
rm command, because once the file is deleted it cannot be recovered.
In this tutorial, we will show you how to use the rm command to remove files and directories through examples and explanations of the most common rm options.
How to Remove Files
To delete a single file use the
rmcommand followed by the file name:
If the file is write protected you will be prompted for confirmation as shown bellow. To remove the file type
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 use the
rmcommand followed by the file names separated by space.
rm filename1 filename2 filename3
You can also use a wildcard (
*) and regular expansions to match multiple files. For example to remove all
When you use
rmwith wildcards (
*) and regular expansions, it is recommend to list the files with the
lscommand so that you can see what files will be deleted before running the
-ioption to confirm each file before deleting it:
rm -i filename(s)
To remove files without prompting even if the files are write-protected use the
rm -f filename(s)
You can also combine
rmoptions. For example, to remove all
.txtfiles in the current directory without a prompt in verbose mode use the following command:
rm -fv *.txt
How to Remove Directories (Folders)
To remove an empty directory use the
rm -d dirname
To remove non-empty directories and all the files within them, use the
rm -r dirname
If 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 the files without being prompted use use the
rm -rf dirname
To remove multiple directories at once, use the
rmcommand followed by the directory names separated by space.
rm -r dirname1 dirname2 dirname3
Same as with files you can also use a wildcard (
*) and regular expansions to match multiple directories.
By now you should have a good understanding of how to use the Linux
rm command and you should be able to safely remove files and directories using the command line.