10 ‘rm’ command examples for beginners

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.

linux-rm-command-options

Example:1 Remove or delete a file.

Let’s delete a file with name “linuxstufff.log”

[linuxtechi@cloud ~]$ rm linuxstufff.log
[linuxtechi@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}

[linuxtechi@cloud ~]$ rm file1.txt file2.txt file3.txt file4.txt
[linuxtechi@cloud ~]$

Example:2 Delete the files interactively.

-i‘ option in rm command will prompt before deleting a file, example is shown below.

[linuxtechi@cloud ~]$ rm -i linuxstufff.log 
rm: remove regular file ‘linuxstufff.log’? y
[linuxtechi@cloud ~]$

Example:3 Delete a empty directory

use ‘-d‘ option in rm command to delete a empty directory.

[linuxtechi@cloud ~]$ ls -R appdata/
appdata/:
[linuxtechi@cloud ~]$ rm -d appdata/
[linuxtechi@cloud ~]$

we can also use ‘rmdir‘ command to delete empty folder or directory.

[linuxtechi@cloud ~]$ ls -R appdata/
appdata/:
[linuxtechi@cloud ~]$ rmdir appdata
[linuxtechi@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.

[linuxtechi@cloud ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 26 23:59 file1.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 26 23:59 file2.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 26 23:59 file3.log
drwxrwxr-x. 2 linuxtechi linuxtechi 6 Mar 26 23:59 service

dbstore/service:
total 0
[linuxtechi@cloud ~]$ rm -r dbstore/
[linuxtechi@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.

[linuxtechi@cloud ~]$ ls -lR dbstore/
dbstore/:
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 00:02 file1.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 00:02 file2.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 00:02 file3.log
drwxrwxr-x. 2 linuxtechi linuxtechi 6 Mar 27 00:02 service

dbstore/service:
total 0
[linuxtechi@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
[linuxtechi@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’

[linuxtechi@cloud ~]$ ls -l tech.txt 
-r--r--r--. 1 linuxtechi linuxtechi 0 Mar 27 00:23 tech.txt
[linuxtechi@cloud ~]$
[linuxtechi@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.

[linuxtechi@cloud ~]$ rm -f tech.txt 
[linuxtechi@cloud ~]$

Also try to delete a non-existing file.

[linuxtechi@cloud ~]$ rm -f nonexist.txt
[linuxtechi@cloud ~]$

Note : ‘-f’ option of rm command will not work for write-protect directories,

Let’s take an example , directory ‘/home/linuxtechi/location/ ‘ is write protected and file (‘db_stuff‘) inside this directory is non-protected.

[linuxtechi@cloud ~]$ ls -ld /home/linuxtechi/location/
drwxrwxr-x. 2 root root 29 Mar 27 00:43 /home/linuxtechi/location/

[linuxtechi@cloud ~]$ ls -l /home/linuxtechi/location/db_stuff 
-rw-rw-r--. 1 linuxtechi linuxtechi 17 Mar 27 00:43 /home/linuxtechi/location/db_stuff

[linuxtechi@cloud ~]$ rm -f /home/linuxtechi/location/db_stuff
rm: cannot remove ‘/home/linuxtechi/location/db_stuff’: Permission denied
[linuxtechi@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’.

[linuxtechi@cloud ~]$ ls -l linux_store/
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app1.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app2.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app3.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app4.log
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:07 app5.log
[linuxtechi@cloud ~]$ rm -I linux_store/app*
rm: remove 5 arguments? y
[linuxtechi@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‘.

[linuxtechi@cloud linux_store]$ pwd
/home/linuxtechi/linux_store
[linuxtechi@cloud linux_store]$ ll
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log1.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log2.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log3.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log4.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log5.txt
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 01:15 log6.txt
........................................
[linuxtechi@cloud linux_store]$ 
[linuxtechi@cloud linux_store]$ rm -f log{1..5}.txt
[linuxtechi@cloud linux_store]$

Delete all the files of the current directory that ends with ‘.txt’

[linuxtechi@cloud linux_store]$ rm -f *.txt
[linuxtechi@cloud linux_store]$

Delete all files of present working directory which has 3 characters in extension.

[linuxtechi@cloud linux_store]$ rm -f *.???
[linuxtechi@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/linuxtechi/linux_store‘ at once.

[linuxtechi@cloud linux_store]$ ls -l | wc -l
300001
[linuxtechi@cloud linux_store]$ rm *.log
-bash: /bin/rm: Argument list too long
[linuxtechi@cloud linux_store]$

To resolve this issue , use the below find command.

[linuxtechi@cloud ~]$ find ~/linux_store/ -type f -exec rm {} \;
[linuxtechi@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.

[linuxtechi@cloud linux_store]$ ll
total 0
-rw-rw-r--. 1 linuxtechi linuxtechi 0 Mar 27 02:05 -store
[linuxtechi@cloud linux_store]$ rm -store
rm: invalid option -- 's'
Try 'rm --help' for more information.
[linuxtechi@cloud linux_store]$

Use below either of the command to delete such files.

[linuxtechi@cloud linux_store]$ rm -- \ -store 
[linuxtechi@cloud linux_store]$

OR

[linuxtechi@cloud linux_store]$ rm ./\ -store 
[linuxtechi@cloud linux_store]$

2 Responses

  1. Stack says:

    Instead of:
    $ find ~/linux_store/ -type f -exec rm {} \;
    use:
    $ find ~/linux_store/ -type f -delete

  2. Everaldo MENOUT says:

    This “rm — \ -store” can be “rm — -store”

Leave a Reply

Your email address will not be published. Required fields are marked *