Page 1 of 2 12 LastLast
Results 1 to 10 of 18

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

  1. #1
    Join Date
    Dec 2012
    Beans
    6

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

    Hi,

    I have a drive with a real messed up set of directories/folders with lots of *.jpg, *.avi. The mess-up, unsurprisingly is the result of i-photo...

    I need to search through the whole structure and pull out all the relevant files to one folder. The problem is that a lot of the files (images from my camera) have the same name, such as DSC_0001.jpg - not a problem now because they're in different folders...

    How can i recursively work through the folder system, copy all the pictures (including the hidden pictures, of which there seem to be many) in to a single new folder and rename them with a unique name?

    Thank you -

  2. #2
    Join Date
    Feb 2012
    Location
    Closer Than You Think
    Beans
    Hidden!
    Distro
    Ubuntu 13.04 Raring Ringtail

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

    I am just going through "THE LINUX COMMAND LINE.pdf" and this would be a great project.

    I hope this helps
    What keeps the world in chains but your beliefs?
    And what can save the world except your Self?
    Belief is powerful indeed.

  3. #3
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,061
    Distro
    Ubuntu 14.04 Trusty Tahr

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

    Hi bashwannabe. Welcome to the forums

    You can accomplish that using the command 'find' and using the --backup option on the command mv to avoid overwriting your files.

    Take a look at this thread with a similar concern a several suggestions.

    Let us know how it goes.
    Regards.

  4. #4
    Join Date
    Dec 2012
    Beans
    6

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

    Thank you both for your help. It looks like the thread you posted is exactly what i need!

  5. #5
    Join Date
    Dec 2012
    Beans
    6

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

    Thank you Papibe - i have been trying out the code and it works. when i have several files with the same name (the files might be different) the back up command does not overwrite, making for example:

    pic_001.jpg
    pic_001.jpg.~2~
    pic_001.jpg.~3~
    pic_001.jpg.~4~

    etc.

    Is there some code i could use to take the back-up files (the ones ending in ~) and give them the jpg extension so that i can look at them and manipulate them as if they were normal jpg and see them in a normal finder or home-folder window?

    The names don't matter so much as the ability to easily look at and deal with each file.

    Thank you and regards,


  6. #6
    Join Date
    Apr 2012
    Beans
    5,972

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

    you could do something like this - just moves the unique numeric part of the ~n~ suffix to the other side of the .jpg extension

    Code:
    $ rename -nv 's/([^.]*)[.]jpg[.]~([0-9]+)~/$1_$2.jpg/' *.jpg.*
    pic_001.jpg.~2~ renamed as pic_001_2.jpg
    pic_001.jpg.~3~ renamed as pic_001_3.jpg
    pic_001.jpg.~4~ renamed as pic_001_4.jpg
    Have a play and decide how you want the names to look - then when you're ready, remove the 'n' option to make it run for real

    (you could use ($2) instead of _$2 or whatever)

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

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

    Code:
    rename -v 's/([^.]+)[.]([^.]+)[.]~(.+)~$/$1__$3.$2/' *~*~
    it is probably more scary than it needs to be, but i thought it's good to make it more universal and it will work with any backed up file type. That expression will reuse the parts enclosed in parentheses:
    (name).(extension).~(number)~ => name__number.extension

    edit: steeldriver posted pretty much the same thing, but with fixed jpg part
    Last edited by Vaphell; December 28th, 2012 at 09:42 PM.
    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. #8
    Join Date
    Dec 2012
    Beans
    6

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

    Hi thank you steeldriver and vaphell - i have tried each of your expressions and in each case i get an error -

    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]

    I've double checked and i am using your code verbatim inside the folder with all the *.jpg files.

    do i need to download/initiate perl?

  9. #9
    Join Date
    Apr 2012
    Beans
    5,972

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

    are you maybe missing a space between the options ( -nv ) and the expression (the 's/.../.../ ' part)?

    Code:
    $ rename -nv's/([^.]*)[.]jpg[.]~([0-9]+)~/$1_$2.jpg/' *.jpg.*
    Unknown option: s
    Unknown option: /
    Unknown option: (
    Unknown option: [
    Unknown option: ^
    Unknown option: .
    Unknown option: ]
    Unknown option: *
    Unknown option: )
    Unknown option: [
    Unknown option: .
    Unknown option: ]
    Unknown option: j
    Unknown option: p
    Unknown option: g
    Unknown option: [
    Unknown option: .
    Unknown option: ]
    Unknown option: ~
    Unknown option: (
    Unknown option: [
    Unknown option: 0
    Unknown option: 9]+)~/$1_$2.jpg/
    Usage: rename [-v] [-n] [-f] perlexpr [filenames]
    $
    $ rename -nv 's/([^.]*)[.]jpg[.]~([0-9]+)~/$1_$2.jpg/' *.jpg.*
    pic_001.jpg.~2~ renamed as pic_001_2.jpg

  10. #10
    Join Date
    Dec 2012
    Beans
    6

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

    Hi steeldriver - i've posted below the error i get - also checked perl version: it is perl 5 version 12

    $ 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]
    $

Page 1 of 2 12 LastLast

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
  •