0

I want to tar compress every file in the directory, including the files in sub directories into a tar archive without any sub directories. So, that all the files are together in a single archive directory.

7
  • 1
    Have you tried these unix.stackexchange.com/questions/24870/… ? Jun 29, 2018 at 1:05
  • Yes, but that question is asking for just the files in the base directory. I want to recursively get every file in the directory including the files in its sub directories. Jun 29, 2018 at 1:30
  • With GNU tar, you could use --transform to strip the paths - however the resulting archive would still include the (then empty) directories I think Jun 29, 2018 at 1:34
  • Yeah. It seems like I'll have to $ find the files and put them into a temp directory then just tar that directory instead. Jun 29, 2018 at 1:49
  • @PhilipMiller with the find options, you could just remove the -maxdepth 1 to enable recursion.
    – muru
    Jun 29, 2018 at 1:52

1 Answer 1

-1

A solution , without copy using tar in append mode

find /etc -type f | ( 
   CNT=1 ; 
   TARDST="/tmp/a_flat_archive.tar" 
   while read F ; 
   do 
       D=$(dirname $F) ; 
       SF=$(basename $F) ; 
       if [ $CNT -eq 1 ]; 
       then 
           tar -C "$D" -cf $TARDST  "$SF" ;
       else 
           tar -C "$D" --append -f $TARDST "$SF" ;
       fi ; 
       CNT=$(( $CNT +1 )) ;
   done 
)
3
  • Note that --append is a GNU extension to tar. If you're stuck with POSIX tar (the question currently is not tagged specifically enough to tell), the -u update option can be used to update an existing tar file. Jun 30, 2018 at 1:56
  • Both, the append option and -u are dangerous with GNU tar in case that the archive is not created with the same tar implementation or same command. GNU tar does not honor existing archive formats and happily creates archives that change the archive format in the middle. Such archives tend to be unreadable by other cleanly written archivers.
    – schily
    Jun 30, 2018 at 8:07
  • BTW: did you read my example that creates the archive in one pass using star?
    – schily
    Jun 30, 2018 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.