-1

I am trying to write a script to detect whenever a file is added to a specific folder and run a command using the last file added name. What I am specifically trying to do is to create a QR code for each file I add in a specific folder.

So what I would need to do is: detect when a file is added to folder, grab base filename and pass to qrencode -o filename.png mysite/filename.ext, and ideally, have it a cronjob that starts at boot.

I was reading something about using inotify, but I'm not sure how to make this happen.

2 Answers 2

1

You can use inotifywait to achieve the desired result.

while true
do
inotifywait -r -e create /directory && /bin/bash path/to/your/script
done

Run this script in background with nohup.

1

Try incron. It is available in most distributives via same-named package. I will use Debian and CentOS as examples, as long as it covers almost all cases.

Steps are like that:

1) Install incron

# For Debian
apt-get install incron
# For CentOS
yum install incron

In CentOS you will also need to manually start and enable it.

# For CentOS6
chkconfig incrond on
service incrond start
# For CentOS7
systemctl enable incrond.service
systemctl start incrond.service

2) Add your user to allowed in file /etc/incron.allow (just add username)

3) Add incrontab rule with command incrontab -e

Rule would be like that:

/full/path/to/your/directory/ IN_CREATE your_script $#

IN_CREATE is event of creating file or directory in watched dir.

your_script is the name of your script, that takes the file and does all the work.

$# is name of file, that triggered the event.

In your case you need to change the extension of the file, so it is better to create some simple script, that takes the file and performs all actions.

Something like that (I tried to check everything, but it still can contain errors - check manually before using):

#!/bin/bash
# Setting constants
output_extension='.png'
path_to_save_files='/full/path/to/needed/folder/'

# Using quotes and $@ to catch files with whitespaces
input_file="$@"

# Verifying input extension
[[ "$input_file" =~ ".*\.txt" ]] || exit 

# Cutting everything from the start to last '/' appeared
input_name=${input_file##/*/}

# Cutting everything from the end to first '.' appeared
short_name="${input_name%.*}"

# Creating full name for output file
output_name="${short_name}${output_extension}"

# Creating full path for your output file
output_file="${path_to_save_files}${output_name}"

# Performing your command at last
qrencode -o "$output_file" "$input_file" 
5
  • Thank you, so lets say i want .txt files to trigger the event i would need to add the rule: /full/path/to/your/directory/ IN_CREATE script.sh $*.txt ?
    – BashN00b
    Oct 19, 2016 at 18:48
  • Also i would need to do qrencode -o basename.png mywebsite.com/basename.ext
    – BashN00b
    Oct 19, 2016 at 19:30
  • @BashN00b No, incron is quiet simple, it cannot filter by filenames. So if you need to validate input file by extension, it is better to be done in script. As I can see,
    – kodu.cloud
    Oct 20, 2016 at 6:53
  • Sorry, never used qrencode before, so didn't know, that syntax is qrencode -o output_file input_file I'll edit the script accordingly.
    – kodu.cloud
    Oct 20, 2016 at 7:00
  • I ended up using inotify, for some reason icrond was not working: #!/usr/bin/env bash dir=/ftproot while true #run indefinitely do inotifywait -m -q --format '%w%f' -e create,moved_to $dir | while read FILE; do if [[ $FILE == *.cia ]]; then /ftproot/despacer.sh "$FILE" & /ftproot/rename.sh fi done done
    – BashN00b
    Oct 20, 2016 at 15:29

You must log in to answer this question.

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