How to remove a file in Linux with rm command

rm command in Linux
rm command in Linux

How to remove a file in Linux Introduction

The tutorial How to remove a file in Linux with rm command will teach you to use rm command and its features to remove files and directories. Any Linux Administrator knows that once the file deleted by the command rm, it will not get recovered unless he uses any third party 'file recovery softwares'.
We will also learn about 'rmdir' command and its uses to remove directory.

  1. Remove directory

To remove directory in Linux, you can use both rmdir and rm command, See the below examples.

# rmdir  <Directory name>
# rmdir   directory

How to remove a file in Linux
How to remove a file in Linux - Removing directory

or

# rm <Directory name>
# rm directory
How to remove a file in Linux
How to remove a file in Linux - rm command

2. Remove file

To remove files in Linux, use the below command, Note, 'rmdir' command cannot be used to remove files.

# rm <file name>
# rm   file1.txt
How to remove a file in Linux
How to remove a file in Linux with rm command


3. Remove nested directory

Use 'p' option with 'rmdir' to remove nested directories and sub-directory in Linux.Without the option 'p' nested directories cannot be removed with rmdir.

# rmdir  /directory/directory2/directory3/directory4/

The above command will only delete the last directory that is directory4. To delete the full nested directory use the below command.

# rmdir -p  directory/directory2/directory3/directory4/

4. Remove non-empty directory

To remove non-empty directory recursively and forcefully, you need to use option 'r' and 'f' with rm command.

r = Recursive
f = Forcefully

# rm  -rf  directory1

How to remove a file in Linux
How to remove a file in Linux Forcibly

The above command will remove all the directories and the sub-directory present inside it.


5. How to remove a file in Linux interactively

You may want a confirmation before you remove a file in Linux. Its better to use option 'i' with rm command to make the command act more interactive.

# rm  -i  file1.txt

 

How to remove a file in Linux
rm command with 'i'

 

The above command will ask you for the confirmation before removing the file. Press 'y'  if you want to delete the file and 'n'  if you don't want to delete. you can even add option 'r' along with the command when deleting non-empty directory. For example.

# rm -ir  directory1


6. Remove a file in Linux having a Dash(-)

For example try to remove a file starting with a dash.

# rm  -file1.txt

The above command will throw an error as given below.

rm command

You can remove these files by the following three ways.

# rm --  -file1.txt

or

# rm ./ -file1.txt                    If you are in the directory where the file is saved then use this command.

or

# rm  /directory/ -file1.txt             You can also give the full path of the file if you are in different directory.


7. Make aliases with rm command

Even though you can make many aliases with the 'rm' command. But some aliases are  always suggestible. For example if you are deleting any file or directory, its better to let the rm command alert you before deleting completely. Similarly, its a better idea to get the verbose output on the terminal while you are deleting any files.

# ls -al

The above command will show you a hidden file  called   .bashrc    or  .bash_profile. You need to write the new alias in one of these file. For example:

alias  rm="rm  -iv"

you need to make the above change in the file to make rm command interactive and give you verbose output when deleting something.

8. Find and remove files

With the combination of Linux find file command and rm command you can find files and remove them. For example, find files in a directory having size less than 100k and remove files. See the below command.

# find -size -100k -exec rm {} \;

Another way of doing the same thing is not using the above command, The below example will execute much faster. However, you may note down the above command for your knowledge and reference.

# find -size -100k -delete

9. Remove all files in a directory

To remove all files in a directory in Linux, use the below command.

# rm -r*

10. Never run this command

The below rm command is given just for information. I would suggest you to never run this command, as it will completely delete whole your Linux and UNIX servers and systems. It is a complete disastrous command.

# rm -rf /*

It deletes all the directories, sub-directories, files, configuration files of your Linux system present inside / (root) directory.

11. How to prevent accidentally deletion of files

There are many ways your important files in Linux may get deleted accidentally while running (rm) remove file command. You can prevent it happening by the following way.

Create a file under the directory where you keep all other important files and name it  '-nouse'.

# touch ./ -nouse

Now, run the command 'rm -rf *'

# rm -rf *
rm: remove regular empty file `file1'? n
rm: remove regular empty file `file2'? n

So, at this time the command won't remove any files and rather act interactively.

12. Rm --version

To know the current version of rm command, use '--version' option.

# 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.

13. Man rm

Last but not the least, to know more about rm command and its features, go to man page.

# man rm

 


 Don't forget to share if you like the article How to remove a file in Linux with rm command

Download Our Free eBook now

Linux and UNIX Shell scripting ebook

1 Comment on How to remove a file in Linux with rm command

Leave a Reply

Your email address will not be published.


*


shares