TransWikia.com

Compress several files with different names

Server Fault Asked by SnowdemBR on September 14, 2020

Example:
I have 100 files each 1 with a name like this:
g1001_1_405901_828719213.arc
g1001_1_405902_828719213.arc
g1001_1_405902_828719213.arc

up to 405999

I want to make a tar.gz of each file by changing their name to g1001_1_405901_405901.tar.gz

can you do all of them at once with your new name?

2 Answers

I'm not sure I understand the question...

Are you trying to take files that have a similar name and combine all with a similar name into a single tar.gz file?

For example, in your question, you mention "g1001_1_405901_828719213.arc g1001_1_405902_828719213.arc g1001_1_405902_828719213.arc" and that they go up to "405999".

If this means that you have lots of files that begin with g1001_1_405901 and the last part of the name ("_828719213.arc") is what varies, then you should be able to use a wildcard to match the name up to the point where they deviate.

So, for example, to tar up all files that start with g1001_1_405901, you could use something like:

tar -czvf g1001_1_405901.tar.gz ./g1001_1_405901_*.arc

If that is not your question, and you simply want to compress the files in the directory that match a pattern, then you could use something like:

gzip g1001_1_405901.*.arc

Your question isn't very clear. If neither of these are what you are looking for, perhaps you can edit your question and give more details.

Answered by mikem on September 14, 2020

IIUC, you could use Perl implementation of rename program (there is another program called rename which is a part of util-linux package). Example - first let's re-create a list of files you presented in your question:

$ touch g1001_1_405901_828719213.arc g1001_1_405902_828719213.arc
$ ls -Al
total 0
-rw-r--r-- 1 ja users 0 Sep 11 21:30 g1001_1_405901_828719213.arc
-rw-r--r-- 1 ja users 0 Sep 11 21:30 g1001_1_405902_828719213.arc

run perl-rename and list files again:

$ perl-rename 's,_[0-9]+.arc,.tar.gz,;s,_([0-9]+).tar.gz,_1_1.tar.gz,'  *
$ ls -Al
total 0
-rw-r--r-- 1 ja users 0 Sep 11 21:30 g1001_1_405901_405901.tar.gz
-rw-r--r-- 1 ja users 0 Sep 11 21:30 g1001_1_405902_405902.tar.gz

Answered by Arkadiusz Drabczyk on September 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP