How to remove a directory in Linux

Removing an obsolete directory in Linux clears out retired releases, extracted archives, caches, or temporary work trees that no longer belong on the system. Because removing a directory deletes the path immediately instead of sending it to a desktop Trash location, the safest workflow starts by confirming the exact target before you run the command.

The standard tool for removing a non-empty directory tree on current Linux systems is rm with the recursive option. rm -r removes the named directory together with the files and subdirectories below it, while -I adds one confirmation prompt before the recursive delete begins and -v prints each removed path so you can see exactly what changed.

These examples use a current Ubuntu environment with GNU rm, but the same command flow applies on most distributions that ship GNU coreutils. If the target directory belongs to root or another privileged account, rerun the same absolute path with sudo only after checking the target again, because the delete takes effect immediately once the confirmation is accepted.

Steps to remove a directory in Linux:

  1. Inspect the target directory first so the absolute path and current contents match what should be deleted.
    $ ls -la /srv/releases/old-build
    total 16
    drwxr-xr-x 3 deploy deploy 4096 Apr 14 04:38 .
    drwxr-xr-x 3 deploy deploy 4096 Apr 14 04:38 ..
    -rw-r--r-- 1 deploy deploy   14 Apr 14 04:38 README.txt
    drwxr-xr-x 2 deploy deploy 4096 Apr 14 04:38 assets

    Checking the target first reduces the chance of deleting a similarly named directory elsewhere in the same parent path.

  2. Remove the directory tree with a single confirmation prompt.
    $ rm -rIv /srv/releases/old-build
    rm: remove 1 argument recursively? y
    removed '/srv/releases/old-build/assets/app.js'
    removed '/srv/releases/old-build/assets/app.css'
    removed directory '/srv/releases/old-build/assets'
    removed '/srv/releases/old-build/README.txt'
    removed directory '/srv/releases/old-build'

    The -I option prompts once for the whole recursive delete. If you see a Permission denied error instead of removal output, stop and rerun the same absolute path with sudo rm -rIv … only after confirming the target again.

  3. Check the parent directory so the removed entry is no longer listed.
    $ ls -la /srv/releases
    total 8
    drwxr-xr-x 2 deploy deploy 4096 Apr 14 04:38 .
    drwxr-xr-x 1 root   root   4096 Apr 14 04:38 ..

    An empty parent listing confirms that old-build no longer exists under /srv/releases.

  4. Confirm the exact path no longer resolves.
    $ ls -ld /srv/releases/old-build
    ls: cannot access '/srv/releases/old-build': No such file or directory

    This final check helps distinguish a successful removal from a case where the directory was renamed or moved somewhere else in the tree.