/**/ How to delete files using rm command in Linux? - Dextutor

How to delete files using rm command in Linux?

Use

rm command in Linux is used to delete the files. Although by default it can remove files only but rm command can also be used to delete directories using certain options (a less known fact to beginners).

Syntax

rm [OPTION]…[FILE]…

Example

How to use rm command in linux

The ls shows that originally there are three files. Then we use the rm command to delete a.txt. Finally, the deletion of a,txt is verified by listing the content again using ls command.

You can also delete multiple files at the same time. For example:

$rm a.txt b.txt c.txt
$rm *.txt (to understand what * means here read file globbing)

Options used with rm command in Linux

1. Remove unwritable files without prompt

-f: option removes non-writable files without giving a warning. To understand this option first make a file un-writable i.e., remove write permission from a file

$touch a.txt //create a file 
$chmod -w a.txt //removes the write permission
$ls -l a.txt //you will see no write permission

Now, try to delete this file using rm. You will see a prompt whether you want to remove the file or not. If you press ‘y’ then the file is deleted. Else if you press ‘n’ its not. The point is the system asks you before deleting a un-writable file.

how to use rm command in linux

The use of -f option will delete the file directly without the prompt message. So, again create a file and remove the write permission and now use the command

$rm -f a.txt

This time the file a.txt gets deleted without prompt.

2. Deleting files Interactively

-i: option deletes files in an interactive manner. Interactive means if will ask for confirmation to delete the file which is not the case by default. For example:

using -i option with rm command in linux

The user has to confirm whether he wants to delete the file or not. This option is very useful to prevent deletion of files by mistake.

3. To Delete Directories

-d: option can be used with rm to delete empty directories. It is important to note that directory must be empty i.e., it must not have files and sub-directories. For example:

Deletes the directory DD. This is an alternate to rmdir command which also deletes empty directories.

4. To Delete non-empty Directories

-r / -R: both these options can be used to delete non-empty directories. This is the only way to delete non-empty directories.
Note: rmdir command can not delete non-empty directories.

using -r option with rm command in linux

Practice Questions on How to use rm command in Linux

Q1. Write the command to delete all files starting with “a”.
Ans: $rm a*
Q2. Write the command to delete a file after confirmation from the user.
Ans: $rm -i <filename>
Q3. Consider a directory D which contains files f1 and f2. Write the command to delete the directory D
Ans: $rm -r D
*Q4. Suppose that there is a file “-foo“. How will you delete this file?

PPT on rm command

Video on rm command

Relevant Commands

touch
mkdir
rmdir
ls
grep

1 thought on “How to delete files using rm command in Linux?”

  1. Q4. Suppose that there is a file “-foo“. How will you delete this file?
    Soln. rm -f — -foo (press enter)
    -f is used in case it’s non-writable too.

Leave a Comment