PDA

View Full Version : rename files using path to file as new filename



monkeyking
July 24th, 2009, 08:52 AM
HI igot alot of files like



path1/res/file.txt
path2/res/file.txt
path3/res/file.txt


Is there some easy way of renaming these to



path1.file.txt
path2.file.txt
path3.file.txt


thanks

ghostdog74
July 24th, 2009, 09:25 AM
i will assume those are in a file, not the actual file names, because "/" is illegal for a file name.




awk '{gsub("/","."}1 ' file

smartbei
July 24th, 2009, 10:06 AM
After you rename them, where do you want them to be?

@ghostdog - the /is in the file path.

monkeyking
July 24th, 2009, 12:07 PM
Well actually I want to move them,
to a different folder.

so what I want to do is a command like




mv path1/res/file.txt /out/path1.file.txt
etc

monkeyking
July 25th, 2009, 03:02 AM
I ended up doing something like



for i in $(ls /path/to/*.txt); do
bas=`basename $i`
pre=`ls $i |cut -f2 -d'/'`
newnams=$pre.$bas
mv $i $newnams

done



this uses the second dirname as prefix for the files