How do I remove filenames with spaces from a shellscript.
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > How do I remove filenames with spaces from a shellscript.




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    How do I remove filenames with spaces from a shellscript.  
Maxim Heijndijk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

#!/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 ever
y string
between spaces as a separate file:

rm: cannot remove `./Software/Office_XP_2003/OUTLOOK': No such file or direc
tory
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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

Maxim Heijndijk <cchq_nospam@wanadoo.nl> wrote:
> The above script cannot handle filenames with spaces, the for loop
> sees every string between spaces as a separate file:

Simpler:
find ./ \( -name TRANS.TBL -or -name trans.tbl \) -exec rm -v '{}' \;

Faster, but requires GNU tools:
find ./ \( -name TRANS.TBL -or -name trans.tbl \) -print0 | xargs -0 rm -v


paul





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

I wrote:
> find ./ \( -name TRANS.TBL -or -name trans.tbl \) -exec rm -v '{}' \;

Oops - make that -o instead of -or for wider portability.


paul





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

On Mon, 27 Sep 2004 17:00:03 -0400, Paul Jarc <prj@po.cwru.edu> wrote:
> I wrote: 
>
> Oops - make that -o instead of -or for wider portability.
>
>
> paul

OK thanx. But this will give an error when there are no *.TBL files:

Removing TRANS.TBL files...
rm: too few arguments
Try `rm --help' for more information.

--
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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

Maxim Heijndijk <cchq_nospam@wanadoo.nl> wrote:
> OK thanx. But this will give an error when there are no *.TBL files:

Ah - the find|xargs method will do that, but (at least with GNU xargs)
you can use -r to prevent that.  The find -exec method shouldn't run
rm unless it finds some files.


paul





[ Post a follow-up to this message ]



    Re: How do I remove filenames with spaces from a shellscript.  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

On 2004-09-27, Maxim Heijndijk wrote:
> #!/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

rmfile() {
for file in TRANS.TBL trans.tbl
do
[ -f "$file" ] && rm "$file"
done
}

recurse()
{
for dir in */
do
(
[ -d "$dir" ] && cd "$dir" || continue
rmfile
recurse
)
done
}

rmfile
recurse

--
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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

In article <slrnclgv9t.4ps.cchq_nospam@warpcore.positronic.net>, Maxim Heijndijk <cchq_nospa
m@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 ev
ery string
> between spaces as a separate file:
>
> rm: cannot remove `./Software/Office_XP_2003/OUTLOOK': No such file or dir
ectory
> 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
(
find . -name 'TRANS.TBL' -print
find . -name 'trans.tbl' -print
) |
while read file
do
rm -v "${file}"
done


There is a convention to use lowercase letters for shell variables,
and uppercase letters for environment variables.

--
Michael Tosch
IT Specialist
HP Managed Services
Technology Solutions Group
Hewlett-Packard GmbH
Phone: +49 2407 575 313
Mail: michael.tosch:hp.com







[ Post a follow-up to this message ]



    Re: How do I remove filenames with spaces from a shellscript.  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 08:32 AM

On 2004-09-28, Michael Tosch wrote:
> In article <slrnclgv9t.4ps.cchq_nospam@warpcore.positronic.net>, Maxim Hei
jndijk <cchq_nospam@wanadoo.nl> writes: 
>
> 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





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 10:56 PM

Maxim Heijndijk <cchq_nospam@wanadoo.nl> wrote:
> #!/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 ev
ery string
> between spaces as a separate file:
>
> rm: cannot remove `./Software/Office_XP_2003/OUTLOOK': No such file or dir
ectory
> 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 ?

Assuming there is no leading or tailing spaces,
find ./ -iname trans.tbl | while read f; do
rm "$f"
done

If there is, then play around with 'find -print0' and 'xargs -0 -n 1'.

--
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada





[ Post a follow-up to this message ]



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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-28-04 10:56 PM

William Park <opengeometry@yahoo.ca> wrote:
> Assuming there is no leading or tailing spaces,
>     find ./ -iname trans.tbl | while read f; do
> 	rm "$f"
>     done

That can also break if any of the filenames contain backslashes.
$ echo 'foo\\bar'
foo\\bar
$ echo 'foo\\bar' | { read line; printf %s\\n "$line"; }
foo\bar


paul





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:01 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register