Results 1 to 6 of 6

Thread: renaming files in directory using a shell script

  1. #1
    Join Date
    Jun 2010
    Beans
    380

    renaming files in directory using a shell script

    I am having a lot of .JPG files
    DSCN2350.JPG DSCN2354.JPG DSCN2358.JPG DSCN2362.JPG DSCN2366.JPG
    DSCN2351.JPG DSCN2355.JPG DSCN2359.JPG DSCN2363.JPG
    DSCN2352.JPG DSCN2356.JPG DSCN2360.JPG DSCN2364.JPG

    I want to rename them d1.jpg d2.jpg d3.jpg and so on so I wrote a shell script

    Code:
    #/bin/bash
    p=0
    for i in *.JPG;
    do
    mv "$i" "d$p.JPG"
    ((p+1))  ;
    done
    but what happened is among a lot of files when I run the above script as an output rest of the JPG files get deleted
    and I am left with only one file
    d0.jpg
    what is the error happening here ?

  2. #2
    Join Date
    May 2011
    Beans
    225
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: renaming files in directory using a shell script

    The problem is in the line:
    Code:
    ((p+1))
    you are computing the value p+1, but you are not storing it anywhere. Use
    Code:
    ((p++))
    which will increment p.

    So in total your code should be:
    Code:
    #!/bin/bash
    
    p=0
    for i in *.jpg;
    do
      mv "$i" "d$p.JPG"
      ((p++))
    done
    
    exit 0

  3. #3
    Join Date
    Jun 2013
    Beans
    2

    Question Re: renaming files in directory using a shell script

    This looks good but... I am looking for something slightly more sophisticated.

    Dream:
    I have a folder called /to be moved
    in this folder there are subfolders of movies which in turn will have either .mp4 or .avi files but in multiple parts
    plexapp.com supports multi-file movies but they need to be named /movie abc pt1.avi /movie abc pt2.avi etc

    the best would be that I could run the script from the folder /to be moved and that it would take each sub-dir take the name of the subdir and do the rename of the files according to what I stated above. Once finished move all the subdirs to /updated

    Good Enough:
    have the script do the renaming mentioned above in each subdir (just take the name of the Directory and apply to the underlying files.

    Any suggestion ?

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: renaming files in directory using a shell script

    "good_enough" is something like

    Code:
    #! /bin/bash
    
    # takes the directory where the files are as an argument
    dir=$1
    rootname=$(basename $dir)
    partseq=1
    for f in "$1/"*.avi
    do
       mv "$f" "$dir/$rootname-part$partseq.avi
       ((partseq++))
    done
    and "dream" is

    Code:
    #! /bin/bash
    
    #execute good_enough in all directories below the root dir passed as a parameter:
    find $1 -type d -exec good_enough {} \;
    Completely untested...

  5. #5
    Join Date
    Jun 2013
    Beans
    2

    Re: renaming files in directory using a shell script

    Many thanks for this. I would like to test this but now the Linux noclue question is:
    - I have the "Master Directory". there I placed the "good enough" how do I run it in order to make it run on a directory called movie a ?
    Sorry for a lame question but really close to 0 skills!
    Thanks

  6. #6
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    rename script

    If you want to continue writing your own script for learning purposes, then go for it. There's nothing like having a concrete problem to solve to focus learning. However, if you are only trying to bulk rename some files and need to get that done to move on to something else, then look at rename. (There's no manual page online for me to link to but you can find it in your own terminal.)

    Code:
    rename -n '$a++;s/^.*.jpeg$/d$a.jpeg/' *.jpeg
    The -n makes it do just a dry run without actually changing the names. Remove it once you see the results you want.

    Edit: You could also add leading zeros so that the result sorts better. Change the sprintf statement to increase the number of zeros.

    Code:
    rename -n '$a++;$a=sprintf("%02d",$a);s/^.*.jpeg$/d$a.jpeg/' *.jpeg
    Last edited by Lars Noodén; June 21st, 2013 at 09:56 AM.

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
  •