Find the answer to your Linux question:
Results 1 to 6 of 6
Suppose I have the following files: 1132_1_fr.mp3 1132_2_fr.mp3 1132_3_fr.mp3 . . . 1132_3_fr.mp3 PD_1132_65_fr.mp3 PD_1132_7_fr.mp3 . . . I want to rename them so everyone look like: PD_1132_1.mp3 PD_1132_2.mp3 PD_1132_3.mp3 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    6

    Angry VERY EASY: Rename many files with one command (in command prompt)


    Suppose I have the following files:

    1132_1_fr.mp3
    1132_2_fr.mp3
    1132_3_fr.mp3
    .
    .
    .
    1132_3_fr.mp3
    PD_1132_65_fr.mp3
    PD_1132_7_fr.mp3
    .
    .
    .



    I want to rename them so everyone look like:

    PD_1132_1.mp3
    PD_1132_2.mp3
    PD_1132_3.mp3
    .
    .
    .
    PD_1132_65.mp3

    So I want to add PD_ to those who dont have this preindex and get rid of _fr.

    Please help....basically rename files in a same directory!!
    Thank you!

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Would you please tell me why you flag this as very easy if you don't know the solution, lol?
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    One incredibly useful utility that I cannot live without is vidir, which allows you to edit a directory in vi. Then doing the above could be done easily with a macro:
    qqiPD_ESC2f_3x0jq

    vidir is part of the moreutils package which contains a bunch of little known but awesome UNIX utilities, such as sponge.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    for i in {1..3};do mv /tmp/1132_"$i"_fr.mp3 /tmp/PD_1132_"$i".mp3;done
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  6. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I don't believe that nmset's solution is correct. The problem is that it only works for files 1 through 3. Furthermore, it only works if the file both does not start with "PD" and ends with "fr".

    With one command, you may be out of luck. However, this is relatively simple:
    Code:
    #!/bin/bash
    
    for file in /path/to/dir/*; do
        base=$(basename "$file")
        newname=$(echo "$base" | sed -re '/^PD_/ ! s/^/PD_/' -e 's/_fr\././')
        mv "$file" "/path/to/dir/$newname"
    done
    What this does is use sed to modify the filename. First we add the PD_ to the beginning if it does not already exist. Then we remove any occurrences of "_fr." and replace them with just a simple "." (essentially removing the "_fr").

    Does this make sense? I hope it helps.

  7. #6
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Quote Originally Posted by Cabhan View Post
    The problem is that it only works for files 1 through 3.
    Well the boundaries are to be set by Drigomaniac.


    Quote Originally Posted by Cabhan View Post
    it only works if the file both does not start with "PD"
    I missed this point, that's true. But yet they all end with _fr.

    Any way, your solution is ubiquitous.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

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