RM: Delete Files and Directories in Linux
How to use the rm command to delete files and directories in Linux.
By. Jacob
Edited: 2022-06-03 08:38
The rm command can be used to delete files and directories in Linux.
To delete a file or folder, one would simply use rm in the following manner:
rm PATH-TO-FILE-OR-DIRECTORY
Depending on permissions, you may need to write sudo in front of rm.
sudo rm /var/www/mysite/index.html
You can also navigate to the directory before deleting the file using the cd command.
cd /var/www/mysite
sudo rm /var/www/mysite/index.html
Or, to delete all the files in the current directory:
rm *
This works because rm does not delete directories by default, but it may generate a harmless error, which is unwanted in bash scripts; to avoid the error telling you that it can not remove a directory, simply redirect stderr to /dev/null. E.g:
rm * 2> /dev/null
When doing this, you should first make sure you also have the right permissions, otherwise you won't be informed in case of errors. Actually, you would normally avoid redirecting errors this way, since, if making a script, it may fail for other reasons too without you knowing.
To move back to your home directory, do something like this:
cd /home/MyUser
If you want to exclude specific files or directories, this method can be used:
cd /path/to/directory
rm -Rv !("[file or directory]"|"[file or directory]")
Deleting a directory and all its contents
A common problem on Linux is how to delete a directory, including all its contents, since the "rm" command does not work as many intuitively expect.
To recursively delete an entire directory, including the content, we can use the following:
rm -r directory
To avoid getting prompted for every file, we can use the -f flag:
rm -rf directory
The rm command is useful when writing scripts and managing a computer or server remotely, mostly since you typically do not have access to a GUI in those cases. But, of course it is also down to personal preference; some users find it faster and/or easier to use the terminal, while typical desktop users may find it difficult to remember system commands.
Excluding files and directories
To remove all files except a small list of files that you want to keep, use globbing; you should first make sure your shell supports the syntax. E.g.:
shopt | grep "extglob"
Output:
extglob on
This tells you that extglob is enabled in your bash environment.
Then, to perform the delete and exclude specific files, do like this:
cd /path/to/directory
rm -Rv !("[file or directory]"|"[file or directory]")
Obviously replace [file or directory] with the item you want to remove in the directory. If you want to leave out more than one item, you can simply separate them with pipe (|) character.
Note. On mac, | may be accessible by pressing option + i.
Here is an alternative method, which does not require extglob:
ls --hide=[file or directory] | xargs -d '\n' rm
Note:
xargs will read and execute items from standard input.
You can use multiple --hide options if you have multiple files or directories that you want to ignore.
Making a list of items to ignore
In bash scripting, if you have a lot of directories of files to ignore, it may be useful to allow creating a list where each item is separated by a space and/or newline \n).
To loop through the files and/or directories on the list, and create the ls command dynamically, you can use a for loop:
path_to_dir="[path to dir]"
# If you were to run ls on the root "/" (just as an example)
ignore_list=$(cat <<'IGNORE_LIST_BLOCK'
home
opt
root
var
etc
IGNORE_LIST_BLOCK
)
ls_command="ls $path_to_dir"
for item in $ignore_list; do
ls_command+=" --hide=$item"
done
# Uncomment to see what is being removed
# echo $($ls_command)
# Uncomment to remove the ls items, excluding those on the list
# cd $path_to_dir
# $($ls_command) | xargs -d '\n' rm
Using the terminal vs your file manager
Alternatively you may also delete files and folders with your favorite file manager. Nemo makes it easy, as it has the option to "Open as Root" when right clicking on a directory. But you could also launch Nautilus as root.
Many Linux users prefer to use the terminal, and resist using GUIs. However, any modern OS should be expected to offer GUIs, both for productivity, and to make things easier for their users. When using the terminal, users often have to learn and memorize complex commands – something we can not expect users to do.
Rm Options
Option | What is this? |
---|---|
-v or --verbose | Show what is being done. I.e. Prints the filename, before removing the file. |
-f or --force | Ignore nonexistent files and doesn't prompt the user. |
-i | Prompt the user before removing each file. |
-I | Prompt once whether to proceed with the command, if more than three files are named or if a recursive removal is requested. |
--interactive=once | Prompt once whether to proceed with the command, if more than three files are named or if a recursive removal is requested. |
--interactive=never | Show no Prompts to the user at all. |
--interactive=always | Prompt for every file being removed. |
--interactive | Equivalent to --interactive=always |
Tell us what you think: