[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Suggestions for rm [WAS: Re: Feature request: install package by passing URL to apt-get]



On Fri, Jun 24, 2022 at 07:42:25PM -1000, Joel Roth wrote:
> I list the files first:
> 
> ls some-pattern
> 
> then add a pipe to rm:
> 
> ls some-pattern | rm 
> 
> or
> 
> ls some-pattern | rm -rf

Those commands do not work.  rm does not read a list of files from stdin.

Even if you were to add xargs, those commands still would not work in
all cases.  They would only work in the simplest cases, where none of
the filenames contain whitespace, single-quote characters, or double-quote
characters.

That's because xargs does not split its input on newlines.  It splits
its input on "quoted words".  Like this:

unicorn:~$ echo 'a list of "quoted words"' | xargs printf '<%s>\n'
<a>
<list>
<of>
<quoted words>

Any unbalanced single or double quote will cause an error:

unicorn:~$ echo "I don't know.mp3" | xargs printf '<%s>\n'
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
<I>

And even if xargs *did* have some option to split its input only on
newlines, a filename that *contains* a newline would still break it.

Finally, ls does not always reproduce filenames exactly.  There are
some systems (I'm not sure about all versions of Debian, but definitely
some Unix systems) where certain characters will be printed as
question marks by ls.  That means any file containing one of those
characters would make the ls|xargs rm construct fail.

If you want to use ls as a preview for what will be removed, that's
perfectly fine.  You just can't use "up arrow | xargs rm" as your
follow-up command.  Instead, use "up arrow" and then use command editing
to replace the ls with rm.


Reply to: