Results 1 to 4 of 4

Thread: how to delete directory inside another directory

  1. #1
    Join Date
    Sep 2013
    Beans
    18

    how to delete directory inside another directory

    Hi

    I had created directory using the command mkdir -p a/{b,c,d/e/f,g}, and my questions is how can i delete the directory 'b,c,d and g' only with the single command.

    i already try this command

    rmdir -p a/{b,c,g}
    rm -f a/{b,c,g}

    with no luck

    Thanks

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: how to delete directory inside another directory

    You can remove a directory tree with
    Code:
    rm -r directory-name
    (but be careful, because it is not going to any trashcan, it is really deleted).

  3. #3
    Join Date
    Aug 2011
    Location
    52.5° N 6.4° E
    Beans
    6,824
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: how to delete directory inside another directory

    Use rm -r a/{b,c,d,g}, which will recursively remove a/d/e/f and a/d/e. If you haven't made other directories in a/, you can also use rm -r a/*

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: how to delete directory inside another directory

    If the directories are empty, then you can use rmdir - but without the -p, because the parent won't be empty if you are not removing all of the subdirs

    Code:
    $ mkdir -p a/{b..g}
    $ ls a
    b  c  d  e  f  g
    $ 
    $ rmdir a/{b,c,g}
    $ ls a
    d  e  f
    If the subdirs are non-empty, then you will need to use rm -r as sudodus says

    Code:
    $ mkdir -p a/{b..g}
    $ touch -p a/{b..g}/afile
    $ rmdir a/{b,c,g}
    rmdir: failed to remove `a/b': Directory not empty
    rmdir: failed to remove `a/c': Directory not empty
    rmdir: failed to remove `a/g': Directory not empty
    $ 
    $ rm -r a/{b,c,g}
    $ ls a
    d  e  f

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •