[buug] Unix script for replacing character in file or folder name?

shiro at uclink4.berkeley.edu shiro at uclink4.berkeley.edu
Wed May 25 19:37:35 PDT 2005


To add another example:

At least on a recent linux, you may have the native command "rename" that does what you want. (Check the man page and test it out first to make sure it's not something else with the same name!)  If so, you could use for example:

find . -print0 |xargs -0 rename ':' '_'  

The -print0 and -0 use null characters instead of new lines to indicate breaks between filenames and keep xargs from choking on badly named files.  It only replaces the first instance in a name, so you might have to run it repeatedly.

But Robert's version is probably better: you could easily match regular expressions instead of just straight replacement, it will replace all instances in a filename, and it will work anywhere.

Best,
Erik

On Wed, May 25, 2005 at 06:46:22PM -0700, Robert Helmer wrote:
> 
> -----8<-----
> 
> #!/bin/sh
> 
> REPLACE_CHAR=":"
> 
> for FILE in `find ./ -type f`
> do
>   echo $FILE | grep $REPLACE_CHAR > /dev/null
>   if [ "$?" == "0" ]
>   then
>     mv $FILE `echo $FILE | sed s/:/-/g`
>   fi
> done
> 
> ----->8-----
> 
> 
> 
> On Wed, May 25, 2005 at 08:33:53PM -0500, Gary Mielke wrote:
> > I was wondering if anybody has a unix shell script that would replace a
> > specific character in a file or folder. I would like the script to look into
> > one directory and search all the sub folders and files and if it finds a ":"
> > colon character in any of the names that it would replace it with a "_"
> > underscore. Has anybody worked with a similar script that they would be
> > willing to share?
> > 
> > Thanks for any help or suggestions.
> > 
> > Gary 
> > 
> > _______________________________________________
> > Buug mailing list
> > Buug at weak.org
> > http://www.weak.org/mailman/listinfo/buug
> > 
> _______________________________________________
> Buug mailing list
> Buug at weak.org
> http://www.weak.org/mailman/listinfo/buug








More information about the buug mailing list