1

I'm trying to make a file sorter. Ideas is, I used a program inotifywait to monitor my downloads directory and then find a file that has been moved into that directory. Once found I used the cut command to acquire the format of that file and then move this latter to a specific directory. Thing is, I'm having problems with files that, instead of spaces or " _ , - ", use period to separate words. I seek another way to acquire a file's format.

Here is the code:

#!/bin/bash

############################### Directory paths section
pictures=$HOME/media/pictures/
videos=$HOME/media/videos/
documents=$HOME/Documents/
downloads=$HOME/Downloads/
###############################

inotifywait -m $downloads -e moved_to |

    while read dir action file; do
        
        notify-send "File Detected!" "A file has been added to your downloads directory"
        format=$(echo "$file" | cut -f 2 -d '.')
        sleep 1s

        case "$format" in # Add more formats if needed
            "jpg" | "png" | "jpeg") # Image formats
                mv $downloads$file $pictures
                notify-send "'$file' has been moved to Pictures directory" 
                ;;
            "mp3" | "wav") # Music formats
                mv $downloads$file $music
                notify-send "'$file' has been moved to Music directory"
                ;;
            "mp4" | "ogg") # Video formats
                mv $downloads$file $videos
                notify-send "'$file' has been moved to Videos directory"
                ;;
            "doc" | "docx" | "pdf" | "html") # Document formats
                mv $downloads$file $documents
                notify-send "'$file' has been moved to Documents directory"
                ;;

            *)
                notify-send "File format not recongnized" "the file is kept in Downloads directory"
                ;;
        esac

    done

And if you have any improvements for suggestions please let me know. Thanks.

Bruheem
  • 13
  • 3
  • As the bash tag you used states - `For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting here.` – Ed Morton Aug 07 '22 at 13:33

1 Answers1

0

I think this is what you want to solve the specific problem you asked about:

$ for file in foo.png foo.bar.pdf foo.bar.png.pdf.jpg; do
    suffix="${file##*.}"
    echo "$file -> $suffix"
done
foo.png -> png
foo.bar.pdf -> pdf
foo.bar.png.pdf.jpg -> jpg

but please do run shellcheck on your code and fix the additional issues it tells you about.

Note that the above is just identifying the suffix a file has, that doesn't necessarily mean the file has any specific format since Unix doesn't use file suffixes to determine the format of a file, that's a Windows thing. A file that ends in .doc or .pdf could be plain text or an image or anything else. Try using the the command file to identify a files actual format instead.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    Thank you. that's exactly what I needed. Also I'll remember running shellcheck whenever I do scripting. Thanks again! – Bruheem Aug 07 '22 at 14:19