Skip to content
Advertisement

Tar search results in .sh file

I have to tar a list of files, without path, that is a result of a find via sh (for crontab use).

In ubuntu’s shell each command works fine but in .sh not.

I tried with :

#!/bin/sh
tar -zcvf  /destination/one-$(date +"%Y%m%d").tgz < find /myfolder/ -iname 'one*' -printf '%fn'

And also with

#!/bin/sh
find /myfolder/ -iname 'one*' -print0 | tar -czvf /destination/one-$(date +"%Y%m%d").tar.gz --null -T - 

But both failed. May someone help? Alternatives ?

Additional scenario info:

  • /myfolder/ contains:
    • one1.log
    • one2.log
    • one3.log
    • two1.log
    • two2.log
      I want one.tgz containing one1.log, one2.log, one3.log

Advertisement

Answer

I think you are looking to pass the filenames to tar on stdin :

find . -name *.png -print0 | tar -cv --null -T- -f tarball.tar

In my case:

find /myfolder/ -iname "one*" -print0 | tar -czv --null -T- -f /destination/one-$(date +"%Y%m%d").tar.gz
Advertisement