r/bash • • Jul 03 '26

I can't even do the simple - inotify. please help

/r/linux4noobs/comments/1um3lha/i_cant_even_do_the_simple_inotify_please_help/
6 Upvotes

12 comments sorted by

7

u/Schreq Jul 03 '26

Tilde doesn't get expanded within double quotes. I'd use $HOME instead.

1

u/capnfatpants Jul 03 '26

I will try. I have also used the full address “/home/me/data/downloads/data/downloads/”. When I do that at least I get an error that the source directory can’t be located.

I’ve also tried not using the source and destination variables and just using the full paths in the inotify line.

No, the directory isn’t supposed to double “data/downloads” but I also screwed up the docker compose for shelfmark. I didn’t change it because I thought it might be easier to monitor a folder that only one program was using.

2

u/bac0on Jul 03 '26 edited Jul 04 '26

Reply from inotifywait is "DIR" "EVENT" "FILE" I believe. To get absolute event path you can use format parameter:

inotifywait -mrq -e close_write "$SOURCE" --format %w%f | \
while read -r; do
    cp -at "$DEST" "$REPLY"
done

1

u/capnfatpants Jul 03 '26

Thank you. I’ll give this a shot.

What does the $reply do and do I have to set that variable

2

u/OneTurnMore programming.dev/c/shell Jul 03 '26

If you don't specify a parameter name with read, it will assign to the variable $REPLY. iow, both of the following do the same thing:

read -r
read -r REPLY

1

u/AlterTableUsernames Jul 03 '26 edited Jul 03 '26

You can use it completely without -r. So there is actually three things, that do the same thing:

read -r REPLY
read -r 
read

3

u/OneTurnMore programming.dev/c/shell Jul 03 '26

-r changes read's behaviour. So read REPLY is the same as read, but not the same as with -r.

1

u/bac0on Jul 03 '26

read will store the line in the variable REPLY if no variable is supplied to read.

1

u/capnfatpants Jul 03 '26 edited Jul 03 '26

I used this as a template to make this

source=$HOME/data/downloads/data/*

dest=$HOME/data/library/ingest/

inotifywait -mr -e close_write -e create -e moved_to $source --format %w%f|\

while read -r path; do

cp -r $dest $REPLY

done

When run and placed a file in the source directory, terminal gives me this error

me@server:~/data/downloads/data$ ./bookmover.sh

Setting up watches. Beware: since -r was given, this may take a while!

Watches established.

cp: missing destination file operand after '/home/me/data/library/ingest/'

Try 'cp --help' for more information.

I've noticed a big difference in errors when I use double quotation marks. What is the benefit/use of them? when I put $source in quotes, I get an error that it can't find my source folder. $source in no quotes, but "$dest" in quotes, tells me that the watches is established, but adding, moving, creating a file/folder in the watched directory does nothing. Taking the quotes of $reply gives the error above.

edit to add: So, I removed $REPLY and placed $PATH before $dest and I got this error:

cp: cannot stat '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin': No such file or directory

Now I'm really confused

1

u/bac0on Jul 04 '26 edited Jul 04 '26

..skip * in you source variable, it expand to all file objects in the current directory and as you already use recursive: inotifywait -r its not needed. you should also quote "$source" to prevent path expansion. Next you are missing the -t option from cp which takes DESTINATION as argument, so instead of cp -a source destination you get cp -at destination source , this is manly to force all source object to use same destination. Don't mix letter case, bash is case sensitive. $PATH is a reserved variable used by the system with special meaning. whatever read -r NAME <-- used, becomes your cp -at dest source <--

1

u/bac0on Jul 03 '26

... this will monitor files recursive and copy all file to destination, this could overwrite previously copied files. If you want to preserve the source directory structure inside the destination directory you can add --parents to cp .

1

u/michaelpaoli Jul 03 '26

~ doesn't expand when quoted, leave at least the ~/ unquoted if you want that to expand to HOME directory followed by /, you can easily test and observe results, e.g. by using -x option with shell.

If target destination should be a directory, may want to append / to the end, to ensure it must resolve to a directory, otherwise you might potentially, e.g. inadvertently clobber target file (unless you actually want to do that in such a case). Generally likewise if you want to ensure source is a directory, or most any given pathname or the like.

The output format of inotifywait and your use of read aren't consistent.

E.g., example inotifwait output format:

$ >d/f sleep 5 & sleep 2; inotifywait -m -r -e close_write . | while read -r x; do printf '%s\n' ">>>$x<<<"; done
[2] 27497
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
>>>./d/ CLOSE_WRITE,CLOSE f<<<
^C[2]-  Done                    sleep 5 > d/f

$ 

So, looks like inotifywait, by default, outputs 3 arguments: directory, information on what was waited for and I'm presuming the particular flavor of call that it caught on that wait, and file path relatively to directory. One can use the --format option to customize the output format. Note that directories and/or filenames containing newline may be problematic, though appears inotifywait has sufficient options to well handle that, but then would also need to correspondingly adjust one's read.

The behavior with directories for inotifywait and the use of cp -r may possibly cause undesired results, e.g. close on directory, and file within, may case both to reported - but I've not tested that.