0

I have several images with ID-sequence.jpg name where ID is same for a group of images, for example:

4fd-00027-1.jpg
4fd-00027-2.jpg
4fd-00027-3.jpg
6gq-00017-1.jpg
6gq-00017-2.jpg
6gq-00752-3.jpg
6gq-00752-4.jpg

... now I need to move all those files into their own directories which should also be named the same as ID. I believe I need something like this:

for FILE in *; do
ID_REGEX="(.*(?=-))"
if [[ $FILE =~ $ID_REGEX ]]; then
  ID="${BASH_REMATCH[1]}"
  echo "$ID"
  mkdir -p "/Users/myname/images_organized/$ID"
  $(mv "/Users/myname/images/$FILE" "/Users/myname/images_organized/$ID/$FILE" )
fi
done

... but its not doing anything. No errors either.

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • Possible duplicate of [How to mkdir only if a dir does not already exist?](http://stackoverflow.com/questions/793858/how-to-mkdir-only-if-a-dir-does-not-already-exist) – Kir Chou Oct 27 '16 at 05:13
  • Ah thanks, knew about it but didn't think. But my code still doesn't move the files. – eozzy Oct 27 '16 at 05:18
  • Do you know the bash `-x` option to print the script flow? – Jdamian Oct 27 '16 at 06:24

1 Answers1

1

Too much work.

for file in *
do
  dir="${file%%-*}"
  [ -d "$dir" ] || mkdir "$dir"
  mv "$file" "$dir"
done
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358