How do I delete a directory in Linux terminal?

I am a new Linux user. How do I delete a directory in Linux terminal?

Introduction: Everything is a file in Linux including the directory. A directory is nothing but a group of files (or data). You can group files as per needs. For example, store all C program files in $HOME/projects/ directory. You can use any one of the following commands to delete a directory in Linux (also known as a folder in the macOS and Microsoft Windows operating system):
  1. rmdir command – Delete the specified empty directories in Linux
  2. rm command – Delete directories even if it is not empty in Linux

Let us see how to delete directories in Linux using the command line.

How to delete a directory in Linux terminal

Say you want to delete a directory named /home/vivek/data/, run:
rmdir /home/vivek/data/
rmdir -v ~/data/

Verify directory deleted from the system with help of ls command:
ls /home/vivek/data/
ls ~/data/

Please note that when attempting to remove a directory using the rmdir command, the directory must be empty. Otherwise, you might see an error message that read as follows on screen when execute rmdir -v /home/vivek/projects/:

rmdir: failed to remove '/home/vivek/projects/': Directory not empty

How do I delete a full directory in Linux?

As I said earlier rmdir command remove the DIRECTORY(ies) if they are empty. But, how do you delete a full directory that has many files and sub-directories? The solutions is to pass the -rf option to the rm command. The syntax is:
rm -r dir1
rm -rf dir2 dir3 /path/to/foo/
rm -rfv /path/to/bar/dir/
rm -rfv /home/vivek/projects/


Where,

  • -r – Delete directories and their contents recursively
  • -f – Forceful option i.e. ignore nonexistent files and arguments, never prompt for anything
  • -v – Be verbose. Show what rmdir or rm command doing with given directory
  • -i – Prompt before every removal of file/dir
  • -I – Confirm (display prompt) once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes. Useful when working on a large number of files on Linux

Conclusion

This page showed how to delete both empty and non-empty directories along with all files/sub-directories using rm and rmdir command in Linux terminal application.


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

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
1 comment… add one
  • Prajakta Jan 21, 2016 @ 13:31

    In Linux, you remove everything in the current directory and below it using
    cd ..
    rm -rf dir

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