Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Recursive copy and rename of *.jpg, *.avi etc

  1. #11
    Join Date
    Apr 2012
    Beans
    6,966

    Re: Recursive copy and rename of *.jpg, *.avi etc

    OK I think I know the problem - you have a file whose name starts with a '-' character and somehow that is getting interpreted as additional options:

    Code:
    $ > '-38.jpg.~1~'
    $ 
    $ rename -nv 's/([^.]*)[.]jpg[.]~([0-9]+)~/$1_$2.jpg/' *.jpg.*
    Unknown option: 3
    Unknown option: 8
    Unknown option: .
    Unknown option: j
    Unknown option: p
    Unknown option: g
    Unknown option: .
    Unknown option: ~
    Unknown option: 1
    Unknown option: ~
    Usage: rename [-v] [-n] [-f] perlexpr [filenames]
    Try adding '--' after the options so that the shell knows that everything beyond that is *not* part of the option list:

    Code:
    $ rename -nv -- 's/([^.]*)[.]jpg[.]~([0-9]+)~/$1_$2.jpg/' *.jpg.*
    -38.jpg.~1~ renamed as -38_1.jpg
    pic_001.jpg.~2~ renamed as pic_001_2.jpg
    You can use Vaphell's nice expression instead of course

  2. #12
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    1,512
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursive copy and rename of *.jpg, *.avi etc

    If you install the program jhead found in the standard repos then it can rename the files as you move them based on the photos metadata, heres a nice few lines for you
    Code:
    mkdir photos
    find ./ -iname *.jpg -exec mv {} ./photos/ \; |xargs find ./ -iname *.jpg |xargs jhead -n%d-%m-%Y\ %H:%M:%S
    This creates a folder named photos, then find any files ending in .jpg or .JPG and moves them to the new photos folder also it renames them to the format "date-month-year hour:minute:second.jpg" this works for all jpegs as long as two wern't taken at exactly the same second, you could then sort them into folders if you wanted using jhead again, I have a script which automates this which runs on login, heres the chunk you'd need if you'd like the make use of it, you could literaly paste this into a terminal so long as all the photos are in the $HOME/Pictures folder it should work fine
    Code:
    IFS=$'\n'
    # determine files for iteration
    for photo in $(ls $HOME/Pictures/ | grep -i -e jpg -e jpeg); do
    	# obtain photo timestamp
    	timestamp=$(jhead $HOME/Pictures/$photo | grep Date/Time)
    	# check for successful timestamp
    	if [ -z $timestamp ]; then
    		let errors=$errors+1
    	fi
    	# obtain year and month 
    	year=${timestamp:15:4}
    	month=${timestamp:20:2}
    	# create directory, move and rename photo
    	if [ $year ] && [ $month ]; then
    		mkdir -p $HOME/Pictures/$year/$month; mv -n $HOME/Pictures/$photo $HOME/Pictures/$year/$month/ && let count=$count+1
    		jhead -n%d-%m-%Y\ %H:%M:%S $HOME/Pictures/$year/$month/$photo
    	fi
    done
    As for the .avi I cant be as helpful I'm afraid
    Catch me on Freenode - imark

  3. #13
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Recursive copy and rename of *.jpg, *.avi etc

    Code:
    IFS=$'\n'
    for photo in $(ls $HOME/Pictures/ | grep -i -e jpg -e jpeg); do
    ...
    very very fugly - redefined IFS, ls used to fuel the loop...

    straightforward globs
    Code:
    for photo in $HOME/Pictures/*.[jJ][pP][gG] $HOME/Pictures/*.[jJ][pP][eE][gG]; do
    find+while read
    Code:
    while IFS= read -rd $'\0' photo
    do
      ...
    done < <( find $HOME/Pictures/ -iregex '.*jpe?g$' -type f -print0 )
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #14
    Join Date
    Dec 2012
    Beans
    6

    Re: Recursive copy and rename of *.jpg, *.avi etc

    Steeldriver, your last post works, thank you!

    So now i have three folders containing hundreds of thousands of *.jpg - many of which are copies, with different names, some as a result of the backup copy we did earlier, many because of the way the images were stored previously. Here there may be copies of the pictures in different resolutions.

    Based on what Captain Mark is saying, is it possible to write a script which will go through all three folders, and put all the pictures into one folder, ensuring that only one copy of the same *picture*, with its highest available resolution, is saved?

    This sounds complicated to me. Thank you all for your help so far - i never thought i would get this far in the first place!!
    Last edited by bashwannabe; December 31st, 2012 at 10:45 AM.

  5. #15
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    1,512
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursive copy and rename of *.jpg, *.avi etc

    Quote Originally Posted by Vaphell View Post
    very very fugly - redefined IFS, ls used to fuel the loop...
    I'm only a noob myself by most standards so be nice on the criticism, I'll take on board your code though,
    Thanks
    Catch me on Freenode - imark

  6. #16
    Join Date
    Apr 2012
    Beans
    6,966

    Re: Recursive copy and rename of *.jpg, *.avi etc

    Quote Originally Posted by bashwannabe View Post
    Steeldriver, your last post works, thank you!
    Glad it worked! sorry about the couple of false starts

    Quote Originally Posted by bashwannabe View Post
    Based on what Captain Mark is saying, is it possible to write a script which will go through all three folders, and put all the pictures into one folder, ensuring that only one copy of the same *picture*, with its highest available resolution, is saved?
    I think something like that might be possible - using the exif metadata in the jpg files - I leave it to one of the picture experts to answer though

  7. #17
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Recursive copy and rename of *.jpg, *.avi etc

    I'm only a noob myself by most standards so be nice on the criticism, I'll take on board your code though,
    Thanks
    sorry about that, but seeing ls output processing in scripts gets old really fast It's the very first error mentioned here:
    http://mywiki.wooledge.org/BashPitfalls


    is there any straightforward rule that says two images of different sizes are related? do they share the same name? exif data? because if not, then you'd have to use imagemagick tools to apply some fuzzy logic to determine the similarity (scale temp copies of images to common size and calculate correlation). While doable, it would be very very annoying.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #18
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    1,512
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Recursive copy and rename of *.jpg, *.avi etc

    I don't think jhead would differeniate the two, I just tested and if you have two images with the same timestamp down to the second it will name them DD-MM-YY HH:MM.jpg and DD-MM-YY HH:MMa.jpg depending on which is first, I'm sure there would be a way to filter all conflicting images to a seperate folder for analysis but I don't know of a way to automate this to choose to keep the highest resolution

    EDIT: Perhaps Vaphell could help, you seem to know bash well, could one rearrange the script to use jhead on all *jpg's starting with the largest file first working its way to the smallest file, this way a find ./ -name *a.jpg would find all the smaller versions of the duplicate files
    Last edited by CaptainMark; January 1st, 2013 at 11:55 AM.
    Catch me on Freenode - imark

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •