0

I want to start from a root directory and recurse through the tree. I want to delete the directory and file if there is only one file in the directory called .picasa.ini

It seems like the "find" command can do a lot of complex searches, but I don't know if can perform actions based on this type of condition.

I am using a Windows machine, but have access to cygwin which provides many command line tools.

1

I'm not sure it can be done with a single find command, but it can certainly be done with a simple bash script. I've just tested this on Cygwin and it works as intended. You'll want to change ROOT_DIR to be the root of whatever filesystem you want to perform this search on (for example /cygdrive/d/ to search the D drive).

The general idea is this: find all files named .picasa.ini in the tree. For each one, look in its directory to see if there are any other files. If there aren't, remove the directory. (The double quotes around $dir will remove directories that contain spaces.) Otherwise move on.

#!/bin/bash

ROOT_DIR=/

find "$ROOT_DIR" -type f -name '.picasa.ini' | while read file; do
    dir=$(dirname "$file")
    filecount=$(ls -1A "$dir" | wc -l)
    if (( filecount == 1 )); then
            rm -rv "$dir"
    fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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