Find the answer to your Linux question:
Results 1 to 4 of 4
I know it's a question that is asked often, but I still can't figure out a solution for my specific situation. I have a directory full of files with names ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    2

    File renaming

    I know it's a question that is asked often, but I still can't figure out a solution for my specific situation.

    I have a directory full of files with names like these (These are the actual file names: note that they include spaces):

    1 - Light My Fire
    2 - Crystal Ship
    ....
    10 - People Are Strange


    For my MP3 player to play these files in the correct order, they need to be sorted alphabetically. So I need leading 0's on the file names beginning with 0 through 9. In other words, I want:

    01 - Light My Fire
    02 - Crystal Ship
    ...
    10 People Are Strange

    I have spent a couple of days playing with find -exec and various forms of xargs, and am still having no luck.

    Any advice?

    Thanks.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Lightbulb

    Did you try rename command -- man page says

    rename will rename the specified files by replacing the first occur-
    rence of from in their name by to.

    -V, --version
    Display version information and exit.

    For example, given the files
    foo1, ..., foo9, foo10, ..., foo278, the commands

    rename foo foo0 foo?
    rename foo foo0 foo??

    will turn them into foo001, ..., foo009, foo010, ..., foo278.

    And
    rename .htm .html *.htm

    will fix the extension of your html files.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Code:
    ls > /tmp/tmp.lst
    
    cat /tmp/tmp.lst | while read a b
    do
      i=`printf "%03d" $a`
      echo "mv \"$a $b\" \"$i $b\""
    done
    Starting point, you should be able to turn that into a script.
    In a world without walls and fences, who needs Windows and Gates?

  4. #4
    Just Joined!
    Join Date
    Apr 2010
    Posts
    2

    Thanks

    Thanks to both of you who responded. I tried the rename command, but couldn't figure out a perl expression to do exactly what I wanted. (I'm totally new at this.)

    I did manage to adapte the shell script, and it works great. Just in case anybody else finds this thread, here's what I did to it:

    ls | grep "^[0-9] -"> /tmp/tmp.lst

    cat /tmp/tmp.lst | while read a b
    do
    i=`printf "%02d" $a`
    mv "$a $b" "$i $b"
    done
    Thanks again.

Posting Permissions

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