On 29 Aug, Scott Howard wrote:
> > I know what I want to delete, but when I do rm -f * in the problem
> > directory it comes back with "Argument list too long"
> >
> > Can anyone think of a way around this one?
>
> A number of other options have been given, but this one will work on
> all occasions on all (?) platforms :
>
> find /base/dir/name -type f -print | sed 's/ /\\ /g' | xargs rm -f
Well, no. You'd have to quote a few other characters besides space for
xargs to work. I usually do "find | quote | xargs" where quote is a
script I wrote to do shell quoting of special characters.
But even then it's wrong.
rm -f * won't remove sub-directories or .* files - the find, above,
will. So it's not the same.
I've attached the "quote" shell script & man page if anyone's
interested. I hope they're small enough not to offend people,
bandwidth-wise.
luke
#!/bin/sh
#
# Protect a word from the shell by adding quotes as necessary.
#
# Dependencies: None.
#
# Author: Luke Kendall 21/02/94
#
IFS=" "
MYNAME=`basename $0`
usage="usage: $MYNAME [-csh] [-e] [-f file] [-q str] [rest]...
-csh Protect csh-special characters too.
-e Treat rest of arguments as things to be quoted.
-f Use 'file' as input
-q str 'str' contains the characters to be backslash-quoted;
remember to quote these, if necessary, in your command-line
(to find out how, type them into the command $MYNAME itself!)
Reads from stdin if no other arguments provided."
#
# Process optional arguments.
#
shellquote=true
quotestr='][ |"\*\\&()#?<>`{}\$'
xsyms=
while true
do
[ $# = 0 ] && break
case "$1" in
-e)
shift
break
;;
-q)
[ $# != 2 ] && echo "$usage" && exit 1
quotestr="$2"
shellquote=false
shift 2
;;
-csh)
xsyms="!^~"
shift
;;
-f)
[ $# != 2 ] && echo "$usage" && exit 1
if [ "x$2" = "x-" ]
then
sedf=" "
else
sedf="$2"
fi
shift 2
;;
-*)
echo "$MYNAME: unknown option $1"
echo "$usage" && exit 1
;;
*)
break
;;
esac
done
quotestr="$quotestr$xsyms"
sedstr="s/[$quotestr]/\\\\&/g"
if $shellquote
then
sedstr="$sedstr;s/'/"'"'"'"'"'"/g"
fi
if [ $# = 0 -o "x$sedf" != "x" ]
then
# sed 's/[][ |"\*\\&()#?<>`{}\$"'"$xsyms"']/\\&/g;'"s/'/"'"'"'"'"'"/g" $sedf
sed "$sedstr" $sedf
fi
for a
do
cat <<EOF
$a
EOF
done | sed "$sedstr"
.TH quote 1 21/02/94
.UC 4
.SH NAME
quote \- escape characters which are special to the shell
.SH SYNOPSIS
.B quote [-e] [-csh] [-f file] [rest]...
.SH DESCRIPTION
\fBquote\fP protects words from the shell by adding quotes as necessary.
.PP
The argument
\fB-e\fP means to treat the rest of the arguments as things to be quoted \-
useful if you have no control over the arguments provided.
The argument
\fB-csh\fP adds the special \fBcsh\fP symbols
\fB^~!\fP to the list of characters needing protection.
The argument \fB-f\fP,
followed by a file name (or \fB\(mi\fP) means the list of words is to come
from that file (or \fIstdin\fP).
.PP
\fBquote\fP reads from standard input if no other arguments are provided.
.SH EXAMPLES
.nf
\fB$\fP quote
sd&*'s
\fB^D\fP
sd\e&\e*"'"s
\fB$\fP echo sd\e&\e*"'"s
sd&*'s
.fi
.ti +1c
Which demonstrates what it's all about.
Or:
.sp 0.5
find . -print | quote -e | xargs ls -l
.br
.ti +1c
Which is a more realistic example: a safe way to use \fBxargs\fP.
.SH SEE ALSO
sh(1)
.SH AUTHOR
Luke Kendall