0

OS: Ubuntu 22.04 LTS.
Many posts deal with file monitoring. One in particular is of interest and based on inotifywait, but I don't know how to modify it for my purpose.

Objective: to monitor $HOME/{Documents/,Downloads/,Archive/} for link files *.lnk as they are created. Those files are created every time I use Word in Wine to create, save, open or do anything with a document. Dozens *.lnk files can be created in mere minutes. This issue is killing me.

I am willing to learn but can't translate generic examples into what I need for lack of knowledge. I know how to run a script in a plain file, but if there's anything special I should know in this regard, please let me know. Tx in advance.

3
  • Are the .lnk files doing any immediate harm ? If they can be left around for (e.g.) five minutes, I would probably solve this with a simple cron job, rather than trigger an inotify and rm process for every individual file and each directory, which I suspect uses more resources per file. Commented Jun 16, 2022 at 19:11
  • @Paul_Pedant, you have a point. Could be using -roption on /home/$USER but it may not be very efficient. Commented Jun 16, 2022 at 19:25
  • The .lnk files quickly overtake my workspace (which are my folders I'm working in), switching between docs, etc, there are dozens of them, so yes, they're negatively affecting my ability to work quickly and efficiently. Fixing this is more important to me than using resources. Commented Jun 16, 2022 at 19:28

1 Answer 1

1

You need to write this small script in a file using your terminal. I assume you are using the bash shell since you are beginning, and on Ubuntu. Let us know if it is otherwise.

$ touch notify_links
$ chmod u+x notify_links
$ cat notify_script
#!/usr/bin/env bash
inotifywait -mr -e moved_to,create "$HOME"/{Documents,Downloads,Archive} |
while read directory action file; do
    if [[ "$file" =~ .lnk$ ]]; then
        echo rm -f "$file"
    fi
done

Run this script. To do so, just issue (in terminal) the following command notify_links in terminal.

Once satisfied by what you see appearing on terminal display, remove the echo in the script line: echo rm -f "$file" to leave only rm -f "$file".

EDIT 1 per @ilkkachu's comment in order to specialize monitoring to three directories/folders instead of the complete $HOME subtree.

EDIT 2 per @Paul_Pedant's comment, in order to run this automatically every 10 seconds as soon as your boot process is finished, edit your /etc/crontab file with crontab -e to include:

 * * * * * $USER for i in $(seq 5); do /usr/bin/find $HOME -name "*.lnk" -delete; sleep 10; done

EDIT 3 for faster result and lesser resource usage, you'll want to search only the directories that you mentionned in OP. The following will search their subtrees:

 * * * * * $USER for i in $(seq 5); do /usr/bin/find "$HOME"/{Documents,Downloads,Archive} -name "*.lnk" -delete; sleep 10; done

In order to prevent find from recursing down the subtrees, add the following option -maxdepth 1 before -name "*.lnk" in the find command.

18
  • Does this require three separate scripts, for Documents, Downloads and Archives, as noted in the question ? Commented Jun 16, 2022 at 19:14
  • I figured it out (bin bash file) and it worked for the one folder, thanks, though I have multiple folders. But I'd prefer a background process that doesn't involve an open terminal to run, or my having to run scripts manually. That's why I was looking for inotifywait to run in the background whenever I boot. To educate myself is there a reason inotifywait is bad to use for this? Because someone already did the work in the hyperlink posted in the OP, for someone else asking the same exact thing, but I don't know how to modify the code there. – Commented Jun 16, 2022 at 20:20
  • @Paul_Pedant, inotifywait -r would recursively set watches on all directories in the tree, and the script here gives the path /home/$USER to inotifywait. (That should probably be just $HOME, though.) Of course one could change that to inotifywait -m -e moved_to,create "$HOME"/{Documents,Downloads,Archive} to just include those three. Commented Jun 16, 2022 at 20:24
  • @Trynn, well, starting a script in the background automatically is a bit of a different question. One way would be to use an @reboot rule with cron. There are others and some of them would require making sure you don't run two copies of the same script at the same time. And yes, of course you could just put * * * * * find ~ -name '*.lnk' -delete in crontab. (At least as long as there' aren't directories within your home where those .lnk-files should not be deleted. Commented Jun 16, 2022 at 20:27
  • This is what I'm looking for, so maybe a cron job instead. I will do more research on that now, but no, there are no folders anywhere where I want or need .lnk files! So covering the entire tree at boot would be ideal. Commented Jun 16, 2022 at 20:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.