[plug] Renaming files

Bernard Blackham bernard at blackham.com.au
Sat Apr 6 12:57:29 WST 2002


On Fri, Apr 05, 2002 at 08:07:44PM +0800, Paul Dean wrote:
> I would like to rename a large number of files by stipping of the first
> 35 chars.
> 
> Short of writing a script to do this, is there a way it can be done from
> the console using find/ls/cut etc, I know it can be done with sed/awk
> but I've not had a lot of success with them (lack off experience)
> 
> So if someone could point me in the right direction would be much
> appreciated.

Something along the lines of:

for i in * ; do
mv "$i" "`echo $i | cut -c 5-`"
done

should do said trick. General layout for working on large quantities
of files (I use): 

for filename in * ; do

.... do what you want on a file called $filename (or whatever you
called your variable above....

done

Also a good rule of thumb to quote the variable (eg, "$filename") to
preserve spaces. Note that this won't operate on files starting with
a period (eg, .bashrc). To do that, find(1) is your friend:

find -exec script_to_do_stuff {} ';'

the {} is replaced with the filename, and the ; on the end tells
the find command that it is the end of your command (and it needs to
be quoted as ';' so that the shell doesnt touch it). Read the man
page for more funky stuff that can be done.

HTH,

Bernard.

-- 
 Bernard Blackham
 bernard at blackham.com.au



More information about the plug mailing list