Re: How do I remove filenames with spaces from a shellscript.

From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 09/28/04


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


Relevant Pages

  • Re: sort files by exif date
    ... >> and the filenames in a list, sort, then extract the filenames again. ... >> Probably a few lines of shell script. ... That string will be a line of text. ...
    (comp.unix.questions)
  • Re: Getting a list of files in a folder
    ... >them into spreadsheets in a folder. ... >Currently I have a table with a list of filenames. ... I loop through the ... >a table, array, or text string that I can loop through ...
    (microsoft.public.access.forms)
  • Re: Variables not passed out of WHILE
    ... that values of a string are not passed out of a WHILE ... loop. ... My script will not ECHO the value of $var at the end. ...
    (comp.unix.shell)
  • Re: Logical problem with small script??
    ... script and finding it very useful. ... >> Wrote it to convert a string using a ceaser cipher for a course I am ... > That eliminates the warnings message and makes the code shorter and easier ... > to read but you can do the same thing without the foreach loop and the $count ...
    (perl.beginners)
  • Re: How do I remove filenames with spaces from a shellscript.
    ... > The above script cannot handle filenames with spaces, the for loop sees every string ...
    (comp.unix.shell)