[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Change filenames recursively



On 31Jan2007 13:38, Stephen Carville <stephen totalflood com> wrote:
| Matthew Anderson wrote:
| >I have hundreds of files that start with a * (asterisk) for the
| >filename.  I am needing to change the asterisk in every file to a _
| >(underscore).  Does
| >
| >Anyone know what command I can use to recursively do that?  I appreciate
| >any help anyone can provide me.
| 
| Check out the 'find' command to locate the files.  Then pipe that that 
| thru a bit of Perl to create the commands.
| 
| $ ls test
| *one  *three  *two
| 
| $ find test -name "\**"|perl -e 
| 'foreach(<>){chomp$_;$aa=$_;$aa=~s/\*/_/;print"mv $_ $aa\n";}
| 
| mv test/*two test/_two
| mv test/*one test/_one
| mv test/*three test/_three
| 
| pipe to a shell for execution...

DON'T!

You need to escape the asterisk before doing this. And if the files have
funny chars you need to do more. A simplistic first cut would be to
generate commands like this:

  mv 'test/*two' 'test/_two'

Not perfect, but much much safer.

Then think about the consequences of renaming a directory during the
process (which your script will try to do as written).
-- 
Cameron Simpson <cs zip com au> DoD#743
http://www.cskk.ezoshosting.com/cs/

BROCCOLI!! THE ONLY VEGETABLE THAT SOUNDS LIKE AN ADVERB!!
        - ken aiai ed ac uk (Ken Johnson)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]