Help with a shell script that’s not moving files?

I'm hoping someone can help me solve a Shell Script problem I have. The Shell Script should move a number of files to specific folders based on what it finds in the filenames.


I've managed to cobble something together but I can't get it to move any files.


Here is the folder structure:


Desktop > testing > Source


Desktop > testing > client name here_Logo set > Black > CMYK


And here are some sample filenames in the Source folder:


client name here_black_CMYK.eps

client name here_black_CMYK.jpg

client name here_black_CMYK.png

client name here_black_RGB.eps

client name here_black_RGB.jpg

client name here_black_RGB.png


This is my code:

kmVAR="client name here"
theSet=${kmVAR// /\\ }
Dst_FolderEnd="_Logo\ set"
Black="/Black"
CMYK="/CMYK"

Src_Folder=~/Desktop/testing/Source/
Dst_Folder=~/Desktop/testing/

Dst_blackCMYK=$Dst_Folder$theSet$Dst_FolderEnd$Black$CMYK

cd $Src_Folder

for file in *;do
  if [[ "$file" == *"$theSet"* && "$file" == *"black_CMYK"* && "$file" != *"ai"* ]];then
    mv "$file" "$Dst_blackCMYK"
  fi
done


If anyone can help, that would be awesome. Many thanks.

Posted on Aug 22, 2022 1:54 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 22, 2022 3:52 PM

short version: filename globbing in the shell with spaces in file names is a nightmare.


As written, the script works if you eliminate the line:


theSet=${kmVAR// /\\ }


and simply replace it with


theSet=$kmVAR


in other words, don't try to escape the spaces... Since you're using quoted strings you don't necessarily need to escape the spaces - as it stands, the script is looking for a file with the literal file name 'client\ name\ here" (including the backslashes in the filename), which is why the if[] statement never passes.


Now, there may be other conditions where you have something other than 'client name here' in the filename, so there may be other things that come to a head, but making that change (and getting rid of the corresponding \ in the Dst_FolderEnd variable) seems to get the script working for me.


(I should also mention the script has no check to make sure the /Black/CMYK/ directory exists for the given $kmVAR. This will also cause an error in the mv command.)

Similar questions

2 replies
Question marked as Top-ranking reply

Aug 22, 2022 3:52 PM in response to JustinR

short version: filename globbing in the shell with spaces in file names is a nightmare.


As written, the script works if you eliminate the line:


theSet=${kmVAR// /\\ }


and simply replace it with


theSet=$kmVAR


in other words, don't try to escape the spaces... Since you're using quoted strings you don't necessarily need to escape the spaces - as it stands, the script is looking for a file with the literal file name 'client\ name\ here" (including the backslashes in the filename), which is why the if[] statement never passes.


Now, there may be other conditions where you have something other than 'client name here' in the filename, so there may be other things that come to a head, but making that change (and getting rid of the corresponding \ in the Dst_FolderEnd variable) seems to get the script working for me.


(I should also mention the script has no check to make sure the /Black/CMYK/ directory exists for the given $kmVAR. This will also cause an error in the mv command.)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Help with a shell script that’s not moving files?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.