1

I have a lots of images with timestamp:

cloudcam-20130825T115716Z.jpg

In the same folder I would like to create a folder named after the date(20130825) and move all the images to this folder.

Is there an easy way to do this?

  • 2 months later you favor a 1 time solution over a solution that is more flexible? – Rinzwind Nov 7 '13 at 10:35
  • Both script is working, hard to decide – OHLÁLÁ Nov 7 '13 at 10:40
  • Oh I do not mind that you accept the other one ;) Please do. I got more than enough rep :D @run – Rinzwind Nov 7 '13 at 10:46
1

Tell me if this works (untested ;) ):

for f in `ls cloudcam-????????.jpg`; do
    name=`echo "$f"|sed 's/ -.*//'`
    datedir=`echo "$name"|cut -c 10-17`
    dir="DestinationDirectory/$datedir/$name"
    mkdir -p "$datedir"
    mv "$f" "$datedir"
done

.. it should copy all jpg's beginning with cloudcam-, create a directory for position 10 to 17 of the file name, create the directory and move the file. So if you have images with another date it will put those in another directory.

Save it and make the script executable with chmod 775.

Try it first on a copy of your files.

3

You could use a for loop...

  • test run first to make sure it lists all you need

    for i in cloudcam-20130825*; do echo $i; done
    
  • move images

    for i in cloudcam-20130825; do mv $i 20130825; done
    
1

You can move all the matching files with a simple shell glob

$ mkdir 20130825
$ mv cloudcam-20130825*.jpg 20130825

This will work provided there aren't 1000s of files (in which case it might be necessary to 'batch' the moves using find -exec mv ... + or xargs)

If you have a bunch of different dates and you want to parse the filenames on the fly and create directories as needed, you could do something like

for f in cloudcam-*.jpg; do d="${f:9:8}"; [[ ! -d "$d" ]] && mkdir "$d"; mv -t "$d" "$f"; done

This assumes the filenames are exactly as shown in your original post i.e. that the date stamp is yyyymmdd starting at character #9 - if that's not the case then more sophisticated parsings are possible e.g. using bash constructs like ${f#front} and ${f%%back} to remove leading and trailing substrings.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.