Reputation: 274
I try to write a script to search specific paths for files older than a given time. Because of argument list length I do this not directly, but write found files to a list. In the second step I take this list and tar those files, the plan is to later read the tar listing and delete the files archived in source directories.
Find specific files and write list:
Solution: added -type f
find /path/to/*/xfiles/* -type f -mtime +8 -print > /tmp/archiv-xfiles.manifest
this is working correct as far as I can see. File list is different with each run, while this will be an effect of using mtime which is working relative I guess. Now I take the list and tar the files listed:
tar -czvPf /tmp/archiv-xfiles.tar.gz --files-from /tmp/archiv-xfiles.manifest
At this Point I'm stuck with an archive containing each file three times? The printed manifest list is correct, each file only appearing once. Am I evil?
While I would go on like that:
tar -tvf /tmp/archiv-xfiles.tar.gz > /tmp/archiv-xfiles-to-delete.manifest
Do I Need an error check here too to be sure of non-corrupted Archive?
diff /tmp/archiv-xfiles.manifest /tmp/archiv-xfiles-to-delete.manifest > /dev/null
v1=$?
if [ $v1 == 0 ] ; then
Now I see two ways to go on, first one. Which one is faster?:
cat /tmp/archiv-xfiles-to-delete.manifest | xargs rm -rf
or second:
while read line
do
rm -f $line
done < /tmp/archiv-xfiles-to-delete.manifest
Do not know which one is better? Guess using tar --remove-files
switch is no good or save idea?
And the rest:
rm -f /tmp/archiv-xfiles.manifest
rm -f /tmp/archiv-xfiles-to-delete.manifest
elif
echo ERROR, check manifest files! | mail -s ScriptError [email protected]
fi
Maybe someone can tell me what going wrong with tarring the list, while any ideas on the whole script are really appreciated. For example, is the tar option "--remove-files" a save and reliable way to achieve the same effect as my way of listing the tar content, diff and delete?
Upvotes: 0
Views: 1247
Reputation: 312
1) As for tarring the list: you are probably getting additional codes because your list have both files and containing dirs. Try adding '-type f' to the find incantation to get only files.
2) As for argument list length, one of the points of using xargs is exactly that it take care of it; you could just do
find ..... -print0 | xargs rm
3) I really fail to see why you would want to generate a list, tar the files and regenerate the same list again from the tar archive.
Upvotes: 1