title: MacOS shell script to move files based on tag
body: <p>I am trying to write a shell script so that I can move school files from one destination to another based on the input. I download these files from a source like canvas and want to move them from my downloads based on the tag I assign, to the path for my course folder which is nested pretty deep thanks to how I stay organized. Unfortunately, since I store these files in my OneDrive school account, I am unable to eliminate some spacing issues but I believe I have accounted for these. Right now the script is the following:</p>
  
  <pre><code>if [ "$1" = "311" ];
  then
      course="'/path/to/311/folder/$2'"
  
  elif [ "$1" = "411" ];
  then
      course="'/path/to/411/folder/$2'"
  
  elif [ "$1" = "516" ];
  then
      course="'/path/to/516/folder/$2'"
  
  elif [ "$1" = "530" ];
  then
      course="'/path/to/530/folder/$2'"
  
  elif [ "$1" = "599" ];
  then
      course="'/path/to/599/folder/$2'"
  
  fi
  
  files=$(mdfind 'kMDItemUserTags='$1'' -onlyin /Users/user/Downloads)
  #declare -a files=$(mdfind 'kMDItemUserTags='$1'' -onlyin /Users/user/Downloads)
  #mv $files $course
  #echo "mv $files $course"
  #echo $course
  
  for file in $files
  #for file in "${files[@]}"
  do 
      #echo $file
      #echo $course
      mv $file $course
  done
  </code></pre>
  
  <p>Where $1 is the tag ID and first part of path selection, and $2 is what week number folder I want to move it to. The single quotation marks are there to take care of the spacing in the filepath. I could very easily do this in python but I'm trying to expand my capabilities some. Every time I run this script I get the following message:</p>
  
  <pre><code>usage: mv [-f | -i | -n] [-v] source target
         mv [-f | -i | -n] [-v] source ... directory
  </code></pre>
  
  <p>I initially tried to just move them all at once (per the first mv command that's commented out) and got this error, then tried the for loop, and array but get the same error each time. However, when I uncomment the echo statements in the for loop and manually try to move each one by copying and pasting the paths to the command line, it works perfectly. My best guess is something to do with the formatting of the variable "files", since </p>
  
  <pre><code>echo "mv $files $course"
  </code></pre>
  
  <p>indicates the presence of a newline character or separator between each file it saves.</p>
  
  <p>I'm sure it's something super simple that I'm missing since I just started trying to pick up shell scripting last week, but nothing I have been able to find online has helped me resolve this. Any help would be greatly appreciated. Thanks</p>