I have a list of log files structured like that for example : test.2020-10-17.log I would like to gzip all the files in 2020 between January and September. I manage to list them with find, but I would like to gzip them too.
Here the output of my current command : https://i.stack.imgur.com/AznpY.png
CodePudding user response:
There are at least three straight-forward ways to accomplish this:
- In your example, find is unnecessary. Just use gzip.
for i in $(seq -w 01 10)
do
gzip *.2020-${i}-*.log;
done
If your files are buried in various levels of subdirectory, you will need find
. Then you can use
find
'sexec
capability:
for i in $(seq -w 01 10)
do
find -name "*.2020-${i}-*.log" -exec gzip {} \;
done
xargs
to callgzip
once per batch of files instead of once per file. It also has the ability to run multiple instances in parallel (see-P
flag in its documentation). If you have a large number of files:
for i in $(seq -w 01 10)
do
find -name "*.2020-${i}-*.log" -print0
done | xargs -0 gzip
where find
's -print0
flag uses ASCII 0 (NUL) to delimit printed filenames and xargs
' -0
flag uses ASCII 0 to delimit arguments. That way, filenames with spaces odd characters are handled correctly.
CodePudding user response:
Using awk
awk -F"[.-]" '$3 <= "09" && $2 == "2020" {print cmd| "gzip " $0}' input_file