Top 9 rm Useful Command With Examples On Linux

Top 9 rm Useful Command With Examples On Linux

Today I will show you the top 9 rm useful command to delete the files using the Linux terminal. you should be very careful when you use this command because once you have deleted something with the command. you can’t retrieve those data.

Syntax: rm <options> {files}

I will show you all examples with root user.

Check Out: Top 11 Split Commands To Break Large File Into Small Files

Examples 1: you can simply use the ‘rm’ command to remove a file without any options. It will not prompt you before deleting the file.

[root@worker1 ~]# touch abc.txt
[root@worker1 ~]# ls -ld abc.txt 
-rw-r--r-- 1 root root 0 Mar 24 20:35 abc.txt
[root@worker1 ~]# rm abc.txt

Examples 2: we can remove multiple files with a pattern or extension match with command.

[root@worker1 files]# touch file1.txt file2.txt
[root@worker1 files]# ls
file1.txt file2.txt
[root@worker1 files]# rm *.txt
[root@worker1 files]# ls
[root@worker1 files]#

Check Out: How To Create Elastic File System(EFS) On Amazon Cloud

Examples 3: we can remove the directory with the ‘-d’ option when a directory is empty.

[root@worker1 files]# mkdir data
[root@worker1 files]# rm -d data/
[root@worker1 files]# ls

when the directory is not empty we can use ‘-R or –r’ to remove recursively everything in the directory.

[root@worker1 data]# mkdir data1 data2
[root@worker1 data]# cd ../
[root@worker1 files]# rm -r data

Examples 4: we will use the ‘-i’ option. It will prompt you before deleting the files.

[root@worker1 files]# touch abc
[root@worker1 files]# rm -i abc 
rm: remove regular empty file ‘abc’? yes

Check Out: How To Block Ads In Google Chrome Using Group Policy

Examples 5: Delete all the files with including subdirectories or files using ‘-ri‘ options. 

[root@worker1 files]# mkdir data
[root@worker1 files]# cd data/
[root@worker1 data]# touch file{1,3}
[root@worker1 data]# ls
file1 file3

Delete the files with ‘-ri‘ options.

root@worker1 files]# rm -ri data/
rm: descend into directory ‘data/’? yes
rm: remove regular empty file ‘data/file1’? yes
rm: remove regular empty file ‘data/file3’? yes
rm: remove directory ‘data/’? yes
[root@worker1 files]# ls
[root@worker1 files]#

Examples 6: you can remove the files forcefully using ‘-f’’ options. it will prompt before deleting the files.

[root@worker1 files]# touch file{1,2,3}
[root@worker1 files]# ls
file1 file2 file3
[root@worker1 files]# rm -f file1 file2 file3
[root@worker1 files]# ls
[root@worker1 files]#

You can check the rm command version using rm –version

[root@worker1 files]# rm --version
rm (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Richard M. Stallman,
and Jim Meyering.

Examples 7: Prompt once before deleting the files with ‘-I’ options.

[root@worker1 files]# touch file{1,2,3}
[root@worker1 files]# rm -I file1 file2 file3
rm: remove 5 arguments?y

Check Out: Learn Useful Tar Command With Example In Linux

Examples 8: Deleting the files with regular expression. Suppose I have created 3 txt files I want to remove them in a single command.

[root@worker1 files]# touch file{1,2,3}.txt
[root@worker1 files]# ls
file1.txt file2.txt file3.txt
[root@worker1 files]# rm -f file{1..3}.txt
[root@worker1 files]# ls
[root@worker1 files]#

Examples 9: if you any issue in deleting a large number of files. we have created 1 lakh empty file using the below commands. we’re not able to delete them with a single command because of large files. 

root@worker1 files]# touch file{1..100000}.txt
[root@worker1 files]# ls -l | wc -l
[root@worker1 files]# rm  *.txt
-bash: /usr/bin/touch: Argument list too long

We can remove these files with the below commands. 

[root@worker1 files]# touch file{1..100000}.txt
[root@worker1 files]# find . -type f -exec rm -rf {} \;
[root@worker1 files]# ls
[root@worker1 files]#

We can also use find . -type f -delete

Check Out: How To Disable IPv6 Network Interface On Linux 7

You can also get the information which file has deleted with rm -v filename command.

You’re done Linux delete file

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment