Linux folder change auto notification - linux

Is there a way that I can programmatically figure out whether a particular file with certain characteristics is being added into say the linux /tmp folder. All I need to create, is a small script that is automatically called every time a new file is added in the tmp folder.

These links might be helpful: Inotify and Inotify-tools.

You can use entr tool to do that in the following way:
while true; do find /tmp/ | entr -d ./temp_changed.sh; done
A directory watch option (-d) reacts to events when a new file is added to a directory.

Related

inotifywait not working with Visual Studio Code

I tried to make myself a small shell script that would watch for file changes using inotifywait and copy modified/created files and delete deleted files to some destination folder (or rsync specific file). I was not able to make this work.
Investigation: inotifywait is able to detect modifications when I'm editing/deleting/creating files using nano/vim/touch/rm. The problem emerges when I modify/create/delete a file using Visual Studio Code. When I try to watch -d stat some_file, I can see all the modifications happening even when using Visual Studio Code - inotifywait does not detect anything.
I'm not able to figure out what's the problem since I'm not super familiar with Linux.
I would like to be able to use inotifywait since I want to watch for changes in nearly 28000 files. I don't want to use some hybrid of watch, ls, find, stat since iterating whole dir recursively is slow on Virtual Box shared folder. Also fs.inotify.max_user_watches was increased to a sufficient number.
To be complete, here is simplified used command:
$ inotifywait --monitor --recursive /share/repo/

Automatically assign permissions to any file copied into directory

I have a directory and I'd like for any file added to that directory to automatically have chmod performed with a specific set of permissions.
Is there a way to do this?
Reacting to filesystem events (in linux) can be done using inotify.
There are many tools built on inotify which allow you to call commands in reaction to file system events. One such tool is incron. You might like it since it can be configured in a way similar to the familiar cron daemon.
Files moved into a monitored directory generate an IN_MOVED_TO event.
So the incrontab file would contain an entry like
/path/to/watch IN_MOVED_TO /bin/chmod 0644 $#
You can create a cron that checks/chmods files in that directory.
Something like this will work:
find /path/to/directory -type f -print0 | xargs -0 chmod 0644
(Of course you have to edit the path and set the permissions you need)
The question is too unspecified and it is dangerous to give any answer as it is.
Who (/what) creates files in aforementioned directory? What rights do you want to set and why do you think this is needed? Why whatever creates them cannot put expected rights on its own?
For instance, all these "find | chmod" or inotify watchers and other tools mentioned in other comments are a huge security hole if this is a directory everyone can put files to and such a chmoding command would be run with root privs, as it can be tricked into a following a symlink and chmoding stuff like /etc/shadow.
This /can/ be implemented securely of course, but chances are the actual problem does not require any of this.

Change working directory while looping over folders

Currently I am trying to run MRI software (TBSS) on imaging files(scan.nii.gz) on the Linux command line.
The scans are all stored in separate folders for different participants and the file names are identical,so:
/home/scans/participant1/scan.nii.gz
/home/scans/participant2/scan.nii.gz
/home/scans/participant3/scan.nii.gz
What this software does is it creates the result of the analysis in the current working directory.Since the scans have the same image name, they get overwritten al the time.
I would like to loop through all the participant folders, make it my working directory and then execute the tbss command, which is simply tbss_1_preproc scan.nii.gz. In this way, the file will be stored in the current working directory,which is the participant directory.
Is there any sensible way of doing this in Linux ?
Thanks so much !
Try it in BASH. The code below is untested, but it should give you a clue
#! /bin/bash
find . -name scan.nii.gz | while read line
do
cd $(dirname "${line}")
tbss_1_preproc $(basename "${line}")
done
Put it in a file and make it executable. Copy it to your scans folder and execute it.

is it possible to put comments in a directory about a dir or file?

is it possible to put comments in a directory about a dir or file locally on my own machine, appearing in my terminal. I don't have any kind of GUI, so this would be helpful.
Maybe something like, when I do ls -l I will see:
file.txt #this is that file I made on tuesday
files #this is the directory I made with all those other files
If so, what is the tool to do it?
Is it available for Arch Linux?
Yes, there is a tool for that. It is called GIT or SVN.

Inotify compatible Shell script to monitor shell a certain directory

This is first question in Stack overflow. need an inotify compatible script writing that will monitor a certain directory, and if any new files/folders are created in in, copy those files to another folder. I need the script to monitor constantly for changes rather than run periodically.
Thanx in advance.
You can use inotifywait, from the inotify-tools page, to build something like this. A typical use:
inotifywait -m /tmp | while read path events name; do
echo "Now I am going to do something with $name in directory $path."
done
There are oodles of options for controlling how inotifywait operates; consult the man page for details.

Resources