Вопрос

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?

Это было полезно?

Решение

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top