| Author |
How do I remove filenames with spaces from a shellscript.
|
|
| Maxim Heijndijk 2004-09-28, 3: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 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
| |
| Paul Jarc 2004-09-28, 3: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
| |
| Paul Jarc 2004-09-28, 3: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
| |
| Maxim Heijndijk 2004-09-28, 3: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
| |
| Paul Jarc 2004-09-28, 3: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
| |
| Chris F.A. Johnson 2004-09-28, 3: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
| |
| Michael Tosch 2004-09-28, 3:32 am |
| 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
(
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
| |
| Chris F.A. Johnson 2004-09-28, 3:32 am |
| On 2004-09-28, Michael Tosch wrote:
> In article <slrnclgv9t.4ps.cchq_nospam@warpcore.positronic.net>, Maxim Heijndijk <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
| |
| William Park 2004-09-28, 5: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 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 ?
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
| |
| Paul Jarc 2004-09-28, 5: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
| |
| William Park 2004-09-28, 5:56 pm |
| Paul Jarc <prj@po.cwru.edu> wrote:
> William Park <opengeometry@yahoo.ca> wrote:
>
> 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
Would 'read -r' help in this matter?
--
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
| |
| Paul Jarc 2004-09-28, 5:56 pm |
| William Park <opengeometry@yahoo.ca> wrote:
> Paul Jarc <prj@po.cwru.edu> wrote:
>
> Would 'read -r' help in this matter?
Yes, as long as it doesn't need to work with older shells like
Solaris's /bin/sh.
paul
| |
| rakesh sharma 2004-09-29, 8:09 pm |
| Maxim Heijndijk <cchq_nospam@wanadoo.nl> wrote in message news:
>
> On Mon, 27 Sep 2004 17:00:03 -0400, Paul Jarc <prj@po.cwru.edu> wrote:
>
> 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.
>
Why should that give an error? If there are no files whose basenames
are either 'TRANS.TBL' or 'trans.tbl' then the -exec rm portion would
never be reached and so I don't understand why do you see that error??
You can tighten it by ANDing with '-type f' clause to look for files only:
/bin/find . -type f -name '[tT][rR][aA][nN][sS].[tT][bB][lL]' \
-exec rm {} \;
| |
| vijay_chulaki 2004-10-09, 1:47 am |
| ====================
#!/bin/sh
for file in * ; do
rm -f $file
done
exit 0
*******************
hope will do,
vijay
************************************** |
|
|
|