I have a directory that consists of many subdirectories, and each subdirectory contains different files such as:
Directory All
contains subdirectories A, B, C, and D
. Each subdirectory contains files such as:
A (Run1.csv, Run4.csv)
B (Run2.csv, Run3.csv)
C (Run1.csv, Run3.csv)
D (Run2.csv, Run4.csv)
As you can see, each file has different duplicates in different subdirectories. For example, Run1.csv
in folder A
has different data from Run1.csv
in folder C
.
What I want to do is that I want to zip a specific run file, for example, I want to zip all the files of run2. I used the following commands:
zip run2.zip All Run2.csv
zip run2.zip Run2.csv
But none of them works.
How can I fix that?
Best Answer
You can use
bash
Pathname Expansion as follows:*/Run2.csv
matches every file calledRun2.csv
in any subdirectory. If you have very many files that match the glob, this fails because of the shell’sARG_MAX
limit. To work around that, use:This uses the builtin
printf
to build up a zero-delimited list of the matching files and pipes it toxargs
, which callszip
as often as necessary. Asadd
iszip
’s default mode, it updates the archive adding the files to it.If you need to dig further into an unknown or changing number of subdirectories, set
bash
’sglobstar
option withand use:
**/Run2.csv
matches every file calledRun2.csv
in any subdirectory recursively.Further reading
man zip
/PATTERN MATCHINGman bash
/EXPANSION/Pathname Expansion