2013年9月6日星期五

File Rename problem

how to achieve the following functions in the shell
to a folder of all files that match the rule according to my rules required to modify the file name
I am using ls and grep access and filter files , and then use sed to modify the original file name , but how to achieve modify the file name ah , I mean how to get to the front of the parameters passed to rename or mv, the key is the corresponding , for example, I put 123.txt modified to abc_123.txt, how do I put in a batch rename 123.txt and abc_123.txt pass or mv?

------ Solution ----------- ---------------------------------
for i in `ls`
do
mv $ i `echo $ i | sed 's / ^ / abc_ /'`
done
like this do
------ Solution --------------------------------- -----------
#!/bin/sh

ls | grep ".txt" | while read oldname;
do
newname=`echo "$oldname" | sed -e 's/^\([^\.]*\)\.txt$/abc_\1.txt/'`
echo "mv $oldname $newname"
done


------ For reference only ---------------------------------------
you in two steps , one step removed original file name , a step of removing the modified file.
then put two parameters , the direct call mv.
------ For reference only -------------------------------------- -


I just want to ask , how to take ?
------ For reference only -------------------------------------- -


good idea , taught , thank you !
------ For reference only -------------------------------------- -


learning , thank you !
------ For reference only -------------------------------------- -
good , learning .

没有评论:

发表评论