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
...
- 02-18-2010 #1Just Joined!
- Join Date
- Feb 2010
- Posts
- 6
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!
- 02-18-2010 #2
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.
- 02-18-2010 #3
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.
- 02-19-2010 #4Linux 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;done0 + 1 = 1 != 2 <> 3 != 4 ...
Until the camel can pass though the eye of the needle.
- 02-19-2010 #5
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:
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").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
Does this make sense? I hope it helps.DISTRO=Arch
Registered Linux User #388732
- 02-19-2010 #6Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292


Reply With Quote
