Need Help Moving Long List Of Files Into Directories

I am very new to BASH and I am having difficulties moving a long list of image files into similarly named directories. I've been trying to come with a script all night and no luck. Here is what my list of files looks like:

DSC_0059_01.jpg 
DSC_0059_02.jpg
DSC_0059_03.jpg
DSC_0059_04.jpg
DSC_0059_.....jpg
DSC_0060_01.jpg                
DSC_0060_02.jpg
DSC_0060_03.jpg
DSC_0060_.....jpg 

Here is what the directories look like:

DSC_0059 
DSC_0060 
DSC_.......
DSC_0111  

How do I move the .jpegs into their corresponding directories so that, for instance, directory DSC_0059 contains: DSC_0059_01.jpg, DSC_0059_02.jpg, ...etc.?

Thank you

for x in *.jpg; do mv $x ./${x%_*}/ ; done
1 Like

I tried it and I got:

  1. Please post the exact command you used.
  2. Also, please post the output of the following commands: uname -a and echo $SHELL
1 Like

That means you were not in the same directory as the jpg files. You need to be in the same directory as the jpg files when the command is run.

1 Like

I can't thank you all enough. The code works! The .jpg was removed somehow when I was testing my poor code earlier. I just copied the original jpegs back into the test folder and ran the code. Thank you so much!