Add

Thursday, 17 October 2013

Linux Tips - find, exec, and tar in archive

Linux Tips - 1

Using find, exec, and tar in archive


Yesterday, I was working on finding the files which are off previous week and archive it into single file.



# find . -type f -name "some*.ext*" -mtime +7 –mtime -14 -exec tar cvf myfile.tar



What was happening was that as exec was carrying the input to the tar command, tar kept re-writing the archive. That is, each time exec passed a new block of input files to tar, tar perceived it as a new command, and went on to re-create the file named myfile.tar. So, instead of the huge myfile.tar that I expected, I ended up with last file of find in the archive.


I have to check for append options and this problem was easily remedied by using 'r' switch/command with tar instead of the 'c' switch/command. The 'r' switch tells tar to append to the archive, while 'c' says "create".


All that being said, this command worked just fine for me to create a very large tar archive:



# find . -type f -name "some*.ext*" -mtime +7 –mtime -14 -exec tar rvf myfile.tar



This combination of find, tar, and exec worked like a champ for me.!!

No comments:

Post a Comment