Professional Documents
Culture Documents
How To Remove Non Empty Directory in Linux - Nixcraft
How To Remove Non Empty Directory in Linux - Nixcraft
nixCraft
Linux / Unix tutorials for new and seasoned sysadmin || developers
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:
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
Trying to remove trip-pictures directory with the rmdir command:
rmdir trip-pictures
Sample outputs:
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
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'
rm -ir foo
Sample outputs:
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 --help
rmdir --help
Conclusion
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
If you liked this page, please support my work on Patreon or with a donation.
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
RSS feed or Weekly email newsletter
UP NEXT
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Processes Management bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
link
Next FAQ: How to find and replace text/IP address with Ansible
Featured Articles