LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell Scripting Question (mv via find/-exec) (http://www.linuxquestions.org/questions/linux-general-1/shell-scripting-question-mv-via-find-exec-684996/)

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!

ddenton 11-21-2008 07:54 AM

Any input would be greatly appreciated...

ddenton 11-21-2008 08:02 AM

I think I've found the solution. Since the first subdir to be copied was the one that wasn't recreated properly, I assumed that subdir's parent directory had not yet finished being created. By inserting a statement before the find command to create the subdir's parent directory using the same CIDDIR variable in the find command, the "-exec mv" created all of the subdirs correctly.

allend 11-21-2008 08:10 AM

Perhaps the 20080423 directory has been modified in the last 180 days?


All times are GMT -5. The time now is 03:59 AM.