rm command

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.

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

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

[[email protected] files]# touch file1.txt file2.txt
[[email protected] files]# ls
file1.txt file2.txt
[[email protected] files]# rm *.txt
[[email protected] files]# ls
[[email protected] 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.

[[email protected] files]# mkdir data
[[email protected] files]# rm -d data/
[[email protected] files]# ls

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

[[email protected] data]# mkdir data1 data2
[[email protected] data]# cd ../
[[email protected] files]# rm -r data

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

[[email protected] files]# touch abc
[[email protected] 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. 

[[email protected] files]# mkdir data
[[email protected] files]# cd data/
[[email protected] data]# touch file{1,3}
[[email protected] data]# ls
file1 file3

Delete the files with ‘-ri‘ options.

[email protected] 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
[[email protected] files]# ls
[[email protected] files]#

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

[[email protected] files]# touch file{1,2,3}
[[email protected] files]# ls
file1 file2 file3
[[email protected] files]# rm -f file1 file2 file3
[[email protected] files]# ls
[[email protected] files]#

You can check the rm command version using rm –version

[[email protected] files]# rm --version
rm (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
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.

[[email protected] files]# touch file{1,2,3}
[[email protected] 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.

[[email protected] files]# touch file{1,2,3}.txt
[[email protected] files]# ls
file1.txt file2.txt file3.txt
[[email protected] files]# rm -f file{1..3}.txt
[[email protected] files]# ls
[[email protected] 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. 

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

We can remove these files with the below commands. 

[[email protected] files]# touch file{1..100000}.txt
[[email protected] files]# find . -type f -exec rm -rf {} \;
[[email protected] files]# ls
[[email protected] 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here