2

I'm trying to make a bash script that monitors a folder and it's subfolders. Basicly I have other app that makes subfolders and adds files to them. I want to add the files that come into these subfolders into a zip named after the subfolder. Upon which the file in the subfolder is to be deleted.

I'm new to linux and bash scripts, so I'm sorta flunking it :s

#!/bin/bash

inotifywait -mr -e create /home/user/files |
while read filename eventlist eventfile
do

IFS='/' array=($eventfile)
zip /home/user/zips/$(array[1]) $(array[2])

done

So I have a folder /home/usr/files/ in which the app creates subfolders for isntance ...files/files1/. After which the app places the files in the files1 subfolder. I want those files to be zipped in a folder called files1.zip. I don't want to have the subfolder in there as well, just the files.

Is also possible to zip to another extension (ofc still being zipped) by simply adding the extension to the zip command?

Kevin V
  • 329
  • 2
  • 9

1 Answers1

2

There are several problems in your script. For one, you should access array elements with ${array[1]} and not $(array[1]). I did a few modifications, and this seems to work on my system:

#!/bin/bash
inotifywait -mr -e create files |
while read -r path eventlist eventfile
do
    [[ $eventlist == *ISDIR* ]] && continue;
    folder=$(basename "$path")
    zip -j "$folder.zip" "$path/$eventfile"
done
user000001
  • 32,226
  • 12
  • 81
  • 108
  • Thank you, i see I also forgot to ignore the creation of the folder. I'll try this out as soon as I get acces to my linux system. Is it also possible to create this file on a different folder? – Kevin V Sep 16 '13 at 11:27
  • 1
    Ok so sitting at my raspberry pi, I'm afraid it's not working for me. It is showing /home/pi/... CREATE filename, but I can't for the live of me find the zip file. – Kevin V Sep 16 '13 at 17:37
  • @KevinV It should be in the directory where you ran the script – user000001 Sep 16 '13 at 17:40
  • Well I've looked there, even adding echo's for folder , path didn't return anything. – Kevin V Sep 16 '13 at 18:01
  • @KevinV Strange. Maybe if you `echo "zip -j $folder.zip $path/$eventfile"` you can see the command, to see what is generated – user000001 Sep 16 '13 at 18:03
  • I've tried echoing a single line infront of the "zip -j..." instead of the "folder=..." to no succes. The folder does contain a "\" though, not sure if that can cause any problems. – Kevin V Sep 16 '13 at 18:16
  • OK, I'm stupid, I was missing the "|" :) path included the "/" at the end though, so removed the one there. It is now working like a charm. Thank you. Now to research how to auto start the script :) – Kevin V Sep 16 '13 at 19:41