0

I'm new here and I'm on a little project currently.

I need to write a script in bash to scan a folder each time a file is dropped in it. In second part it should move it in a new directory created with the name used by this file.

I thought to use incron or watch but I don't know if it's a good solution. The scheme would be like this.

directory="/usr/share/docker-compose"
if "*.yml" exist; then
   do 
      move /usr/share/used-images

Thanks in advance.

2
  • What is the relationship with docker here? Please also edit your post to given an example of the file and the result.
    – Jeff Schaller
    Mar 31, 2020 at 13:14
  • Files dropped in the folder are Dockerfiles. I want to move dropped files in a newly created folder that wears the name of this file and execute a Docker compose command to build my container. Mar 31, 2020 at 13:20

1 Answer 1

0

You could use inotifywait. Example script:

#!/bin/bash

watchdir="$1"

if ! [ -d "$watchdir" ]; then
    echo "Dir $watchdir doesn't exist"
    exit 1
fi

while file=$(inotifywait --format "%f" -e 'create' -e 'moved_to' "$watchdir"); do
    if [ -f "$watchdir/$file" ]; then
        tmpname=$(tempfile -d "$watchdir")
        mv "$watchdir/$file" "$tmpname"
        mkdir "$watchdir/$file"
        mv "$tmpname" "$watchdir/$file/$file"
        # YOURCOMMANDS
    fi
done
1

You must log in to answer this question.

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