You are not logged in.

#1 2009-10-13 07:09:39

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

bash script for removing directories NOT containing file type

i want to have something like this:
check all directories (AND subdirectories) for a certain filetype. If the filetype can NOT be found i want it to delete the the parent.

e.g.:

directory test contains an mp3
directory test2 contains no mp3
directory test3 contains no mp3
directory test3/1/ contains no mp3
directory test3/2/2/  contains no mp3
directory test4/1/2/ contains a mp3

the script should now delete test2 (as it does not contain mp3) and
should delete test3 (since somewhere down in its subfolders there are no mp3s), but
it should NOT delete test1 (has mp3) and
should NOT delete test4 (somewhere in its subfolders is a mp3)

alright.. i have come up with this so far:

find -mindepth 1 -type d |
while read dir; do
    if [ ! -f $dir/*.mp3 ]; then
    rm -fr $dir
    fi
done

this - however - does not work when a mp3 is found somewhere deep in directory structure.

Last edited by Rasi (2009-10-13 07:10:38)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2009-10-13 07:13:16

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,485
Website

Re: bash script for removing directories NOT containing file type

untested...

find -mindepth 1 -type d |
while read dir; do
    if [ $(find $dir -name *.mp3 | wc -l) -eq 0 ]
        rm -fr $dir
    fi
done

Offline

#3 2009-10-13 07:25:39

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: bash script for removing directories NOT containing file type

hmm seems to work.. will test some more... thanks!

Last edited by Rasi (2009-10-13 07:28:14)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

Board footer

Powered by FluxBB