FreeKB - Linux Commands rm (remove delete)
Linux Commands - rm (remove delete)

The rm command can be used to remove files and directories. Let's take an example where there is a file named foo.txt.

[john.doe@server1 ~]# ls -l
-rw-rw-r--.  1  john.doe  john.doe  183  May 23 18:53  foo.txt

 

The rm command followed by the file name can be used to remove the file. In this example, foo.txt is removed (deleted).

[john.doe@server1 ~]# rm foo.txt
[john.doe@server1 ~]# ls -l

 


File does not exist (--force)

Let's say you try to remove a file that does not exist.

[john.doe@server1 ~]# rm bogus.txt
rm: cannot remove ‘bogus.txt’: No such file or directory

 

The -f or --force flag can be used to suppress the standard error output when the file being removed does not exist.

[john.doe@server1 ~]# rm --force bogus.txt

 


Remove another users file

By default, you can remove files you own, and you cannot remove files you do not own. For example, if jane.doe attempts to remove foo.txt, which is owned by john.doe, an error will appear.

[jane.doe@server1 ~]# rm /home/john.doe/foo.txt
rm: cannot remove '/home/john.doe/file1': Permission denied

 

If jane.doe is a member of the sudo group, jane.doe can remove foo.txt using the sudo command.

[jane.doe@server1 ~]# sudo rm /home/john.doe/foo.txt

 

Root can remove any file without having to use sudo.

[root@server1 ~]# rm /home/john.doe/foo.txt

 


Remove directories

Let's say there is a directory named bin in the /home/john.doe directory.

[john.doe@server1 ~]# ls -l
drwxrwxr-x.  2  john.doe  john.doe  6  May 23 19:06  bin

 

The rm command without any options will display an error when attempting to remove the bin directory.

[john.doe@server1 ~]# rm /home/john.doe/bin
rm: cannot remove '/home/john.doe/bin': Is a directory

 

The -r, -R, or --recursive option can be used to remove directories that are not empty. This will also remove all of the contents inside of the directory.

[john.doe@server1 ~]# rm -r /home/john.doe/bin

 

The -d or --dir option can be used to remove a directory that is empty.

[john.doe@server1 ~]# rm -d /home/john.doe/bin

 


Prompt before removal

To ebb on the side of caution, the -i or -I options can be used to display a prompt before removing a file.

[john.doe@server1 ~]# rm -i /home/john.doe/foo.txt
rm: remove regular file 'foo.txt'?

 

The -i option will prompt for every file being removed. For example, let's say the /home/john.doe directory contains 2 files, foo.txt and bar.txt.

[john.doe@server1 ~]# rm -i /home/john.doe/*
rm: remove regular file 'foo.txt'?
rm: remove regular file 'bar.txt'?

 

The -I option will only prompt once for every file being removed.

[john.doe@server1 ~]# rm -I /home/john.doe/*
rm: remove 2 arguments?

 


Exclude files from being removed

This command will remove all of the files in the present working directory except for foo.txt. However, this will not remove files that contain spaces in the file name.

ls | grep -v foo.txt | xargs rm

 

This will remove files that contain spaces in the file name.

ls | grep -v foo.txt | parallel rm

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 0249b in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |