4.2.14. rm
— remove files or directories#
With the rm
command, you can remove (or delete) files
or directories.
The basic format of the command is:
rm source
A safer approach is to add the -iv
options to the
command:
rm -iv source
With -i
(interactive), rm
will require your
confirmation before deleting a file or directory.
-v
(verbose) will print the command’s actions on
the screen.
For convenience, you can add an alias.
Sample files#
To follow the examples below, you will need to:
Copy the following files into your home directory using the cp command:
cp -v /usr/share/dict/words ~ cp -rv /usr/share/doc/bash ~
~
is a shortcut for home directory.Create an empty directory:
mkdir empty-dir
Removing files#
To remove a file, for example, the words
file
copied above, you can use:
rm -iv words
Output:
rm: remove regular file 'words'? y
removed 'words'
Removing directories#
Removing empty directories#
If the directory is empty, you can remove it using the
-d
option:
rm -d empty-dir
Alternatively, you can use the rmdir command:
rmdir empty-dir
Removing directories with content#
If the directory has some content i.e., files or
subdirectories, you will need to add the -r
(recursive)
option.
For example, using the bash
directory copied
above:
rm -ivr bash
This command will ask for your confirmation for deleting every file in the directory and then delete it:
rm: descend into directory 'bash'? y
rm: remove regular file 'bash/RBASH'? y
removed 'bash/RBASH'
...
rm: remove regular file 'bash/README.gz'? y
removed 'bash/README.gz'
rm: remove directory 'bash'? y
removed directory 'bash'
Instead of -i
, you could use the -I
option,
which will only prompt once, when removing directories
recursively:
rm -Ivr bash
Output:
rm: remove 1 argument recursively? y
removed 'bash/RBASH'
...
removed 'bash/README.gz'
removed directory 'bash'
If you are completely sure you do not need the
directory and its contents, you can force its removal
using the -f
option:
rm -vrf bash
rm
will delete the directory without confirmation.
Notes#
Adding an alias for rm
#
Rather than typing rm -iv
, every time you need to use
the command, you can add an alias
for the command in your ~/.bash_aliases
file.
For example:
alias rm='rm -iv'
Now, when you type rm
, you will actually be running
rm -iv
.