Monday, July 02, 2012

Pipe a tar command after find command

For example, we want to pack all pdf file from a folder tree :

neo@sd2bsp:~$find 15r6 -type f -name \*.pdf -print0 | tar --null -cvjf pdfpack.tar.bz2 -T -

man find :

       -print0
              True; print the full file name on the standard  output,  followed  by  a  null  character
              (instead of the newline character that -print uses).  This allows file names that contain
              newlines or other types of white space to  be  correctly  interpreted  by  programs  that
              process the find output.

man tar :
       --null --files-from reads null-terminated names, disable --directory

       -T, --files-from F
     get names to extract or create from file F

More over, move all pdf files from one folder to another :

neo@sd2bsp:/tmp/all_pdf$ find ~/vendor/15r6 -type f -name \*.pdf -print0 | tar --null -cvf - -T - | tar x


Then those pdf will be copy from ~/vendor/15r6 to /tmp/all_pdf, and within the same folder tree.



No comments: