Issue in Find and mv command

Hi

I am using the below code to find mv the files.
Files are moving to the Target location as expected but find is displaying some errors like below.

find ./ -name "Archive*" -mtime +300 -exec mv {} /mnt/X/ARC/ \;
find: `./Archive_09-30-12': No such file or directory
find: `./Archive_10-07-12': No such file or directory
find: `./Archive_11-11-12': No such file or directory
find: `./Archive_10-28-12': No such file or directory
find: `./Archive_11-18-12': No such file or directory
find: `./Archive_10-01-12': No such file or directory
find: `./Archive_10-21-12': No such file or directory
find: `./Archive_10-14-12': No such file or directory
find: `./Archive_11-04-12': No such file or directory

Any help is appreciated

Thanks

Try without '/'

find . -name "Archive*" -mtime +300 -exec mv {} /mnt/X/ARC/ \;

I think it has something to do with the mv command and timing.
Check the destination folder, all these files might have got moved to that folder.

--ahamed

Thanks mathbalaji and Ahamed

@Mathbalaji - same error

@Ahamed - i created a new location in /tmp - same error.
I am not getting it if i use mv -f or cp, any idea why mv is throwing such error even though its moving the file to the destination

 find . -name "Archive_*" -mtime +180 -exec mv {} /tmp/test \;
find: `./Archive_03-17-13': No such file or directory
find: `./Archive_03-10-13': No such file or directory

mv is not throwing the error. May be someone else can explain it - it has something to do with the stat in find command.

Not sure what difference it will make but try this

find . -name "Archive_*" -mtime +180 | xargs -I% mv % /tmp/test

--ahamed

1 Like

Thank you .. it worked..

Any idea why does it happened with exec ?

find's use of stat is not a problem; stat is called before any -exec can mv a pathname, since mtime must be inspected prior to making the decision to mv.

The issue is that these "Archive_*" files are directories. By default, find visits the directory itself first and then its contents. The error messages are the result of find trying to read the contents of a directory just after -exec mv ... \; has moved it.

The reason that piping into xargs works is buffering delaying xargs' execution of mv long enough for find to descend into the directory. I would not depend on that behavior.

A more robust solution is to use -prune to instruct find not to descend into directories that have been moved. Or, you can use -depth. Or, lastly, you can just ignore the harmless messages.

-prune is probably what you want, since it doesn't modify the traversal order that is currently giving you the desired result. Using -depth traverses the file hierarchy in a different order and could yield unexpected results if there are nested matching filenames.

 find . -name 'Archive_*' -mtime +180 -prune -exec mv {} /tmp/test \;

Regards,
Alister

1 Like