javaguy Posted April 6, 2006 Report Share Posted April 6, 2006 I have a bunch of JPGs arranged in directories that describe when the pictures where taken, i.e. there's a directory called "2002", one called "2003" and so on. Under each there are month directories, "2003 June", "2003 July" and so on, and under each of these there are further directories called batch1, batch2 and so forth. Some of the batch directories, however, are empty, and I've been thinking about a script to clean them up. My first step was to write a script to identify the empty directories. Here's my first attempt. #!/bin/sh cd /home/javaguy/Documents/Photos DIRS=$(ls -d 2*) for DIR in $DIRS do echo "Changing to directory $DIR" cd "$DIR" MONTHS=$(ls -d *) for MONTH in $MONTHS do echo "Checking $MONTH..." if [[ -d '$MONTH' ]] then echo "Changing to month directory $MONTH" cd "$MONTH" BATCHES=$(ls -d batch*) for BATCH in $BATCHES do if [[ ! `ls $BATCH` ]] then echo "$BATCH is empty." fi done cd .. fi done cd .. done Here's the problem with this. When it lists all the directories under "2002", it sees directories called "2002 January" and such. I'd like it to check to see if "2002 January" is a directory and, on finding that it is, to cd into it and further check all its subdirectories. But what it does instead is first check to see if "2002" is a directory and then if "January" is a directory (neither exists at that level). How can I make it recognize that "2002 January" is a single directory name? Quote Link to comment Share on other sites More sharing options...
neddie Posted April 6, 2006 Report Share Posted April 6, 2006 Have you tried something like this? find . -type d -empty Quote Link to comment Share on other sites More sharing options...
javaguy Posted April 6, 2006 Author Report Share Posted April 6, 2006 Woo hoo! :) Much simpler. Thanks! Quote Link to comment Share on other sites More sharing options...
iphitus Posted April 6, 2006 Report Share Posted April 6, 2006 Just another tip, try not to use ls for file listings as a users settings may mess up the script. By default, my ls shows detailed information, and this tends to screw up any scripts that use ls. find is much better. James Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.