rm command in Linux. Delete a file(s) and directories.

delete file in linux

Introduction

The rm command is an utility in Linux and UNIX Like OS to delete a file in Linux or more files. rm command also helps to delete directories in Linux. This is very easy to use and widely used command and being a User or administrator you should be very familiar with this command.

This command normally operates silently and while invoking this command you should be very careful.

In this tutorial i will guide and instruct you how to use rm command with various types of example.

Syntax of rm Command

The general syntax for rm command is as below.

rm [option] [file]

For example i have a file abc.txt the command will be as below. If you wish you can execute with options.

$ rm abc.txt

Options

There are multiple options used with rm command. Let’s make a table and discuss about the various types of options.

OptionFunctionality
– iPrompt before every removal
-Iprompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving pro‐tection against most mistakes
-f(force) ignore nonexistent files and arguments,
-r or -Rremove directories and their contents recursively
-dRemove empty directory
-vVerbose– Explains what is being done

How to remove file(s) ?

When rm is invoked without any option,  it doesn’t remove directories. It also does not give any other warning or information.

Delete a file in Linux — When the below command is invoked it delete a single file. There is a file named tiger.txt and the command will be as below

$ rm tiger.txt

Remember if the file is not write protected it will remove silently without any warning. And a file once deleted can not be recoverd.

Deleting Multiple files is possible in rm command, you have 3 files tiger1, tigher2 and tiger3 in a directory the below command will delete all the files with one command.

$ rm tiger1 tiger2 tiger3

Remember rm tiger* will be dangerous to use as it will remove all the files starting with tiger.

You can use regular expression ‘*’ to match and delete multiple files. Suppose in your download directory there are lot of files laying and you wanted to delete all files with .pdf extension, so the below command will be helpful.

$ rm *.pdf

Removing all the files from a directory is very easy with rm command. You may sometimes need to delete all files in a directory. You can use the ‘*’ , as it represents all the available files in a directory to clean the complete directoy.

$ rm *

Important:: Whether you are able or not able to delete a file does not depend on the file permission but on the permission you have for the directory where the file resides.

Removing Directories #

We know rmdir helps to remove directories in Linux but you can also remove directory using rm command. To remove a directory(ies) rm is invoked with -d option which is identical to rmdir command.

For example the following command deletes a empty directory named dir1.

$ rm -d dir1

To Delete a non empty directory below is the command, rm is invoked with -r option to remove non empty directory. For example there is directory dir2 having some data.

$ $ rm -r dir2

Interactive Deletion

As you know rm command when invoked without any option it delete files silently. But the -i (interactive) option makes the command ask the user for deleting file. With this option you can delete a file in linux and more files at a time.

For example i have a file tiger1.txt below will be the command.

$ rm -i tiger1.txt
Output

rm: remove regular file 'tiger1.txt'?

Type yes or y to confirm then rm command will remove tiger1.txt file.

Forcing Removal of File

When a file is write protected rm prompts for removal. The -f option overrides this write protection and deletes the file forcefully. When you combine -f option with -r option it is more risky and will remove the directory completely.

Conclusion

In this tutorial i have instructed you how to remove files and directory(ies) in Linux. But while working with rm command you should be very caution. Make sure you are doing the right thing before using rm * .

Leave a Reply

Your email address will not be published. Required fields are marked *