1

I'd like to search for all csv files in a few "date" subfolders, and then tar them preserving that structure, since the date is only at the level of folder, not file. So, for example I have

20200101
   A.csv
   B.py
20200102
   G.csv
   D.csv
   F.c

So, I'd like to have a tar with

20200101
   A.csv
20200102
   G.csv
   D.csv

I've tried:

ls 20200*/<some regex>.* | xargs -n1 tar -vczf odlists.tar.gz

But it only saves one file!

3

xargs -n1 means to execute the command for each argument. So tar saves one file at a time, overwriting the previous archive each time.

Even without -n1, the command would be fragile due to the command line length limit. … | xargs tar … makes repeated calls to tar if all the arguments can't fit at once, and each repeated call would overwrite the previous one.

You can create an archive with one file (or more) with tar -c, then append to it repeatedly with tar -r. That's awkward because there's no convenient way to pass different options the first time, and you have to create an uncompressed archive then compress it at the end.

If your tar supports the -T option, you can use it to pass file names on standard input. This works like xargs, but without the limitations of the command line length.

printf '%s\n' 20200*/*.csv | tar -vczf odlists.tar.gz -T -

Alternatively, assuming your tar supports filtering, you can use that.

tar -vczf odlists.tar.gz --exclude='*.' 2020*
| improve this answer | |

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.