10 'rm' Command Examples for Linux Beginners

Channel: Linux
Abstract: [[email protected] linux_store]$ rm *.log[[email protected] linux_store]$ rm -f log{1..5}.txt

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」

[[email protected] ~]$ rm linuxstufff.log
[[email protected] ~]$

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}

[[email protected] ~]$ rm file1.txt file2.txt file3.txt file4.txt
[[email protected] ~]$
Example:2 Delete the files interactively.

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

[[email protected] ~]$ rm -i linuxstufff.log 
rm: remove regular file ‘linuxstufff.log’? y
[[email protected] ~]$
Example:3 Delete an empty directory in linux

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

[[email protected] ~]$ ls -R appdata/
appdata/:
[[email protected] ~]$ rm -d appdata/
[[email protected] ~]$

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

[[email protected] ~]$ ls -R appdata/
appdata/:
[[email protected] ~]$ rmdir appdata
[[email protected] ~]$
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’

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

[[email protected] ~]$ rm -f tech.txt 
[[email protected] ~]$

Also try to delete a non-existing file.

[[email protected] ~]$ rm -f nonexist.txt
[[email protected] ~]$

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.

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

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

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

[[email protected] linux_store]$ rm -f *.txt
[[email protected] linux_store]$

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

[[email protected] linux_store]$ rm -f *.???
[[email protected] 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.

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

To resolve this issue , use the below find command.

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

[[email protected] linux_store]$ rm -- \ -store 
[[email protected] linux_store]$

OR

[[email protected] linux_store]$ rm ./\ -store 
[[email protected] linux_store]$

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

Ref From: linuxtechi

Related articles