Remove files (delete/unlink)
Syntax rm [options]... file... Options -f, --force ignore nonexistent files, 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 Do not remove '/' (default) -r, -R, --recursive Remove directories and their contents recursively -v, --verbose Explain what is being done --help Display this help and exit --version 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.
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.
To delete a file with non-printable characters in the name: ‘bad file name’ Use the shell wildcard “?” for each character
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 my_dir -type f | while read -r; do rm -v “$REPLY”; sleep 0.2; done
Related linux commands: