rm stands for ‘remove‘, as the name suggests rm command is used to delete or remove files and directory in Linux and UNIX like operating systems. If you are new to Linux then you should be very careful while running rm command because once you delete the file or directory then you can not recover the contents of file 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 we 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”

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}

Example:2 Delete the files interactively.

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

Example:3 Delete an empty directory in linux



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

we can also use ‘rmdir‘ command to delete an empty directory in linux.

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.

[[email protected] ~]$ 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 [[email protected] ~]$ rm -r dbstore/ [[email protected] ~]$

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.

[[email protected] ~]$ 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 [[email protected] ~]$ 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 [[email protected] ~]$

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’

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.

Also try to delete a non-existing file.

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.

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’.

[[email protected] ~]$ 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 [[email protected] ~]$ rm -I linux_store/app* rm: remove 5 arguments? y [[email protected] ~]$

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‘.

[[email protected] linux_store]$ pwd /home/linuxtechi/linux_store [[email protected] 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 ........................................ [[email protected] linux_store]$ [[email protected] linux_store]$ rm -f log{1..5}.txt [[email protected] linux_store]$

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

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

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.

To resolve this issue , use the below find command.

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.

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

Use below either of the command to delete such files.

Read More on : 16 Useful ‘cp’ Command Examples for Linux Beginners