Deleting files in Linux can be sometimes tricky. We have a tool named rm which is the shortcut for the word remove. In this tutorial, we will look at how to remove or delete a file in Linux with different examples and ways.
rm Command Syntax
rm
command syntax is the same as the most the Linux command. We can provide options before specifying the file and directories we cant to operate or delete.
rm OPTIONS FILENAME
OPTINS
states the behavior of therm
command as we will see below in detail.FILENAME
is the file or directory name we want to delete or operate.
rm Command Help
rm
command help information can be printed to the console with the --help
command which is like below. Help information will provide some popular options and usages.
$ rm --help

Delete Single File with rm Command
We will start with simple steps just deleting a single file. We will just specify the file name we want to delete. In order to remove the file successfully, we should have privileges to modify a file. For example, if we try to remove the file owned by root
with a regular user we will get an error and would not delete the file. In this example, we will delete the file named foo.txt
$ rm foo.txt
Delete Multiple Files with rm Command
We have the ability to delete multiple files in a single rm
command. We will just put file names we want to delete by separating them with space. In this example, we will delete file names foo.txt
and bar.txt
but we can add more if we need it.
$ rm foo.txt bar.txt
Delete Files According To Their Extensions/Types with rm Command
Linux bahs provides the glob or asterisk in order to specify the files with a specific name or extension. We can use glob *
in order to specify a specific extension like *.txt
, *.pdf
, *.tmp
etc. We can use this extension or name specification in order to delete specific files. In this example, we will delete all *.deb
extensions.
$ rm -v *.deb

We can also specify names like deleting all files which name starts with pof
like below.
$ rm pof*
Delete Files Recursively
rm
command provides the ability to delete or remove files recursively. Recursive removal will check subdirectories for files to remove with the directories. We will remove the directory name ndiff
with all sub-directories and files in this example. We will use -R
option for the recursive operation.
$ rm -R -v ndiff

Delete File with Prompt Before Every Removal
While removing files and directories we may need approval for each file to delete. In this case, we can use -i
option which will prompt to accept or deny deletion of the given file.
$ rm -R -i test

Print Verbose Output About Delete Operation
While deleting files and directories we may want to see details of the removal operation. rm
command provides a verbose option which will list information about each deletion of file or directory. We will use -v
option for this.
$ rm -R -v test

Delete empty Directories or Folders with rmdir Command
In some cases, we need to delete empty folders. rm
without options will not work in this case as we can see this in the following screenshot. We case use rmdir
command to remove an empty directory or folder.
$ rmdir delete_me/

Read File Names From Text File For Delete or Removal
Another interesting use case for rm
command is providing file or directory names from a list like a text file. We will use xargs
command to-read the list and redirect to the rm
command.
$ xargs rm < delete_file.txt

Delete File Names Starts with Dash -
Another interesting case is dash
or -
problem where file or directory names starting with dash
. As we know Linux commands options are specified with dash
. So how can rm
recognize file name from option? We will use --
or double dash were to specify the file or directory name start. For example we have a file named -file.txt
and we want to remove. We will use the following command. As we can see the file name is specified after double dash. Options are specified before the double dash.
$ rm -i -- -file.txt
Delete Files By Reading Their Names From A File/List
In some cases, we may need to read a list that contains the file names we want to delete. This is generally a simple text file where each file name with their path is specified line by line. We can use xargs
command to redirect the list contents to the rm
command which will delete them one by one. In this example, we will read the list file names file-list.txt
.
$ xargs rm -v < file-list.txt

Delete Files By Finding them with find Command
find
is a very useful command which is used to find files and folders. find
command also provides some options like running commands on the search results. We can also remove or delete files found by the find
command. In this example, we will delete files that are older than 3 days.
$ find . -type f -mtime +3 -exec rm '{}' ';' -print
