Search code examples
linuxbashshelljob-schedulingautosys

Shell script to unzip files using file watcher


I have started working on Linux recently. I'm looking for a solution for monitoring a folder for zipped files and unzip them. I have created a simple script(say unzip.sh) to unzip the files but there is no fix time at which zipped are received, so I want to use a script that checks for zipped files and calls this unzip.sh script if found any zipped files. Thanks.

#!/bin/sh
cd <dir name>
for i in `find . -name "*.gz"`; do
    gunzip $i
done

Solution

  • crontab is what you need

    Build a simple textfile like this:

    */5 * * * * bash /path/to/your/script.sh
    

    Assuming you called this file my.cron, then you can install it with

    crontab my.cron
    

    The script will then run every 5 minutes. here some more examples: https://crontab.guru/examples.html

    Stop it again with

    crontab -r