[ale] Removing blanks

Björn Gustafsson bg-ale at bjorng.net
Fri Jan 22 12:51:16 EST 2010


The hard part is making a list that gets interpreted correctly, i.e.
doesn't split on the spaces in the filename.

One way you could do it is like this:

ls *\ * | xargs -i bash -c 'file="{}"; mv "$file" ${file// /_}'

Note that the number of slashes are significant in the variable
substitution.  If you only put one slash after file it will only
replace the first space in the variable name.

You could also do it like this without a pipeline:

(IFS=$'\n\t'; for file in `ls -d *\ *`; do mv $file ${file// /_}; done)

Note that you want to keep the parens around that statement to prevent
your shell's IFS from getting messed up.

If you want to convert a tree where directories also have spaces in
their names, this becomes more complex.

On Fri, Jan 22, 2010 at 12:26 PM, Terry Bailey <terry at bitlinx.com> wrote:
> I don't want to change any characters in a file.  I want to change
> each " " to a "_" within the file name itself.
>
> At 11:40 AM 1/22/2010, you wrote:
>>Terry Bailey wrote:
>> > Hi,
>> >
>> > Does anyone know how to use either a command or batch file to change
>> > all the blanks in a file name to another character?
>> >
>> > I thought maybe sed would work, but if so, I have not been able to
>> > figure it out.
>>
>>cat filename |tr " " "$NEWCHARACTER"
>>
>> >
>> > Thanks,
>> >
>> > Terry Bailey

-- 
Björn Gustafsson



More information about the Ale mailing list