Remove files (delete/unlink).

      rm [options]... file...


   -f, --force
       Ignore nonexistent files, never prompt.
       Prompt before every removal.
       Prompt once before removing more than three files, or when removing recursively.
       Less intrusive than -i, while still giving protection against most mistakes.

       Prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompt always.

       When removing a hierarchy recursively, skip any directory that is on a file system
       different from that of the corresponding command line argument.

       Do not treat '/' specially.

       Do not remove '/' (default).

   -r, -R, --recursive
       Remove directories and their contents recursively.

   -v, --verbose
       Explain what is being done.

       Display this help and exit.

       Output version information and exit.

To remove a file you must have write permission on the file and the folder where it is stored.
The OWNER of a file does not need rw permissions in order to rm it.

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.

Note that if you use rm to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred.

Expanding variables

If you are expanding a variable always put quotes around the filename in case it contains spaces:

rm -- "$filename"

Also consider the case where the $variable has not been set: rm -rf /$variable is not a good idea.

The rm command accepts the -- option which will cause it to stop processing flag options from that point forward.
Without this a filename variable which happened to contain something like "-rf" will be interpreted as part of the command.

Using -- also allows the removal of file names that begin with a dash -.
rm -- -some_file
Alternatively use an absolute or relative path reference.
rm /home/user/-some_file
rm ./-some_file

Undeletable files

To delete a file with non-printable characters in the name: 'bad file name' Use the shell wildcard "?" for each character

rm bad?file?name

Bulk Deletes

Older file systems such as ext2fs, perform badly for massive bulk deletes. The newer ext3fs doesn’t have this performance problem.
To remove a very large number of files from a directory it can be quicker to rm them one at a time in a loop:

find demo_dir -type f | while read -r; do rm -v "$REPLY"; sleep 0.2; done


Remove demo.txt:

rm demo.txt

Remove demo1.txt and demo2.txt:

rm demo1.txt demo2.txt

Remove all the .txt files in the current directory and prompt before each deletion:

rm -i *.txt

Remove all the files from the current directory apart from text.txt.

rm -f !(test.txt)

“But over all things brooding slept, The quiet sense of something lost” ~ Alfred Tennyson

Related Linux commands

ls - List information about files.
find - Find and optionally Delete files.
rmdir - Remove folder(s).
Equivalent Windows command: DEL - Delete one or more files.

Copyright © 1999-2024 SS64.com
Some rights reserved