Summary
- The rm and rmdir commands are used to delete files and directories on Linux and other Unix-like systems. They are similar to del and delttee commands in Windows and DOS.
- Files and directories deleted using rm and rmdir are immediately removed from the computer, without being moved to the Trash. To restore accidentally deleted files, a backup must be used.
- The rm command can delete single or multiple files, with options to use wildcards for selecting groups of files. The rmdir command can only delete empty directories, while the rm -r option is used to delete directories and all their contents. Caution is advised when using the rm -rf command, as it can cause data loss or system malfunction.
Therm andrmdir commands delete files and directories on Linux, macOS, and otherUnix-like operating systems. They're similar to thedelanddeltree commands in Windows and DOS. These commands are very powerful and have quite a few options.
It is important to note that files and directories deleted usingrm andrmdir do not get moved to the Trash. They are immediately removed from your computer. If you accidentally delete files using these commands, the only way you'll be able to restore them is from a backup.
How to Delete Files on Linux with rm
The simplest case is deleting a single file in the current directory. Typetherm command, a space, and then the name of the file you want to delete.
rm file_1.txt
If the file is not in the current working directory, provide a path to the file's location.
rm ./path/to/the/file/file_1.txt
You can pass more than one filename torm. Doing so deletes all of the specified files.
rm file_2.txt file_3.txt
Wildcards can be used to select groups of files to be deleted. The* represents multiple characters and the? represents a single character. This command would delete all of the png image files in the current working directory.
rm *.png
This command would delete all files that have a single character extension. For example, this would delete File.1 and File.2, but not File.12.
rm *.?
If a file is write-protected you will be prompted before the file is deleted. You must respond withy orn and press "Enter."

To reduce the risk of usingrm with wildcards use the-i (interactive) option. This requires you to confirm the deletion of each file.
rm -i *.dat

The-f (force) option is the opposite of interactive. It does not prompt for confirmation even if files are write-protected.
rm -f filename
How to Delete Directories on Linux with rm
To remove an empty directory, use the-d (directory) option. You can use wildcards (* and?) in directory names just as you can with filenames.
rm -d directory
Providing more than one directory name deletes all of the specified empty directories.
rm -d directory1 directory2 /path/to/directory3
To delete directories that are not empty, use the-r (recursive) option. To be clear, this removes the directories and all files and sub-directories contained within them.
rm -r directory1 directory2 directory3
If a directory or a file is write-protected, you will be prompted to confirm the deletion. To delete directories that are not empty and to suppress these prompts, use the-r (recursive) and-f (force) options together.
rm -rf directory
Care is required here. Making a mistake with therm -rf command could cause data loss or system malfunction.It's dangerous, and caution is the best policy. To gain an understanding of the directory structure and the files that will be deleted by therm -rf command, use thetree command.
Useapt-get to install this package onto your system if you're using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution's package management tool instead.
sudo apt-get install tree
Running thetree command produces a simple to understand diagram of the directory structure and files beneath the directory from which it is run.
tree

You can also supply a path to thetree command to cause it to start the tree from another directory in the file system.
tree path/to/directory
Therm command also has--one-file-system, --no-preserve-root, --preserve-root options, but those are only recommended for advanced users. If you get something wrong, you could accidentally delete all your system files. Consult the command'smanual page for more information.
How to Delete Directories on Linux with rmdir
There is another command, calledrmdir, that you can use to delete directories. The difference betweenrm andrmdir is thatrmdir can only delete directories that are empty. It will never delete files.
The simplest case is deleting a single empty directory. As withrm, you can pass multiple directory names tormdir , or a path to a directory.
Delete a single directory in the current directory by passing its name tormdir :
rmdir directory
Delete multiple directories by passing a list of names tormdir :
rmdir directory1 directory2 directory3
Delete a directory not in the current directory by specifying the full path to that directory:
rmdir /path/to/directory
If you try to delete a folder that is not empty,rmdir will give you an error message. In the following examplermdir successfully, and silently, deletes theclients directory but it refuses to delete theprojects directory because it contains files. Theprojects directory is left exactly as it was and the files in it are untouched.

Whenrmdir gives a "Directory not empty" error, it stops processing the directories that were passed to it on the command line. If you've asked it to delete four directories and the first one had files in it,rmdir would give you the error message and do nothing more. You can force it to ignore these errors with the--ignore-fail-on-non-empty option so that other directories are processed.
In the following example two folders have been passed tormdir, these arework/reports andwork/quotes . The--ignore-fail-on-non-empty option has been included in the command. Thework/reports folder has files in it, sormdir cannot delete it. The--ignore-fail-on-non-empty option forcesrmdir to ignore the error and move on to the next folder it needs to process, which iswork/quotes. This is an empty folder, andrmdir deletes it.
This was the command used.
rmdir --ignore-fail-on-non-empty work/reports /work/quotes

You can use the-p (parents) option to delete a directory and to delete its parent directories too. This trick works becausermdir starts with the target directory and then back-steps to the parent. That directory should now be empty, so it can be deleted byrmdir, and the process repeats stepping back up the path that was provided tormdir.
In the following example the command that is passed tormdir is:
rmdir -p work/invoices

Both theinvoices and thework directories are deleted, as requested.
Whether you're using Bash or any other shell, Linux provides flexible and powerful commands for you to delete directories and files straight from the terminal command line. Some people prefer to have a workflow that revolves around the terminal. Others may have no choice in the matter. They may be working on servers without a GUI installed or on a remote session onto a headless system such as a Raspberry Pi. These commands are perfect for that group of people.
But whatever type of workflow you prefer, these commands lend themselves very well to being included in shell scripts. If a script is triggered by acron job, it can help automate routine housekeeping tasks such as purging unwanted log files. If you investigate that use case, remember the power of these commands, test everything carefully, and always maintain a recent backup.
Linux Commands | ||
Files | tar·pv·cat·tac·chmod·grep ·diff·sed·ar·man·pushd·popd·fsck·testdisk·seq·fd·pandoc·cd·$PATH·awk·join·jq·fold·uniq·journalctl·tail·stat·ls·fstab·echo·less·chgrp·chown·rev·look·strings·type·rename·zip·unzip·mount·umount·install·fdisk·mkfs·rm·rmdir·rsync·df·gpg·vi·nano·mkdir·du·ln·patch·convert·rclone·shred·srm·scp·gzip·chattr·cut·find·umask·wc·tr | |
Processes | alias·screen·top·nice·renice·progress·strace·systemd·tmux·chsh·history·at·batch·free·which·dmesg·chfn·usermod·ps·chroot·xargs·tty·pinky·lsof·vmstat·timeout·wall·yes·kill·sleep·sudo·su·time·groupadd·usermod·groups·lshw·shutdown·reboot·halt·poweroff·passwd·lscpu·crontab·date·bg·fg·pidof·nohup·pmap | |
Networking | netstat·ping·traceroute·ip·ss·whois·fail2ban·bmon·dig·finger·nmap·ftp·curl·wget·who·whoami·w·iptables·ssh-keygen·ufw·arping·firewalld |










