files will generate every 5th minute in /kit directory.Want to tar all the files and move that files to /kit/bkp directory every 2 hr

  • You dialed the wrong number; this is not the scripting service. But we are glad to help if you edit the question to show your attempt for that script. We can point out what went wrong and suggest what to change. Thank you. – Philippos Aug 12 '19 at 8:07
find your_path -t f -mmin 5 -exec tar -zcvf your_compress_file.tar.gz {} +

You can make an script and put your script into crontab.

| improve this answer | |

Write a script and set it to run with cron jobs or systemd timers. Include the appropriate tar command in the script and use the exclude flag for tar to avoid taring the /tar/bkp folder. And use find with the -mmin 120 flag to limit the files tarred to files modified in the last 120 minutes.

For example:

find "${source}" -mmin 120 -exec \
  tar cpf "${dest}/$(date +'%Y%m%d%H').tar" --exclude="${dest}" {} +

This script will be executed with /bin/bash, defines a source and destination path, selects only files that have been modified in the last 120 minutes, and then creates a tar file named for the year-month-day-hour in the destination folder, preserving permissions and ownership, and excluding the destination folder.

Then type crontab -e as a user with read and execute access to /kit... write, read, and execute access to /kit/bkp... and read and execute permission to the script we made before. In the cron file add a line like this:

0 */2 * * * /path/to/your/script.sh > /dev/null

This will run the script every 2 hours on the hour and pipe stdout to /dev/null to keep the script silent unless it produces an error. Cron should message you if an error occurs.

| improve this answer | |
  • every 5 minutes a file with .log extension is generating, have to tar only all those files with that extension to backup directory. – Jithin Aug 12 '19 at 7:05
  • Ah, I see, then Using the find command with the option -mmin 120 to limit the files tar runs on to files modified in the last 120 minutes is your best bet. Everything is the same, except the line with tar in the script needs to be modified to include the find command. – Cliff Armstrong Aug 12 '19 at 7:09
  • Now i faced a problem that the source directory contains many files in which the files with name such as starting with 'log' and ending with .log should only tared. – Jithin Aug 12 '19 at 7:44
  • what to do for that – Jithin Aug 12 '19 at 7:44
  • You could start by searching google for the find command. Or type man find in the linux console. Look, I've already done 95% of the work for you. Stack Exchange isn't really meant to solicit other people to write code for you. That's why your question is getting down-voted. It's meant as a way to exchange knowledge. I've told you want needs to be done. I've given you everything you need to know what to do next. It's up to you now. – Cliff Armstrong Aug 12 '19 at 16:47

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.