How to remove non empty Directory in Linux

I am trying to delete a directory in Linux using the rmdir command. However, I am getting an error message that read as follows:
rmdir: failed to remove 'trip-pictures': Directory not empty
How can I remove non empty directory in Linux using the cli?

There are two commands that one can use to delete non empty directories in Linux operating system:

  1. rmdir command – Delete directory only if it is empty.
  2. rm command – Remove directory and all files even if it is NOT empty by passing the -r to the rm to remove a directory that is not empty.

The following commands works with CentOS, RHEL, Fedora, Alpine, Arch, Debian, Ubuntu and all other Linux distros. Let us see some examples.

Procedure to remove non empty directory in Linux

We use the rm command to delete a directory that is not empty. The syntax is:
rm -rf dir-name
rm -rf /path/to/dir/name

Be careful when you use the rm command with -r and -f options. The -r option remove directories and their contents recursively including all files. The -f option to rm command ignore nonexistent files and arguments, never prompt for anything. There is no undo option. So you have to be very careful with rm -rf command. Let us see some examples.

Examples for removing non empty directory under Linux

Trying to remove trip-pictures directory with the rmdir command in Linx:
rmdir trip-pictures
Sample outputs:

rmdir: failed to remove 'trip-picture/': Directory not empty

To see files inside the directory use ls command ls -l trip-pictures
ls trip-pictures

To delete all files inside trip-pictures including folder itself run the following rm command:
rm -rf trip-pictures
Remove non empty directory Linux command

How to get visual confirmation about deleting directory

Pass the -v to the rm command:
rm -vrf dir1
rm -vrf dir1 dir2

Sample outputs:

removed 'dir1/resume.txt'
removed 'dir1/bar.txt'
removed 'dir1/foo.txt'
removed directory 'dir1'
removed directory 'dir2/pictures'
removed directory 'dir2'

How to get confirmation prompt before every removal of a dir

You need to pass the -i option to the rm command:
rm -ir foo
Sample outputs:

rm: descend into directory 'foo/'? y
rm: remove regular empty file 'foo/bash.tar.gz'? y
rm: remove regular empty file 'foo/db.sql'? y
rm: remove directory 'foo/'? y

To get prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes pass the -I option:
rm -Ir bar
Sample outputs:

rm: remove 1 argument recursively? y

Want to get info on all rm and rmdir switches? Try:
rm --help
rmdir --help


You learned how to remove non empty directory under Linux or Unix-like operating systems using command line options. For more information see rm command and rmdir command command man pages by typing the following man command command:
man rm
man rmdir

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Trisha Jan 10, 2021 @ 20:48

    In Linux, how do you remove a directory? To delete a folder that is not empty, use the rm -r command for recursive deletion. Be very careful with this command, because using the rm -r command will delete not only everything in the named folder (directory), but also everything in its folders. If you don’t use this command very carefully, you risk deleting all of your files. Never run the following:
    sudo rm -rf /
    sudo rm -rf /etc/

  • Stealla Mar 2, 2021 @ 9:27

    I wanted to how to remove non empty directory in macOS on my development system. you helped me a lot. so this message is confirmation that commands works on macOS unix system too.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum