rm stands for ‘remove‘ as the name suggests rm command is used to delete or remove files and directory in UNIX like operating system. If you are new to Linux then you should be very careful while running rm command because once you delete the files then you can not recover the contents of files and directory. Though there are some tools and commands through which deleted files can be recovered but for that you need expert skills.
In this post i will demonstrate 10 Linux rm command examples. Below is the basic syntax of rm command.
# rm <options> {files}
options used in rm command.
Example: 1 Remove or delete a file.
Let’s delete a file with name “linuxstufff.log”
[lpicentral@cloud ~]$ rm linuxstufff.log
[lpicentral@cloud ~]$
Delete multiple files at once.
Let’s assume that i want to delete four text files at once. Use the below syntax
# rm {file1} {file2}] {file3} {file4}
[lpicentral@cloud ~]$ rm file1.txt file2.txt file3.txt file4.txt
[lpicentral@cloud ~]$
Example: 2 Delete the files interactively.
‘-i‘ option in rm command will prompt before deleting a file, example is shown below.
[lpicentral@cloud ~]$ rm -i linuxstufff.log
rm: remove regular file ‘linuxstufff.log’? y
[lpicentral@cloud ~]$
Example: 3 Delete a empty directory
use ‘-d‘ option in rm command to delete a empty directory.
[lpicentral@cloud ~]$ ls -R appdata/
appdata/:
[lpicentral@cloud ~]$ rm -d appdata/
[lpicentral@cloud ~]$
we can also use ‘rmdir‘ command to delete empty folder or directory.
[lpicentral@cloud ~]$ ls -R appdata/
appdata/:
[lpicentral@cloud ~]$ rmdir appdata
[lpicentral@cloud ~]$
Example: 4 Deleting a directory recursively using ‘-r’ option
‘-r‘ option in rm command will delete all the files and sub-directories recursively of the parent directory.
[lpicentral@cloud ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 26 23:59 file1.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 26 23:59 file2.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 26 23:59 file3.log
drwxrwxr-x. 2 lpicentral lpicentral 6 Mar 26 23:59 service
dbstore/service:
total 0
[lpicentral@cloud ~]$ rm -r dbstore/
[lpicentral@cloud ~]$
Example: 5 Delete the files and sub-directories interactively.
Use ‘-ri‘ option in rm command to delete file and sub-directories interactively, Let’s assume we want to all files and directories of ‘dbstore’ directory interactively.
[lpicentral@cloud ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 00:02 file1.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 00:02 file2.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 00:02 file3.log
drwxrwxr-x. 2 lpicentral lpicentral 6 Mar 27 00:02 service
dbstore/service:
total 0
[lpicentral@cloud ~]$ rm -ri dbstore/
rm: descend into directory ‘dbstore/’? y
rm: remove regular empty file ‘dbstore/file1.log’? y
rm: remove regular empty file ‘dbstore/file2.log’? y
rm: remove regular empty file ‘dbstore/file3.log’? y
rm: remove directory ‘dbstore/service’? y
rm: remove directory ‘dbstore/’? y
[lpicentral@cloud ~]$
Example: 6 Deleting files forcefully using ‘-f’ option
‘-f‘ option in rm command will remove or delete the files forcefully regardless of its permissions and will also ignore non-existing files.
Let’s delete a write-protected file ‘tech.txt’
[lpicentral@cloud ~]$ ls -l tech.txt
-r--r--r--. 1 lpicentral lpicentral 0 Mar 27 00:23 tech.txt
[lpicentral@cloud ~]$
[lpicentral@cloud ~]$ rm tech.txt
rm: remove write-protected regular empty file ‘tech.txt’?
As we can see above that when we try to delete a write-protected file using rm command without ‘-f’ option , it gives us a prompt to delete write-protected file.
Now try to delete a file using ‘-f’ option.
[lpicentral@cloud ~]$ rm -f tech.txt
[lpicentral@cloud ~]$
Also try to delete a non-existing file.
[lpicentral@cloud ~]$ rm -f nonexist.txt
[lpicentral@cloud ~]$
Note : ‘-f’ option of rm command will not work for write-protect directories,
Let’s take an example , directory ‘/home/lpicentral/location/ ‘ is write protected and file (‘db_stuff‘) inside this directory is non-protected.
[lpicentral@cloud ~]$ ls -ld /home/lpicentral/location/
drwxrwxr-x. 2 root root 29 Mar 27 00:43 /home/lpicentral/location/
[lpicentral@cloud ~]$ ls -l /home/lpicentral/location/db_stuff
-rw-rw-r--. 1 lpicentral lpicentral 17 Mar 27 00:43 /home/lpicentral/location/db_stuff
[lpicentral@cloud ~]$ rm -f /home/lpicentral/location/db_stuff
rm: cannot remove ‘/home/lpicentral/location/db_stuff’: Permission denied
[lpicentral@cloud ~]$
Example 7: Prompt once before deleting more than three files or recursive delete.
‘-I‘ option in rm command will prompt once before deleting more than three files or recursive delete.
Suppose i want to delete all log files which starts with the name ‘app’ under the directory ‘linux_store’.
[lpicentral@cloud ~]$ ls -l linux_store/
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app1.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app2.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app3.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app4.log
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:07 app5.log
[lpicentral@cloud ~]$ rm -I linux_store/app*
rm: remove 5 arguments? y
[lpicentral@cloud ~]$
Example: 8 Regular expression in rm command
We can use regular expression in the rm command, some of the examples are shown below :
Let’s delete 5 log files starting from log1 to log5 under the directory ‘linux_store‘.
[lpicentral@cloud linux_store]$ pwd
/home/lpicentral/linux_store
[lpicentral@cloud linux_store]$ ll
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log1.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log2.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log3.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log4.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log5.txt
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 01:15 log6.txt
........................................
[lpicentral@cloud linux_store]$
[lpicentral@cloud linux_store]$ rm -f log{1..5}.txt
[lpicentral@cloud linux_store]$
Delete all the files of the current directory that ends with ‘.txt’
[lpicentral@cloud linux_store]$ rm -f *.txt
[lpicentral@cloud linux_store]$
Delete all files of present working directory which has 3 characters in extension.
[lpicentral@cloud linux_store]$ rm -f *.???
[lpicentral@cloud linux_store]$
Example: 9 Delete large number files using rm command.
If your are trying to delete large number of files using rm command then you will get an error message ‘Argument list too long’
In the below example i am trying to delete all the files (around ‘300001’) of the directory ‘/home/lpicentral/linux_store‘ at once.
[lpicentral@cloud linux_store]$ ls -l | wc -l
300001
[lpicentral@cloud linux_store]$ rm *.log
-bash: /bin/rm: Argument list too long
[lpicentral@cloud linux_store]$
To resolve this issue , use the below find command.
[lpicentral@cloud ~]$ find ~/linux_store/ -type f -exec rm {} \;
[lpicentral@cloud ~]$
Example: 10 Delete a file which starts with hyphen symbol (-)
Let’s assume that we have a file with name ‘-store‘ in our current working directory and we want to delete this file.
[lpicentral@cloud linux_store]$ ll
total 0
-rw-rw-r--. 1 lpicentral lpicentral 0 Mar 27 02:05 -store
[lpicentral@cloud linux_store]$ rm -store
rm: invalid option -- 's'
Try 'rm --help' for more information.
[lpicentral@cloud linux_store]$
Use below either of the command to delete such files.
[lpicentral@cloud linux_store]$ rm -- \ -store
[lpicentral@cloud linux_store]$
OR
[lpicentral@cloud linux_store]$ rm ./\ -store
[lpicentral@cloud linux_store]$