Make all files ( that are in diffrent folders ) in one file

34 Views Asked by At

I tried to do specific thing using for loops and I need you help.

I have different folders that have different names like GCF_A1 GCF_C4 GCF_R3... and so on.

Every folder there's lots of files that have different names like this GCF444444 GCF00001RR GCF1000FF... and so on.

What I want to do is make all the GCF4444 files (that are in gz format, and in the different folders) in one file.

For example if I have 5 folders in each one there's a file I want to copy, then I want to have, in the end, one file that contains all of the content of the five files in the different folders.

2

There are 2 best solutions below

0
JNevill On

This will probably look something like:

 find <base_path_of_your_zips> -type f -name "*.gz" -exec cat {} \path\to\single\zippedfile.gz \;
2
Adam Katz On

I'm not sure what exactly you're asking, but does this answer it?

find /path/to/parent/dir -name GCF4444 -print0 |xargs -0 cat > GCF4444.gz

This likely doesn't make any sense, but the question isn't terribly clear on what you want. This just blindly concatenates each file whose entire name is GCF4444 in whatever order they're found in. (gzip is robust and individual gzip files can be concatenated together without issue. There's a very small overhead cost to the extra headers, which you could remove by changing the end to |xargs -0 zcat |gzip -9 > GCF4444.gz)

Otherwise, perhaps you want to preserve the hierarchy:

cd /path/to/parent/dir
find . -name GCF4444 -print0 |xargs -0 tar -pcf GCF4444.tar

This will create a GCF4444.tar file at the top level of the parent directory. It will contain each GCF4444 file in the nested directory structure. I didn't use tar -z because you stated the file contents are already gzipped (the resulting file would likely be slightly larger).