I am trying to remove a directory named data in my $HOME. However, I am getting an error that read as ‘rmdir: data/: Directory not empty‘. How do I delete a non-empty directory in bash terminal running on Linux operating system?

Yes, you can use any one of the following command to delete all the files in the directory and remove the directory itself including all files on Linux operating system.

Advertisement

Open the Terminal app and type the following command.

How to delete a non-empty directory in bash shell?

The syntax is (be careful all files will be deleted forever):
rm -rf {dirName}
rm -rf {/path/to/dirName}

For example, the following will deletes all files and sub-directories contained in the movies directory your home directory. Run rm command:
rm -rfv movies
OR
rm -rfv $HOME/movies

If you get an error that read as ‘Permission denied’, try:

In case if you do not have the permission to delete the directory run command as root user using the sudo command:
sudo rm -rfv movies
OR
sudo rm -rfv $HOME/movies
OR
sudo rm -rfv /path/to/data/dir/
Sample session:

Fig.01: How to delete a non-empty directory in Linux bash shell terminal?

Fig.01: How to delete a non-empty directory in Linux bash shell terminal?

Understanding rm command options

  1. -r – Attempt to remove the file hierarchy rooted in each file argument i.e. recursively delete all files and sub-directories.
  2. -f – Force file delete operation.
  3. -v – Be verbose when deleting files, showing them as they are removed.

The rmdir command delete directory (folder) provided it is empty. Use rm command to delete the non-empty directory on Linux.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

1 comment… add one
  • Jouni "rautamiekka" J May 26, 2017 @ 2:16

    Be damn sure you don’t have a Space after the leading slash in a

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.