Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Somehow I got a copy of every folder inside itself.

What is the shell command I could run to delete all folders that are inside folders of the same name?

screenshot of problem

For example in the above, running the command from the parent of lnp_eventbrite and libraries, delete only the lnp_eventbrite and libraries folders within themselves.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

In tcsh you could do this:

foreach i (*)
   if ( -e "$i/$i" ) rm -rf "$i/$i"
end
share|improve this answer
    
maybe replace the 'rm -rf' with 'rmdir' to avoid deleting whole subdirs even if they contain other things than another subdir with a matching name ? –  Olivier Dulac Dec 20 '12 at 10:00

If you want to delete matching subdirs, you need to be sure 1) it is empty 2) it is a dir with the same name as its parent. And you need to start from the deepest one down (ex: A/A/A/A/A/ : start with the further down one.

Here is a way to do it in bash + grep + find (pretty basic ones, as I test on an old machine) :

recdeldir ()
{
    _tab_="`printf '\011'`";
    find . -type d -depth -print | grep -v '^\.$' | while IFS= read dir; do
        echo "======= $dir =============";
        pushd "${dir}";
        if [ -d "../../`basename ${dir}`" ]; then
            echo "Matching subdir: `pwd`";
            if ( find . -print | grep -v '^\.$' | wc -c | grep "[ ${_tab_}]*0$" >/dev/null ); then
                echo "empty, deleting `pwd`/";
                cd ..;
                rmdir "`basename ${dir}`";
            else
                echo "`pwd`/ is not empty. doing nothing.";
            fi;
        else
            echo "Not matching subdir : `pwd`";
        fi;
        popd;
    done
}

And here is an exemple of use of this function: I created a /tmp/DIR, and inside it I created a bit of a test case (with several dir with same name, some beginning with ".", and in some of them i added other files to ensure those won't be deleted and are taken into account, etc.

In other words, I make sure in that script to NOT delete a subdir matching the parent's dir name if that subdir is not empty, ensuring we don't delete things we shouldn't delete (I believe that's in accord with the asker's wishes). If we should delete regardless of content/emtyness, modifications should be made so that it doesn't care about the "empty/not empty" difference.

In your bash session, just copy/paste this function above to create it in your current shell (or add it to a file you'll source, or to your bashrc) and then you can use it:

I then cd into /tmp/DIR, and launched: recdeldir

bash-2.05b$ find . -ls
 8192    1 drwxrwxr-x  5 user      group          256 Dec 18 12:03 .
 8193    1 drwxrwxr-x  3 user      group          256 Dec 18 11:31 ./tre
 8194    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsd
 8195    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsdq
 8196    1 drwxrwxr-x  2 user      group          256 Dec 18 12:10 ./tre/tre
 8199    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/tre/i
 8200    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.O
 8203    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.O/.O
 8204    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.O/.O/.O
 8205    0 -rw-rw-r--  1 user      group            0 Dec 18 12:11 ./.O/.something
 8201    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.OOO
 8198    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.OOO/.OOO
 8202    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.OOO/.OOO/.OOO
 8197    1 -rw-rw-r--  1 user      group          967 Dec 18 12:11 ./result
bash-2.05b$ recdeldir
======= ./tre/tre =============
/tmp/DIR/tre/tre /tmp/DIR
Matching subdir: /tmp/DIR/tre/tre
/tmp/DIR/tre/tre/ is not empty. doing nothing.
/tmp/DIR
======= ./tre =============
/tmp/DIR/tre /tmp/DIR
Not matching subdir : /tmp/DIR/tre
/tmp/DIR
======= ./.O/.O/.O =============
/tmp/DIR/.O/.O/.O /tmp/DIR
Matching subdir: /tmp/DIR/.O/.O/.O
empty, deleting /tmp/DIR/.O/.O/.O/
/tmp/DIR
======= ./.O/.O =============
/tmp/DIR/.O/.O /tmp/DIR
Matching subdir: /tmp/DIR/.O/.O
empty, deleting /tmp/DIR/.O/.O/
/tmp/DIR
======= ./.O =============
/tmp/DIR/.O /tmp/DIR
Not matching subdir : /tmp/DIR/.O
/tmp/DIR
======= ./.OOO/.OOO/.OOO =============
/tmp/DIR/.OOO/.OOO/.OOO /tmp/DIR
Matching subdir: /tmp/DIR/.OOO/.OOO/.OOO
empty, deleting /tmp/DIR/.OOO/.OOO/.OOO/
/tmp/DIR
======= ./.OOO/.OOO =============
/tmp/DIR/.OOO/.OOO /tmp/DIR
Matching subdir: /tmp/DIR/.OOO/.OOO
empty, deleting /tmp/DIR/.OOO/.OOO/
/tmp/DIR
======= ./.OOO =============
/tmp/DIR/.OOO /tmp/DIR
Not matching subdir : /tmp/DIR/.OOO
/tmp/DIR
bash-2.05b$ find . -ls
 8192    1 drwxrwxr-x  5 user      group          256 Dec 18 12:03 .
 8193    1 drwxrwxr-x  3 user      group          256 Dec 18 11:31 ./tre
 8194    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsd
 8195    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsdq
 8196    1 drwxrwxr-x  2 user      group          256 Dec 18 12:10 ./tre/tre
 8199    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/tre/i
 8200    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.O
 8205    0 -rw-rw-r--  1 user      group            0 Dec 18 12:11 ./.O/.something
 8201    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.OOO
 8197    1 -rw-rw-r--  1 user      group          967 Dec 18 12:11 ./result
bash-2.05b$
share|improve this answer
    
That appears to be a very old copy of bash you're running. Cygwin's at 4.1, stable release is 4.2. –  Nicole Hamilton Dec 18 '12 at 16:50
    
It is, indeed. But it is good, in a way, in making sure I don't use too "fancy" things and stay compatible with more versions of bash. –  Olivier Dulac Dec 18 '12 at 18:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.