Removing a Directory In Linux

How do I remove a directory in Linux using the shell prompt?

You can use the following commands:

ADVERTISEMENTS

[a] rmdir command : remove empty directories.

[b] rm command : remove files or directories.

Linux Delete a Directory Command Examples

The syntax is as follows:

rmdir dirName

Login using the ssh client or open a command-line terminal (select Applications > Accessories > Terminal under Gnome desktop), than type the following command to delete empty directory called foo:

rmdir foo

By default, the rm command does not remove directories. Use the –recursive (-r or -R) option to remove each listed directory, too, along with all of its contents including all files and subdirectories:

rm -r documents

You can also specifiy the full path:

rm -r /data/dir1
rm -r /home/vivek/dir2
rmdir $HOME/mp3
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Ramlee Aug 6, 2014 @ 6:56

    Greetings,
    My name is Ramlee and my laptop has the latest Ubuntu Studio distro installed.

    I am trying to remove a folder/directory that I extracted from an archived file incorrectly. The path to the folder is;

    /usr/share/sounds/sf2/GeneralUser GS 1.44 FluidSynth

    That last folder (GeneralUser GS etc.) is the folder I want to delete and it contains a mix of subdirectories and single files.

    I have tried every combination of the rm -r command in a terminal with no success. So far, I have had the following three results;

    1. nothing happens, or
    2. I’m told the directory doesn’t exist, or
    3. I’m told I don’t have the correct permission.

    I have a feeling I’m not typing in my commands correctly. All advice welcome.

    Regards Ramlee.

Leave a Reply

Your email address will not be published.

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