I have a lot of files and want to create an archive for every 100 files i.e. the first 100 files are in one tar file, and the 101st to 200th files are in another tar file. And I want to use the date (nanoseconds) as tar names.

I have got something like this:

( cd "${SOURCE_DIR}" && find . -type f -name "${FILE_GLOB}" ) | 
  xargs -n 100 | xargs -I % sh -c '{tar -C "${SOURCE_DIR}" --files-from - % -cf "${DESTINATION_DIR}/$(date '+%Y%m%d%H%M%S%N')-$(uname -n).tar~"}'

Now the problem is, the '%' in date format string will accept argument from xargs, which I do not want.

How can I prevent this from happening?

  • Sorry one more thing. what is the best way to pass SOURCE_DIR? I just found the script above does not work because SOURCE_DIR cannot be interpreted.
    – Ron
    Apr 14, 2014 at 20:54
  • OK I used export command to set the variables as environment variables and it worked.
    – Ron
    Apr 14, 2014 at 21:41

1 Answer 1


Obviously, use another symbol instead of %.

Use for example -I @. So it will be:

( cd "${SOURCE_DIR}" && find . -type f -name "${FILE_GLOB}" ) | 
  xargs -n 100 | xargs -I @ sh -c '{tar -C "${SOURCE_DIR}" --files-from - @ -cf "${DESTINATION_DIR}/$(date '+%Y%m%d%H%M%S%N')-$(uname -n).tar~"}' 
  • Thanks. For some reason my mind was trapped and thought % is the only valid symbol
    – Ron
    Apr 14, 2014 at 19:44
  • @Ron If this answer solved your problem, please remember to mark it as accepted and/or upvote it instead of posting a thank you comment. That is the way thanks are expressed on SE sites.
    – terdon
    Apr 14, 2014 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .