I'm trying to rename log files and make tar them as 'tar.gz', I ended with below command but it doesn't get the renamed file name from STDIN (output of mv command) and it causes the tar command to create empty files,

find -type f -name "logs*" -exec bash -c ' \
     mv "$0" "$0_$(date -r "$0" +"%Y%m%d%H%M%S")"| \
     xargs tar -czf "$0_$(date -r "$0" +"%Y%m%d%H%M%S")".tar.gz --null -T - 
' "{}" \;

How can I tar the renamed file with revised of above command or is there other ways (in bash shell)? thanks

  • Do you want a separate .tar.gz file for each input file?
    – Arronical
    Oct 14, 2016 at 11:10

1 Answer 1


If you want to create a separate .tar.gz file for each of the log files that find collects, you don't need to use xargs. You can save the dated appended name of each file to a variable (f in this case) and reuse the new name in the tar command. This will also leave your renamed files in place.

find . -type f -iname "log*" -exec bash -c ' \
    f="${0}_$(date -r "$0" +"%Y%m%d%H%M%S")"; \
    mv "$0" "$f" && tar -czf "${f}.tar.gz" "$f" \
' "{}" \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.