You are on page 1of 1

🐧 🐧

nixCraft
Linux / Unix tutorials for new and seasoned sysadmin || developers

Home Linux Shell Scripting Tutorial RSS Donate Search

How to remove non empty Directory in Linux


Author: Vivek Gite • Last updated: November 22, 2019 • 1 comment

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 empty

2. rm command – Delete directory and all files even if it is NOT empty

Procedure to remove non empty directory in Linux


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
Trying to remove trip-pictures directory with the rmdir command:

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

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

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

Share on Twitter • Facebook • 1 comment... add one ↓

UP NEXT

How to Undo in Vim / Vi text editor

Delete / Remove a Directory Linux Command

How To Set Up PF Firewall on FreeBSD to Protect a Web Server

Linux Delete Folder Using rmdir and rm Command

OpenBSD Command: See PCI Device Information

Linux / UNIX Delete a file

What does rm -rf command do on a Linux or Unix

Category List of Unix and Linux commands

File Management cat

Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04

Network Utilities dig • host • ip • nmap

OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04

Package Manager apk • apt

Processes Management bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time

Searching grep • whereis • which

User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w

WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Vivek Gite • Nov 22, 2019 @ 16:01

Have a question or comment? Post it on the forum topic here.

link

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.

Next FAQ: How to find and replace text/IP address with Ansible

Previous FAQ: How to delete and remove files on Arch Linux

Featured Articles

1 30 Cool Open Source Software I Discovered in 2013

2 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X

3 Top 32 Nmap Command Examples For Linux Sys/Network Admins

4 25 PHP Security Best Practices For Linux Sys Admins

5 30 Linux System Monitoring Tools Every SysAdmin Should Know

6 40 Linux Server Hardening Security Tips

7 Linux: 25 Iptables Netfilter Firewall Examples For New SysAdmins

8 Top 20 OpenSSH Server Best Security Practices

9 Top 25 Nginx Web Server Best Security Practices

10 My 10 UNIX Command Line Mistakes

©2020 nixCraft • Privacy • ToS • Contact/Email • Search • Sponsored by Linode

You might also like