From: Tuxedo on
Hi,

I have accumulated a directory with at least 5000 digital images. They're
always named like img_9374.jpg etc.

Doing a simple 'ls' takes a long time to return the list given the amount
of images present. Some graphic programs also seem to impose a limit of
what they can preview or list. Furthermore, my digicam will soon reset the
image count which would eventually result in conflicting file names unless
I reorganise the storage system. As such, I would like to place all images
in daily sub-directories, but preferably not the manual way...

Instead I would like to run a batch procedure to loop through the directory
and move all images into daily named sub-directories based on their file
modification date, so that all images with a same date, such as for example
those dated 16th July 2010 would end up in a sub-directory named
2010-07-16/ etc. In other words, the shell procedure should create this
sub-directory, move the images with the matching date into it before
proceding with the next batch. Any relevant sub-directories should be
automatically created based on the modification dates found amongst the
images in the present directory, from where for example a 'reorganise.sh'
shell script may be run.

Perhaps someone has a similar procedure in use already and are willing to
share it?

Tuxedo

From: pk on
Tuxedo wrote:

> Hi,
>
> I have accumulated a directory with at least 5000 digital images. They're
> always named like img_9374.jpg etc.
>
> Doing a simple 'ls' takes a long time to return the list given the amount
> of images present. Some graphic programs also seem to impose a limit of
> what they can preview or list. Furthermore, my digicam will soon reset the
> image count which would eventually result in conflicting file names unless
> I reorganise the storage system. As such, I would like to place all images
> in daily sub-directories, but preferably not the manual way...
>
> Instead I would like to run a batch procedure to loop through the
> directory and move all images into daily named sub-directories based on
> their file modification date, so that all images with a same date, such as
> for example those dated 16th July 2010 would end up in a sub-directory
> named 2010-07-16/ etc. In other words, the shell procedure should create
> this sub-directory, move the images with the matching date into it before
> proceding with the next batch. Any relevant sub-directories should be
> automatically created based on the modification dates found amongst the
> images in the present directory, from where for example a 'reorganise.sh'
> shell script may be run.

Something like (assuming you have GNU stat)

cd /photo/dir

for f in *.jpg; do

ymd=$(stat -c %y "$f")
ymd=${ymd%% *}

if [ ! -d "${ymd}" ]; then
mkdir "${ymd}"
fi

mv -- "$f" "${ymd}"

done

From: Janis Papanagnou on
On 28/07/10 15:33, Tuxedo wrote:
> Hi,
>
> I have accumulated a directory with at least 5000 digital images. They're
> always named like img_9374.jpg etc.
>
> Doing a simple 'ls' takes a long time to return the list given the amount
> of images present. Some graphic programs also seem to impose a limit of
> what they can preview or list. Furthermore, my digicam will soon reset the
> image count which would eventually result in conflicting file names unless
> I reorganise the storage system. As such, I would like to place all images
> in daily sub-directories, but preferably not the manual way...
>
> Instead I would like to run a batch procedure to loop through the directory
> and move all images into daily named sub-directories based on their file
> modification date, so that all images with a same date, such as for example
> those dated 16th July 2010 would end up in a sub-directory named
> 2010-07-16/ etc. In other words, the shell procedure should create this
> sub-directory, move the images with the matching date into it before
> proceding with the next batch. Any relevant sub-directories should be
> automatically created based on the modification dates found amongst the
> images in the present directory, from where for example a 'reorganise.sh'
> shell script may be run.
>
> Perhaps someone has a similar procedure in use already and are willing to
> share it?

Something like this... (untested)

for f in img_*.jpg
do
d=$( stat -c%y "$f" | awk '{gsub(/-/,"/",$1); print $1}' )
mkdir -p "$d"
mv -- "$f" "$d"/"$f"
done

It creates a directory tree starting with year on level 1 and month on
level 2 and day on level 3. So one file may end in directory, e.g.,

./2010/07/16/img_9374.jpg


Janis

>
> Tuxedo
>

From: Janis Papanagnou on
On 28/07/10 16:19, Janis Papanagnou wrote:
> On 28/07/10 15:33, Tuxedo wrote:
>> Hi,
>>
>> I have accumulated a directory with at least 5000 digital images. They're
>> always named like img_9374.jpg etc.
>>
>> [...]
>>
>> Instead I would like to run a batch procedure to loop through the directory
>> and move all images into daily named sub-directories based on their file
>> modification date, so that all images with a same date, such as for example
>> those dated 16th July 2010 would end up in a sub-directory named
>> 2010-07-16/ etc. [...]

Oops! Reading pk's proposal I apparently missed that you want them all
beneath a single directory level. So what I proposed may not suit you.

Janis
From: Loki Harfagr on
Wed, 28 Jul 2010 15:33:18 +0200, Tuxedo did cat :

> Hi,
>
> I have accumulated a directory with at least 5000 digital images.
> They're always named like img_9374.jpg etc.
>
> Doing a simple 'ls' takes a long time to return the list given the
> amount of images present. Some graphic programs also seem to impose a
> limit of what they can preview or list. Furthermore, my digicam will
> soon reset the image count which would eventually result in conflicting
> file names unless I reorganise the storage system. As such, I would like
> to place all images in daily sub-directories, but preferably not the
> manual way...
>
> Instead I would like to run a batch procedure to loop through the
> directory and move all images into daily named sub-directories based on
> their file modification date, so that all images with a same date, such
> as for example those dated 16th July 2010 would end up in a
> sub-directory named 2010-07-16/ etc. In other words, the shell procedure
> should create this sub-directory, move the images with the matching date
> into it before proceding with the next batch. Any relevant
> sub-directories should be automatically created based on the
> modification dates found amongst the images in the present directory,
> from where for example a 'reorganise.sh' shell script may be run.
>
> Perhaps someone has a similar procedure in use already and are willing
> to share it?
>
> Tuxedo

idea posted in c.u.q. reposted here as you curiously multi-posted that Q. ,-)

this little trick should do:

$ ls -l *.jpg | awk '{d=$6;sub(/^.*:.../,"\"");sub(/$/,"\"");printf("( [ -d %s ] || mkdir %s ) && mv %s %s/\n",d,d,$0,d)}' | sh

of course use with your own "selector" (*.jpg *.png / find... whatever)
obviously you may prefer to check what it'd do before actually doing it, then just
redirect the output and only activate the moves when dry and high, e-g:

$ ls -l *.jpg | awk '{d=$6;sub(/^.*:.../,"\"");sub(/$/,"\"");printf("( [ -d %s ] || mkdir %s ) && mv %s %s/\n",d,d,$0,d)}' > /tmp/wooof
$ sh /tmp/wooof