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
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.
Comments