gunzip multiple gz files with same compressed file name in multiple folders

sojim2

I have a directory where there are multiple folders, each folder contains multiple .gz files with the same zipped file name "spark.log". How can I unzip all of them at once and rename them like the gz file?

My data looks like this

List of folders

A
B
C
D

In every of them there are files as

A
spark.log.gz
spark.log.1.gz
spark.log.2.gz
spark.log.3.gz
B
spark.log.gz
spark.log.1.gz
spark.log.2.gz
spark.log.3.gz
C
spark.log.gz
spark.log.1.gz
spark.log.2.gz
spark.log.3.gz
D
spark.log.gz
spark.log.1.gz
spark.log.2.gz
spark.log.3.gz

in each of the gz file contains spark.log, I'd like to be able to unzip and rename them according to their gz name. For example: spark.log.1.gz -> spark.log.1.log

Stéphane Chazelas

While gzip does or can store the original name, which you can reveal by running gzip -Nl file.gz:

$ gzip spark.log
$ mv spark.log.gz spark.log.1.gz
$ gzip -l spark.log.1.gz
         compressed        uncompressed  ratio uncompressed_name
                170                 292  51.4% spark.log.1
$ gzip -lN spark.log.1.gz
         compressed        uncompressed  ratio uncompressed_name
                170                 292  51.4% spark.log

gunzip will not use that for the name of the uncompressed file unless you pass the -N option and will just use the name of the gzipped file with the .gz suffix removed.

You may be confusing it with Info-ZIP's zip command and its related zip format which is a compressed archive format while gzip is just a compressor like compress, bzip2, xz...

So you just need to call gunzip without -N on those files:

gunzip -- */spark.log*.gz

And you'll get spark.log, spark.log.1, spark.log.2... (not spark.log.1.log which wouldn't make sense, nor spark.1.log, which could be interpreted as a log file for a spark.1 service as opposed to the most recent rotation of spark.log).

Having said that, there's hardly ever any reason to want to uncompress log files. Accessing the contents is generally quicker when they are compressed. Modifying the contents is potentially more expensive, but you generally don't modify log files after they've been archived / rotated. You can use zgrep, vim, zless (even less if configured to do so) to inspect their contents. zcat -f ./*.log*(nOn) | grep... if using zsh to send all the logs from older to newer to grep, etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reading in multiple files compressed in tar.gz archive into Spark

Find multiple files' paths with the same file name

Delete the same files inside multiple folders (python)

Shell script to modify variable with file name of multiple files in same directory

Copy file from multiple folder to a new folder - files are with the same name

Move multiple files with the same name into new folders named with the file paths

Moving multiple files from a single folder to multiple folders according to their name

How to gzip multiple files into one gz file?

extracting multiple .gz files, and printing the name of the extracted file

Is it possible to gunzip multiple files to a specific directory without deleting the original gz files?

Give same access to folders/files for multiple users

Find author name in multiple files and folders in python

Adding date to the name of multiple files in multiple folders

gunzip multiple files

Open multiple files in folders with specific name pattern

Saving multiple files with same name

mv files with multiple `.` in the file name

how to gunzip multiple files at once to a specific folder?

Move multiple files by the date in file name into date named folders

Bash: how to copy multiple files with same name to multiple folders

Change name of multiple files/folders under a directory

load multiple files from different folders with same file name in bash-scipt

gunzip all .gz files in directory

Search for text in files of same name in multiple folders of various depths

How to decompress .gz file containing multiple files

Copy same file to multiple different folders PowerShell

How to split big file(12gb) into multiple 1GB compressed(.gz) archives? C#

Renaming multiple files with same name

Copy Files from a folder to multiple folders based on the file name in Azure Data Factory