Bootstrap FreeKB - Linux Commands - Delete files and directories using the rm (remove) command
Linux Commands - Delete files and directories using the rm (remove) command

Updated:   |  Linux Commands articles

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

 


rm write protected file

You may be prompted "rm write protected file". echo n (no) or echo y (yes) can be used to avoid this prompt.

echo n | rm foo.txt

 


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

 


File begins with one or two dashes

Let's say you have a file that begins with one or two dashes.

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

 

Here is how you can remove files that begin with one or two dashes.

rm ./-foo.txt

 


Remove back slash

Let's say you have a file that is just a single back slash.

~]$ ls -l
-rw-r--r--. 1 john.doe users   17686 Feb  8 06:45 \
-rw-r--r--. 1 john.doe users   23529 Feb 22 03:57 foo.txt
-rw-r--r--. 1 john.doe users    2800 Feb 17 01:06 bar.txt

 

Here is how you can remove the single back slash file.

rm \\

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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