Linux Remove Directory and its Contents Recursively

Last updated: February 5, 2023

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.

For example:

ls -ld /home/ubuntu/mydata/
list information only about the directory

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.

For example:

ls -l  /home/ubuntu/mydata/
list contents inside the directory

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.

Syntax:

rm -r /path/to/DirectotyName

Example:

rm -r /home/ubuntu/mydata/
recursively delete directory

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.

Syntax:

rm -rf /path/to/DirectotyName

Example:

rm -rf /home/ubuntu/mydata/
force delete directory

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.

Syntax:

rm -riv /path/to/directory

Here's an example of using -v and -i options together:

rm -riv /home/ubuntu/mydata
rm options i and v with -r

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.

For example:

ls /home/ubuntu/mydata/
or
ls -ld /home/ubuntu/mydata/
confirm directory deleted completely

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:

OptionsDescription
-rto remove directories and their contents recursively.
-fto force delete, without prompting for confirmation. This can be useful for scripts and automated processes.
-ito interactively prompt for confirmation before removing each file.
-vto 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.

Conclusion

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.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment