SLS SLS - 1 year ago 101
Bash Question

Linux find command and copy and rename them same time

Will you be able to help me to write a script, I just want to find log files over 2GB and copy them to archive folder in same directory.I just write a find command it is not working, appreciate if someone could help me.

ex - main log folders - /vsapp/logs/
- app1,app2,app3
there are lot of logs in the app1, app2 and app3 folders.

so i want to find the logs in the logs folder which is over 2GB, and copy them to archive folder with the different name with today's date.

ex - abcd.log -----copy to -----> abcd.log-08-22-2016

My command at the moment which is not working

find $i/* -type f -size +2G -exec cp '{}' $i/$arc/{}-$date

Answer Source

You can do:

find /src -type f -name '*.log' -size +2G -exec cp {} /dest/{}-$(date -I) \;

Additions/Modifications i made:

  • -name '*.log' searches only for log files, as we are only interested in those. You can look for files with any names too if unsure, just omit -name '*.log in that case

  • $(date -I) is command substitution the output will be today's date in format YYYY-mm-dd, you can also define a custom format, check man date

  • End the -exec action of find with \;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download