Help Needed with Shell Script for File Movements
Asked by
SunnyDaze89
I'm trying to set up a shell script using Mac Automator to automate moving files into specific folders based on their file extensions. Everything works fine except it's not processing image files like .jpg, .jpeg, .png, .dng, .bmp, .gif, and .heic. I've even tried isolating the script for each image type, but it still fails. Here's a snippet of my script: I'm checking each file, but I'm stuck trying to figure out what I'm doing wrong. Can this tool truly handle image files? I'm getting a bit frustrated over here!
Answers
If you check your `DEST` paths, you need a leading slashโlike this: `DEST="/Users/username/Documents/Images"`. Also, itโs a good idea to quote your variables for commands like `mv`, as this will help manage filenames with spaces. Watch out for that unexplained `elif` at the end too; it's broken syntax! Maybe consider using `else` instead.
Reply to this answer
Just a heads up, your use of backslashes looks a bit sketchy. Make sure they're intentional. Also, using double square brackets like that but mixing in glob patterns is a no-go. Instead, try `[[ $f == *.png ]]` without any quotes or asterisks inside them. You could also test with case statements for cleaner checks!
Reply to this answer
You might want to switch from using `==` to `=~` for regex matching in your if statement. This will clean up your check for image files a lot. For example, you could use `if [[ ${f,,} =~ .(png|jpg|jpeg|dng|gif|heic)$ ]]`. This approach will also consider filenames with uppercase extensions, so you're covered there!
CuriousCoder7: Is there a case problem on a macOS system? I thought the filesystem is case insensitive or is there a nuance?
Reply to this answer
Add your answer
Answers are reviewed before publication.
ShellScriptGuru: Could I possibly send you an email with a complete revision based on your comments? If not I understand ... thanks for your response in any event! It's a txt file for use in Automator.