0

I have multiple directories like below and I want to compress each file in place.

/var/dir1/logs/logfile.log
/var/dir2/logs/logfile.log
/var/dir3/logs/logfile.log

I want to use find and tar with combine but I don't know how to use the file name and path as variables.

This command zips all files in the one file:

find /dir/to/search/ -name "*.log" -exec tar -rvf out.tar {} \;

1 Answer 1

0

Try using xargs.

find *.log | xargs -I input zip input.zip input

Note that this is the Mac version of xargs. The behaviour might be different on Ubuntu, but hopefully something close to this should work.

xargs works by taking each line of the input (via stdin) and running some command for it. The -I input means that for each line, the input line is used instead of the string input. So in this case, it runs the zip command on each file.

1
  • 1
    Thank you @Simon Lundberg. xargs worked! Commented Jan 24, 2023 at 20:47

You must log in to answer this question.

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