Remove Directory Recursively in Linux

Remove Directory Recursively in Linux

Directory can contain many subdirectories and files inside it. When working with filesystem in Linux, might be need to remove all files and subdirectories contained in a directory. This process can be called recursive deletion. This tutorial explains how to remove directory recursively in Linux.

Create few directories and files for testing:

mkdir -p docs/dir_1 docs/dir_2 img/dir_1
echo "Hello world" > docs/dir_1/test.txt
echo "Hello world" > docs/dir_2/test.txt

To remove directory recursively, use rm command with -r option. To avoid prompting the user to confirm unwritable files deletion, add -f option.

rm -rf docs

To remove multiple directories recursively, specify paths of the directories separated by space as follows:

rm -rf docs img

Leave a Comment

Cancel reply

Your email address will not be published.