rmdir delete files and directories under Linux, Mac OS and other Unix-like operating systems. They are similar to the
deltree commands in Windows and DOS. These commands are very powerful and have many options.
It is important to know that files and directories deleted with
rmdir will not be moved to the Trash. They are immediately removed from your computer. If you accidentally delete files with these commands, you can restore them only through a backup.
Removing Files with rm
The simplest case is deleting a single file in the current directory. Enter the command
rm a space and then the name of the file to be deleted.
If the file is not in the current working directory, specify a path to
rmto hand over. This deletes all 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 deletes all PNG image files in the current working directory.rm * .png
This command deletes 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 read-only, you will be prompted to delete the file. You must answer
nand press Enter.
To reduce the risk of using
rmuse wildcard option
-i(interactive). To do this, you must confirm the deletion of each file.rm -i * .dat
-f(force) is the opposite of interactive. You will not be asked for confirmation, even if files are read-only.rm -f filename
Removing directories with rm
-d-d (directory) to remove an empty directory. You can use wildcard characters (
?) as well as filenames in directory names.rm -d directory
If you specify more than one directory name, all names are deleted from the specified empty directories.rm -d directory1 directory2 / path / directory / directory3
To delete non-empty directories, use the option
-r(recursive). This means that the directories and all files and subdirectories they contain are removed.rm -r directory1 directory2 directory3
If a directory or file is read-only, you are prompted to confirm the deletion. To delete non-empty directories and suppress these prompts, use the options
-f(force) together.rm -rf  Here caution is required. Failure to use the
rm -rfcommand may result in data loss or system failure. It is dangerous and caution is the best policy. To understand the directory structure and the files that are deleted by the command
rm -rfuse the command
.to install this package on your system if you are using Ubuntu or any other Debian-based distribution. Instead, use the package management tool of your Linux distribution on other Linux distributions.sudo apt-get install tree
When you run the
treean easy-to-understand diagram of the directory structure and underlying files is created from where it is executed.Tree
You can also specify a path to the
treecommand to start the tree from another directory in the file system.tree path / to / directory
- single-file system, --no-preserve-root, --preserve-rootoptions, this however, they are only recommended for advanced users. If you detect an error, you may accidentally delete all system files. For more information, see the manual page of the command.
Removing directories with rmdir
There is another command named
rmdirthat you can use to delete directories. The difference between
rmdircan only delete empty directories. It will never delete files.
The simplest case is deleting a single empty directory. As with
rmyou can pass multiple directory names to
rmdiror a path to a directory.
Delete a single directory in the current directory by passing its name to
Delete multiple directories by adding a list of names
rmdir:rmdir directory1 directory3
delete Provide the full path to this directory:rmdir / path / to / directory
If you try to delete a folder that is not empty is, you get an error message with
rmdir. In the following example
clientdirectory successfully and in the background, but does not delete it because it contains files and the
projectsdirectory stays exactly as it was and the files it contains are not affected.
rmdirreturns the error "Directory not empty", processing the directories that were submitted to the command line becomes canceled. If you asked him to delete four directories and the first directory had files,
rmdirwould display the error message and do nothing else. You can force it to ignore these errors with the option
- ignore-fail-on-non-emptyto handle other directories.
In the following example, two folders were added to [19459009übergeben] rmdir these are
work / reportsand
work / quotes. The option
- ignore-fail-on-non-emptywas added to the command. Folder
work / reportscontains files so
rmdircan not delete it. The option
rmdirto ignore the error and move to the next folder it must process, which is
. Quotes. This is an empty folder, and
This was the command used.rmdir --ignore-fail-on-not-empty work / reports / work / citations 
With the option
-p(Parents) you can delete a directory and delete its parent directories as well. This trick works because
rmdirstarts with the target directory and then jumps back to the parent. This directory should now be empty so that it can be deleted from
rmdirand the process repeats the path specified for
rmdirin the following example command issued to [19459010rmdir is:rmdir -p work / bills
workDirectories are deleted as desired.
Regardless of whether you use Bash or another shell, Linux provides flexible and powerful commands that allow you to delete directories and files directly from the terminal's command line. Some people prefer a workflow that revolves around the terminal. Others can not have a choice in the matter. You may work on servers without a GUI installed or in a remote session on a headless system such as a Raspberry Pi. These commands are perfect for this group of people.
Regardless of the type of workflow you prefer, these commands work very well for inclusion in shell scripts. When a script is triggered by a
cronjob, it can help automate routine administrative tasks such as deleting unwanted log files. When investigating this use case, think about the power of these commands, test everything carefully, and always keep a current backup.