This command is used to remove files in a directory or the directory itself. A directory cannot be removed if it is not empty.
$ rm file1
$ rm -r myproject
To delete multiple files at once, use the rm
command followed by the file names separated by space.
rm filename1 filename2 filename3
You can also use a wildcard (*
) and regular expansions to match multiple files. For example, to remove all .pdf
files in the current directory, use the following command
rm *.pdf
Use the rm
with the -i
option to confirm each file before deleting it
rm -i filename(s)
To remove files without prompting, even if the files are write-protected, pass the -f
(force) option to the rm
command
rm -f filename(s)
You can also combine rm
options. For example, to remove all .txt
files in the current directory without a prompt in verbose mode, use the following command You can also combine rm
options. For example, to remove all .txt
files in the current directory without a prompt in verbose mode, use the following command
rm -fv *.txt
To remove an empty directory, use either rmdir
or rm -d
followed by the directory name
rm -d dirname
rmdir dirname
To remove non-empty directories and all the files within them, use the rm
command with the-r
(recursive) option
rm -r dirname
To remove non-empty directories and all the files without being prompted, use rm
with the -r
(recursive) and -f
options
rm -rf dirname
To remove multiple directories at once, use the rm -r
command followed by the directory names separated by space
rm -r dirname1 dirname2 dirname3