Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 14 Aug 1998 15:39:05 +0200
From:      Juergen Nickelsen <ni@tellique.de>
To:        freebsd-questions@FreeBSD.ORG
Subject:   Re: removing files
Message-ID:  <35D43DF9.B607F5D5@tellique.de>
References:  <35D195D9.CD9E1460@graphnet.com> <13777.47045.480782.154919@neuron.webmore.de>

next in thread | previous in thread | raw e-mail | index | archive | help
Malte Lance wrote:

>  > Let's say I have a directory with la.txt and foo.sh and 6 million
>  > other files.
[...]
>  > Can I issue a command that says: delete everything BUT la.txt and
>  > foo.sh
[...]
>   rm -f `/bin/ls /tmp/. | grep -v -e la.txt | grep -v -e foo.sh`

You haven't tried that, have you?

First, your approach fails to delete the files "hola.txt" and
"foo.shut", which, though not mentioned, of course need to be removed as
well.

Second, deleting a large number of files as this one is tricky in
itself. Even "rm *" does not work, since the argument list passed to
exec is too long. ARG_MAX is 64K by default, which is easily reached by
11000 Files with names 6 characters long.

Provided the shell can handle a "*" expansion of 6 million files, it is
easiest to move the two files out of the way and pipe the "*" expansion
to xargs:

mkdir .keep
mv la.txt foo.sh .keep
echo * | xargs rm
mv .keep/* .
rmdir .keep

If the shell can't handle this(*), you'd have to make e. g. a perl
script which does the necessary opendir()/readdir() itself and erases
the files.

(*) I did not want to try it with 6 million files. But then, it is
unwise to put so many files into one directory anyway.

-- 
Juergen Nickelsen <ni@tellique.de>
Tellique Kommunikationstechnik GmbH
Gustav-Meyer-Allee 25, 13355 Berlin, Germany
Tel. +49 30 46307-552 / Fax +49 30 46307-579

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?35D43DF9.B607F5D5>