Everything in Linux and Unix systems is considered to be a file. Hence, the need to organize the file system. Organizing the file systems often includes: freeing up disk space, removing unnecessary directories or files, and cleaning up outdated directories.
The rm command is commonly used for deleting directories or files in Linux. it can also remove a directory and its contents recursively when used with options.
In this tutorial, we learn how to remove a directory recursively in Linux.
Step 1. List Directory and its contents
The ls command is often used to view the contents of a directory in Linux. It lists the contents in the files and subdirectories of a specified directory. When no directory is specified, it by default lists the contents of the current directory.
When used with the -ld option, the ls command displays the information of a directory not a file. The -d specifies that the information of a directory should be displayed.
ls -ld /home/ubuntu/mydata/
The -l option specifies that the information of a directory or file should be displayed in the long format. It usually displays information, such as its permission, last modification time, and ownership.
ls -l /home/ubuntu/mydata/
The ls command has different options that allow for its output to be customized and information to be displayed in different ways.
Step 2. Remove Directory Recursively
In Linux, the rm is used to remove a directory and its contents. They are many options that allows for the modification of the behavior of the rm command. The -r and -f command are examples.
The -r option specifies that the rm command should remove the specified directories and its contents - subdirectories and files, recursively. It is advisable to use the rm command with caution especially when it is used with the -r option and double-check the directory path to avoid deleting important files.
rm -r /path/to/DirectotyName
rm -r /home/ubuntu/mydata/
This will remove the directory /home/ubuntu/mydata/ and its subdirectories and files - prompt when encountering errors. Remember rm -r won't prompt if no errors. That means rm -r ask for confirmation when there is no write access to it (read-only directory or files) or in any situations it cant pass the file.
Note: rm command may not prompt when you run the command with sudo as it passes the permissions errors.
The -f option, unlike the -r option is usually used to avoid the confirmation prompt. This means the -f option tells the rm command to force delete, without prompting confirmation when encountering errors.
When both options are used together, that is, the -r and -f options, the specified directory and its contents will keep deleted without considering any errors.
rm -rf /path/to/DirectotyName
rm -rf /home/ubuntu/mydata/
This will remove the directory /home/ubuntu/mydata/ and its subdirectories and files, without prompting for confirmation.
With that note rm -rf always exit status with 0 whereas rm -r exit 0 on success and 1 if failed to delete.
Other options that can be used with the rm command are the -v and -i option. The -v option tells the rm command to display information about the specified directory being removed. The -i option specifies that the rm command should interactively ask for confirmation before removing each files in a specified directory.
rm -riv /path/to/directory
Here's an example of using -v and -i options together:
rm -riv /home/ubuntu/mydata
Step 3. Verify
Verifying that a specified directory has been removed from a Linux system, the ls command is used. If no information about the specified directory is displayed, then, the file has been successfully deleted.
ls -ld /home/ubuntu/mydata/
This confirms that the directory /home/ubuntu/mydata/ has been deleted as no information about it is displayed.
Options Directory Deletion
Consider the following options when you delete directory:
|-r||to remove directories and their contents recursively.|
|-f||to force delete, without prompting for confirmation. This can be useful for scripts and automated processes.|
|-i||to interactively prompt for confirmation before removing each file.|
|-v||to be verbose, that is, to display information about the files being removed.|
Tips to mitigate the risks of rm
- Think twice before hitting the rm commands.
- Avoid using an absolute path, instead cd into the directory and delete the contents.
- Use sudo instead of going directly into the interactive root shell. This has visual logs and also avoids the worst situations of
rm -rf /.
- Do a precheck using echo rm -rf to know what going to happen.
- Make sure you have a backup.
You can use rmdir to delete an empty directory whereas to delete directory with content use rm -r or rm -rf command. Be cautious when using rm command with -r and -f options.