Mailinglist Archive: opensuse (2459 mails)

< Previous Next >
Re: [opensuse] /bin/rm: Argument list too long
  • From: "Scott Simpson" <simpson100@xxxxxxxxx>
  • Date: Thu, 27 Mar 2008 15:58:43 -0700
  • Message-id: <79759ec90803271558n634de54by1f6626edc49dd269@xxxxxxxxxxxxxx>
> How can I delete these files w/o removing the folder itself?

ls -1 .thumbnails/large/* | while read f; do rm $f; done

This won't work. The problem is the * which the shell expands BEFORE
the command is called. There is a limit on the number of characters
that can be passed to execv(2). That is xargs(1) purpose: to do the
minimum number of forks while staying within execv(2) limits.

Is this an exercise? What's wrong with "rm -rf .thumbnails/large"
followed by "mkdir .thumbnails/large"?

What's wrong with it is that is may not regenerate the directory with
the same ownership or permissions.



The original owner also didn't say whether they wanted the files
beginning with a period deleted or the files deleted recursively. The
original poster said "rm .thumbnails/large/*" and just to be perverse,
I'm going to take him at his literal word. So in that case, no period
files and no recursion. Here is the incantation:

(cd .thumbnails/large; find . -maxdepth 1 -type f ! -regex '^\./\..*'
-print0 | xargs --null rm -f)

The reason that the () are used is that it is executed in a subshell
(the Bash shell forks itself, runs the commands, then exits) and
running in a subshell leaves you in the same directory as you started.
The reason that "-maxdepth 1 is used is to stop recursion. "-type f"
prevents looking at directories. "! -regex '^\./\..*'" says to not
match files that begin with "./." (i.e., dot files).
The reason that "-print0" and "--null" are used is to allow file names
with spaces in them to be deleted.
The reason that "-f" is passed to "rm" is so files without the write
bit on are also removed (assuming you have permission).

I often using xargs with etags. Witness,

rm TAGS; find . -name '*.[hc]' -print0 | xargs --null etags -a

for Emacs tags.
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >