Home » Linux » Basic Linux Commands » The “rm” Command in Linux

The “rm” Command in Linux

The “rm” command is used to “remove” or “delete” files in Linux. By typing command “rm filename” will delete the file from your storage.

NOTE: Once deleted the file will not be recoverable, hence us this command with care and unless you know what you are deleting.

We will execute “rm” using the some of the command line supported options to delete some files.

Firstly, we will use “ls -l” to list the files available:

$ ls -l

-rw-r--r-- 1 myuser myuser       19 May 20 17:58  test1.txt
-rw-r--r-- 1 myuser myuser       20 May 20 17:58  test2.txt
-rw-r--r-- 1 myuser myuser       20 May 20 17:59  test3.txt

As you can see above, we have created 3 test files for this purpose.

Silent Delete – If you want to delete the files inside some directory, you can use command “rm filename” , then this command will delete the file silently without asking for user to reconfirm deletion.

$ rm test1.txt

Interactive Delete – This is mostly used from the shell scripts, to make sure which files we are deleting and proceed with delete only when the user confirms the file is correct.

Now using the “rm -i” function we will delete test files 2 and 3 and it will ask for confirmation as shown below, and only when user types “y” it will proceed with deletion.

$ rm -i test2.txt
rm: remove regular file 'test2.txt'? y

Force Delete – If we use “rm -f” we will remove file/directory forcefully, no questions asked. The output does not prompt any confirmations and deletes the file without any warnings.

$ rm -f test1.txt

Delete Directory and its contents

If we have a directory which in itself contains multiple files, then deleting the directory with just name of directory is not sufficient, as we first have to delete the contents of this directory as well. This can be done as below in an interactive way.

$ rm -dir hello/
rm: descend into directory 'hello/'? y
rm: remove regular file 'hello/helloworld.txt'? y
rm: remove directory 'hello/'? y

Here, every time we have to enter y to delete each file…

But what if our directory has thousands of files, we can’t type y for each file.. in that case, the recursive delete helps us.. so we can use “-r” to delete the contents of directory recustively.

$ rm -r hello

We will delete the empty directory named test using the “rm -dir” command. It will ask for confirmation as shown below:

$ rm -dir test
rm: remove directory 'test'? y

Toooo much options… don’t worry, as you get used with rm command, the command normally which works for all filetypes, directories is if we can user “recursive force” delete together..

$ rm -rf test

Running “rm –help” on the terminal gives us the following options:

$ rm --help
Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).

  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively; less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i); without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat '/' specially
      --preserve-root[=all]  do not remove '/' (default);
                              with 'all', reject any command line argument
                              on a separate device from its parent
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done
      --help     display this help and exit
      --version  output version information and exit

By default, rm does not remove directories.  Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.

To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
  rm -- -foo

  rm ./-foo

Note that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time.  For greater
assurance that the contents are truly unrecoverable, consider using shred.

That’s all in this article! If you have any questions and/or suggestion, do write to us or comment below.

Leave a Comment