I have a folder that I need to search for new files and copy on the latest. I've been using "-mtime -1" in my command line but it doesn't seem to work.
I've been meaning to fine a different script because files are dropped into the folder all day long and because of the -mtime, I've only be able to look for files once a day.
Does someone have a better way for me to search this folder throughout the day to find and copy only the latest files?
The problem is, when I run this it copies ALL the files in the directory instead of those that are created in 1 day or less. It's crude, cause what I really need is something to look for only NEW files and copy those.
Many versions of the find command have an option called -newer which can be used to find files which are newer than the date of a reference file. See the find(1) man page for further information.
Save the list fist, and then feed that into cpio, otherwise you could get a file that will arrive while you are doing the cpio and you'll never retrieve it.
Porter has a point which I was also wondering about, how can you be sure you won't get a partial? If it is longer term storage it doesn't matter since you will get them next time around, but if the files are being processed then you could end up processing an incomplete file.
Possible but very highly unlikely if the second option above is followed, or an extension of using two touch files on a round robin and touching before find could be used that would definitely not fail.
Either way it is simpler and more efficient than comparing directory listing.
If I understand the problem correctly, you only want to copy new files.
1). Manualy do a list of your directory and direct it to a file.
ex:
ls > filelist.txt
2). In your script, replace the find with this.
ls > filelist_new.txt
**** The below should appear in a loop
diff filelist.txt filelist_new.txt | grep -v 'file2' | grep '>' | awk ' { print $2} '
*****
example:
for FileToCopy in `diff filelist.txt filelist_new.txt | grep -v 'filelist_new.txt' | grep '>' | awk ' { print $2} '`
do
cp $FileToCopy <where ever it goes.
done
cp filelist_new.txt filelist.txt
Every time you run that script, you will only pick up new filers.
;o), I only tested the diff part of this script. But, it should work. This is sh shell scripting and not bash.
If your moving files, after I think about all the posts... This wont work, and I appoligize for the dumb post.