2

I'm looking for a bash script that will detect when a file enters a specific folder. Basically I want to eventually make a script that will detect when a file enters a certain folder on my desktop (using windows with wsl), add .png to the end of the file name (long story, just need that to happen), and then move to another file on my desktop. I have something to rename the files, as a friend made it and we worked together to troubleshoot it

for i in /[directory here] ; do mv "$i" "$i.png" ; done

Now I just need to automate it. I don't want to bother him more, since he has already done so much, and I am too new to Ubuntu to understand exactly what to do

4

You can use inotify-tools

To install it, run the following command in the terminal:

sudo apt install inotify-tools

To use it to serve your purpose, please follow these steps:

  1. Create and edit a script file in your home directory by running the following command in the terminal:

    nano ~/File_Monitor.sh

  2. Copy and paste the following code into the editor replacing PATH_TO_DIRECTORY with the full path of the directory you want to monitor:

#!/bin/bash
inotifywait -m PATH_TO_DIRECTORY -e create |
    while read path action file;
        do
                echo "$path$file was created"
                mv "$path$file" "$path$file.png"
                echo "and renamed to $path$file.png"
        done

  1. Save the script file and exit the editor by pressing Ctrl + X then press Y.

  2. Make the script file executable by running the following command in the terminal:

    chmod +x ~/File_Monitor.sh

  3. Run the script by running the following command in the terminal:

    bash ~/File_Monitor.sh

Done, all new files added to the monitored directory will be renamed to Original_File_Name.png.


Notice:

If you cannot get inotify-tools installed for any reason, you can replace the code in step 2 with the following code also changing PATH_TO_DIRECTORY with the full path of the directory you want to monitor:

#!/bin/bash

while true
do
       sleep 1
       find PATH_TO_DIRECTORY -type f ! -name "*.*" -exec mv {} {}.png \;
done

It will get the job done.

4
  • I got an error when installing inotify-tools ``` E: Unable to locate package inotify-tools ``` Is this something to worry about? – Aidan Quiett Jan 8 '20 at 3:46
  • @AidanQuiett Yes, it is. Please run sudo apt update first then try again. – Raffa Jan 8 '20 at 4:38
  • 1
    Couldn't get inotify-tools working, so I used your second method. Works perfectly now, thank you! – Aidan Quiett Jan 8 '20 at 15:49
  • @AidanQuiett You are most welcome. I am glad it helped. – Raffa Jan 29 '20 at 15:15
0

there is a way to detect whether the folder contains a file that has not .png as ending and then to fix it by adding ".png" to the end.

Just create an executable file by

touch pngchecker

make it executable by

chmod +x pngchecker

open any file editor, for example nano

nano pngchecker

and then insert the following code with any text editor.

#!/bin/bash

testForEnding () {
    echo $file | grep -qE ".png"
    if [ $? == 0 ]; then
        echo "$file has right ending"
    else
        echo "$file has wrong ending -- WROOONGGG"
        mv $file $file.png
        echo " -> fixed!"
    fi
}

if [ -z $@ ]; then         # check whether a path/to/png_folder is given
    echo "Please provide a path/to/png_folder"
    exit 1
fi

for file in $@* ; do testForEnding ; done

At the end you just need to type

./pngchecker path/to/file/ 

If you need the script to be triggered by some event (or beeing executed as a cron job or at system boot) it could be quite handy to add the script to your path.

By doing that you could even delete this part

if [ -z $@ ]; then         # check whether a path/to/png_folder is given
    echo "Please provide a path/to/png_folder"
    exit 1
fi

And just run the command by

pngchecker

in any folder you want after cd ing to there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.