Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
help renaming multiple files
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Angrybob
Guru
Guru


Joined: 19 Apr 2003
Posts: 575

PostPosted: Tue Dec 16, 2003 5:02 pm    Post subject: help renaming multiple files Reply with quote

Hi

I'm trying to rename multiple files so that the first three letters from the original file are replaced with another arbitrary length string. e.g:

change abcdefgh.ijk -> xxxxxdefgh.ijk

also I'd like a script to change underscores into proper spaces.
Back to top
View user's profile Send private message
Voltago
Advocate
Advocate


Joined: 02 Sep 2003
Posts: 2584
Location: userland

PostPosted: Tue Dec 16, 2003 5:30 pm    Post subject: Reply with quote

As for the underscore thingy, here is a little script that should achieve this:
Code:

FLIST=`ls -d $1`
for FILENAME in $FLIST; do
    NEWFILENAME=`echo $FILENAME | sed 's/_/ /g'`
    echo "$FILENAME -> $NEWFILENAME"
    #mv $1/$FILENAME $1/$NEWFILENAME
done

As you notice, the actual renaming action is commented out, please try this first, i haven't tested it. Save it to a file ren.sh and do a
Code:
chmod a+x ren.sh

and execute it with a directory as argument. If you are feeling geeky, you can improve this script with input validation, directory recursion option and so forth. If you are feeling lazy, try to find a batch renaming app, i'm sure there is something in portage. I think i remember reading something about kde krusader having this feature, but i'm not sure...
Your other task should be solvable too by more tinkering with sed, try this ibm tutorial if you want more info:
http://www-106.ibm.com/developerworks/linux/library/l-sed1.html


Last edited by Voltago on Tue Dec 16, 2003 5:42 pm; edited 2 times in total
Back to top
View user's profile Send private message
thodi
Tux's lil' helper
Tux's lil' helper


Joined: 20 Oct 2003
Posts: 92
Location: Emden, Germany (Old Europe)

PostPosted: Tue Dec 16, 2003 5:40 pm    Post subject: Reply with quote

See if mmv does what you need, it's in portage. I've heard good things about krename, too.
_________________
Hier fliegen gleich die Löcher aus dem Käse
Jabber: thodi@jabber.org
Back to top
View user's profile Send private message
Angrybob
Guru
Guru


Joined: 19 Apr 2003
Posts: 575

PostPosted: Tue Dec 16, 2003 10:40 pm    Post subject: Reply with quote

thanks, mmv is just what I needed. Here's how I did it:-

to replace the first three letters with something else I use this:-
Code:
mmv -mv "???*" "something#4"


to replace underscores with whitespace run this a few times until it cant match anymore (bit of a hack but its the first solution that popped into my head :) )
Code:
mmv -mv "*[_]*" "#1 #3"
Back to top
View user's profile Send private message
gentsquash
l33t
l33t


Joined: 03 Nov 2004
Posts: 753
Location: Still a Gentoo beginner.

PostPosted: Sun Apr 17, 2005 8:41 pm    Post subject: Reply with quote

Another method, that gives programmable control, is to use Emacs.
In dired-mode, apply a keyboard macro which used the dired "R"
cmd to rename a file.
_________________
Your thread resolved? Putting [SOLVED] in its title helps all Gentooers. (Button "edit" , first post)
Prof. Jonathan LF King, Mathematics dept., University of Florida
Back to top
View user's profile Send private message
HAL_9000
Tux's lil' helper
Tux's lil' helper


Joined: 29 Sep 2004
Posts: 114
Location: Netherlands

PostPosted: Tue Apr 19, 2005 8:35 am    Post subject: rename Reply with quote

rename <regexp1> <regexp2> <filemask>

Regexp 1 will define what string to replace...
Regexp 2 will define what regexp1 should be replaced with
Filemask > the files you want to rename in this fashion.

or otherwise:
ls | while read file
do
newname=`echo ${file} | <modifications with awk, sed, tr or whatever>`
mv ${file} ${newname}
done

;)
_________________
We are the keepers of the sacred words: Ni peng and Nee wom!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum