0

I would like to know how to compress several files according to the pattern using gzip, tar, etc... that is, if I have these files:

server_regTCA.log.2021.02.12
server_regTCA.log.2021.02.13
server_regTCA.log.2021.02.14
server_regTCA.log.2021.02.15
server_regTCA.log.2021.02.16
server_regTCA.log.2021.02.17
server_regTCA.log.2021.02.18

I would like to do something like gzip -9 server_regTCA.log.2021.02.[12-15]

Thanks

0

2 Answers 2

3

You haven't mentioned what shell you're using, and that's what controls expansions from patterns.

If you're using bash you can use a brace expansion:

gzip server_regTCA.log.2021.02.{12..15}

If you're using a simpler shell such as sh that doesn't have brace expansions, you're limited to matching with single character patterns:

gzip server_regTCA.log.2021.02.1[2-5]

In both cases you can prefix the command with echo to see how the pattern is expanded by the shell without actually executing the gzip.

If you're looking to compress "older" files, there may be better ways to do it. For example, this will compress all (uncompressed) files matching the shape of your listed filenames that were last changed over a year (366 days) ago:

find ... -type f -mtime +365 -name 'server_regTCA.log.????.??.??' -exec gzip {} +

(Each ? matches a single character. Compressed files have a .gz suffix that won't be matched by the pattern.)

2

With zsh:

gzip -9 -- *.log.2021.02.<12-15>

Or to gzip files within an arbitrary date range:

start=2021.01.31 end=2022.03.28
gzip -9 -- *.log.*(^e['[[ ${REPLY##*.log.} < $start || ${REPLY##*.log.} > $end ]]'])

(that assumes all *.log.* files have that YYYY-MM-DD-style suffix. If not you can make the matching more explicit with *.log.[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9] for instance).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .