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.

muru
  • 72,889
Trynn
  • 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. – Paul_Pedant Jun 16 '22 at 19:11
  • @Paul_Pedant, you have a point. Could be using -roption on /home/$USER but it may not be very efficient. – Cbhihe Jun 16 '22 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. – Trynn Jun 16 '22 at 19:28

1 Answers1

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.

Cbhihe
  • 2,701
  • Does this require three separate scripts, for Documents, Downloads and Archives, as noted in the question ? – Paul_Pedant Jun 16 '22 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. – – Trynn Jun 16 '22 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. – ilkkachu Jun 16 '22 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. – ilkkachu Jun 16 '22 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. – Trynn Jun 16 '22 at 20:28
  • Only thing is, just read about cron jobs and it sounds like they run every minute at most... can it be set to watch for the creation of a link file and immediately delete it like your script did? In one minute dozens of .lnk files are created. – Trynn Jun 16 '22 at 20:48
  • @Cbhihe, or just run crontab -e under their regular user account. And actually the syntax of /etc/crontab and the files in /etc/cron.d/* is different from what I just wrote: the system-wide files have an additional column for the username to run under. (It's an additional column, not a replacement to the last *) – ilkkachu Jun 16 '22 at 20:49
  • @Trynn, no, cron runs tasks based on the time, on a fixed schedule. It doesn't do what e.g. inotifywait or other such tools do. – ilkkachu Jun 16 '22 at 20:50
  • Thanks, but between that question and now, I learned it won't delete files on the fly as they're created, so a cron won't work. I need something that catches the files as they are created. I found FileWatcher yesterday which had an XML template and tried using that but again I got some character wrong and it didn't execute the task. – Trynn Jun 16 '22 at 20:55
  • I understand, and your script did what I wanted, so that's what I meant. I thought inotifywait would perform this function in the background at boot, basically doing what your script did, but without having to manually run it or have a terminal open. I apologize for my ignorance and can see I need to do much more research before asking for help. Thanks for your time and your help AND for modifying your script! I will mark your answer as solved. Cheers. – Trynn Jun 16 '22 at 21:06
  • @Trynn You can write a trivial script under cron which runs the rm on entry, with a loop that also runs 5 times after sleep 10 (for example). That gives you a 10-second delay. I am concerned that inotify monitors the whole sub-directory tree under the nominated start points, but then I suspect this requirement was implied in your question (I didn't pick up on that). – Paul_Pedant Jun 16 '22 at 21:13
  • @Cbhihe Thank you for that last comment and the suggestions. Will do a deep-dive into that tremendously helpful guidance. If needed will ask a new question after self-ed and trials. Sincerely appreciated your time and patience in this ramp-up into Linux. – Trynn Jun 16 '22 at 22:02
  • @Paul_Pendant Very good to know, thank you. Everything I'm learning will definitely come in handy! – Trynn Jun 16 '22 at 22:07
  • @Cbhihe - you were editing the script as I was leaving a comment to you. Thank you once again! – Trynn Jun 16 '22 at 22:09
  • @Cbhihe - those last edits were brilliant - can't thank you and Paul enough, and ilkkachu too ... so appreciated! – Trynn Jun 16 '22 at 22:19
  • @Paul_Pedant, if one doesn't want inotifywait to set watches to the whole subtree, they can leave the -r option out. – ilkkachu Jun 16 '22 at 22:34
  • @ilkkachu I will try the script with the three folders first (after I read more about all of this) but then I do want the Home tree covered as I will be using Word in new folders I create as time goes on (and in others already there). So I will be switching that line to leaving just "$HOME" after trials. And thank you for your help here! – Trynn Jun 16 '22 at 22:40
  • Have the old script too, the correct line is not what I wrote above but the following, just so you know I have it right. inotifywait -mr -e moved_to,create /home/$USER | – Trynn Jun 16 '22 at 23:01