Terry-Crunchbang
Terry-Crunchbang

Reputation: 21

How do I sort a directory full of pictures into sub-directories based on the image name?

I want to take all of the files in /media/mdrive/dump/:

1COD-234355.jpg
MAK-LXT218.jpg
ZIR-CON145.jpg

And create and sort them into the following directories:

/media/mdrive/dump/1/1COD-234355.jpg
/media/mdrive/dump/M/MAK-LXT218.jpg
/media/mdrive/dump/Z/ZIR-CON145.jpg

How would I do that?

Upvotes: 0

Views: 92

Answers (2)

CMS_95
CMS_95

Reputation: 335

to make two directories you could try something like

    dir "/media/mdrive/dump/1/" :: CD would also work here 
    mkdir folder 1
    mkdir folder 2 

from here I think you can continue with your IF statements and so forth. all you need to do is set the dir commands with the Direct path takes the guess work out. then to check each just do:

    start explorer.exe "the folder's path here" 

it should open the folder to view the files

Upvotes: 0

fge
fge

Reputation: 121712

This script takes a directory as the first argument and performs what you need:

#!/bin/bash

DIR="$1"

if [ -z "$DIR" ]; then
    echo >&2 "Syntax: $0 <directory>"
    exit 1
fi

if [ ! -d "$DIR" ]; then
    echo >&2 "\"$DIR\" is not a directory"
    exit 1
fi

cd "$DIR"

for file in *.jpg *.JPG; do
    first=${file::1}
    mkdir -p $first && mv $file $first/;
done

head -c xx will return the first xx characters of its input (here, the filename). mkdir -p will skip directory creation if it already exists.

Upvotes: 4

Related Questions