| ddenton |
11-20-2008 05:26 PM |
Shell Scripting Question (mv via find/-exec)
Hello.
I have a shell script whose purpose is to move images from one location to another to save space. It's supposed to find any image directory in a tree of parent directories and move any image folder older than 180 days.
The issue I'm having is that for the first subfolder (coincidentally the oldest) to be moved, somehow only the files in that directory are moved to their target location, leaving loose files that should be in their own folder.
Next is the script:
Code:
#!/bin/sh
# Set blacklist variables...
BL="/usr/local/imagetest/XXXXXX"
for CID in /usr/local/imagetest/??????; do
if [ -d "$CID" ] && [[ `echo $BL | grep -v $CID` ]];
then
cd $CID
date >> /usr/local/imagetest/purgelist.log
CIDDIR=`pwd | awk -F / '{print $4}'`
echo $CIDDIR >> /usr/local/imagetest/purgelist.log
find . -type d -mtime +180 -exec mv {} /usr/local/imagetest/retired_images/$CIDDIR/ \; # >> /usr/local/imagetest/purgelist.log
else
echo "$CID is in the blacklist. Ignore." >> /usr/local/imagetest/purgelist.log
fi
done
echo "Purge process complete for `date +%F`." >> /usr/local/imagetest/purgelist.log
And here's output of the script, and a file listing for the target location:
Code:
find: ./20080423: No such file or directory
find: ./20080501: No such file or directory
find: ./20080505: No such file or directory
find: ./20080506: No such file or directory
find: ./20080507: No such file or directory
find: ./20080508: No such file or directory
find: ./20080509: No such file or directory
find: ./20080512: No such file or directory
find: ./20080513: No such file or directory
find: ./20080514: No such file or directory
find: ./20080515: No such file or directory
find: ./20080520: No such file or directory
And...
Code:
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080501
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080505
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080506
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080507
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080508
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080509
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080512
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080513
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080514
drwxr-xr-x 2 root root 4096 Nov 20 17:11 20080515
drwxr-xr-x 2 root root 8192 Nov 20 17:11 20080520
-rw-r--r-- 1 root root 11840 Apr 23 2008 file1.tif
-rw-r--r-- 1 root root 1430 Apr 23 2008 file2.tif
-rw-r--r-- 1 root root 10608 Apr 23 2008 file3.tif
-rw-r--r-- 1 root root 301 Apr 23 2008 file4.tif
So can anyone tell me why the 20080423 directory wasn't re-created whole during the move? The files should be in their own directory. I'm sure it's some stupid quirk with using move with find -exec, but I'd like a solid answer.
Also, yes, I know the error output can be piped to /dev/null, but in this case it helped. Thanks!
|