Re: How do I remove filenames with spaces from a shellscript.
From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 09/28/04
- Next message: Seb: "Re: find and replace strange characters"
- Previous message: Michael Tosch: "Re: How do I remove filenames with spaces from a shellscript."
- In reply to: Michael Tosch: "Re: How do I remove filenames with spaces from a shellscript."
- Next in thread: William Park: "Re: How do I remove filenames with spaces from a shellscript."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Sep 2004 07:18:30 GMT
On 2004-09-28, Michael Tosch wrote:
> In article <slrnclgv9t.4ps.cchq_nospam@warpcore.positronic.net>, Maxim Heijndijk <cchq_nospam@wanadoo.nl> writes:
>> #!/bin/bash
>>
>> echo "Removing TRANS.TBL files..."
>>
>> FILES_1="`find ./ -name 'TRANS.TBL' -print`"
>> FILES_2="`find ./ -name 'trans.tbl' -print`"
>>
>> for FILE in ${FILES_1} ${FILES_2}; do
>>
>> rm -v "${FILE}"
>>
>> done
>>
>> exit 0
>>
>>
>> The above script cannot handle filenames with spaces, the for loop sees every string
>> between spaces as a separate file:
>>
>> rm: cannot remove `./Software/Office_XP_2003/OUTLOOK': No such file or directory
>> rm: cannot remove `WITH': No such file or directory
>> rm: cannot remove `BCM/X86/TRANS.TBL': No such file or directory
>>
>> How do I fix this ?
>>
>> --
>> Best regards, M@X.
>> * Climate Control Psychedelic Soundscapes - http://go.to/cchq/
>> * Linux Shell Scripts & RPM Software Packages - http://go.to/conmen/
>> * Photography Pages - http://home.wanadoo.nl/cchq/photo/photo.html
>
> Replace lists (``-expressions and for loops) by a pipe to a while loop:
>
> #!/bin/sh
> # open a sub shell to collect and redirect stdout
Why a subshell? What's wrong with a braced list?
> (
{
> find . -name 'TRANS.TBL' -print
> find . -name 'trans.tbl' -print
> ) |
} |
> while read file
> do
> rm -v "${file}"
The -v option is far from universal:
$ rm -v "${file}"
usage: rm [-rif] file ...
> done
Since the OP is using bash, a good way is to use arrays:
IFS=$'\n'
files=(
`find . -name '[Tt][Rr][Aa][Nn][Ss].[Tt][Bb][Ll]' -print`
)
rm "${files[@]}"
> There is a convention to use lowercase letters for shell variables,
> and uppercase letters for environment variables.
>
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
===================================================================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
- Next message: Seb: "Re: find and replace strange characters"
- Previous message: Michael Tosch: "Re: How do I remove filenames with spaces from a shellscript."
- In reply to: Michael Tosch: "Re: How do I remove filenames with spaces from a shellscript."
- Next in thread: William Park: "Re: How do I remove filenames with spaces from a shellscript."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|