Help Needed with Shell Script for File Movements

April 19, 2025 3 answers
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

CodeBender
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.

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.

Reply to this answer
ShellSeeker
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
PixelWizard42
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.

Related Discussions