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.