[oclug] Re: find
Ian! D. Allen
idallen at idallen.ca
Thu Apr 22 21:05:05 EDT 2004
On Thu, Apr 22, 2004 at 06:10:18PM -0400, Bob Lockie wrote:
> I need the file names but not the content.
Many of the sample scripts people have been posting do not handle file
names with blanks or GLOB characters in them, or directories containing
things other than files. The scripts also don't handle hidden names.
Most solutions put a newline into every file instead of emptying the file.
Here's a Bourne shell answer, simliar to the best one posted so far and
with the same limitations, that presumes you want empty files instead
of files with newlines in them:
for f in * ; do
(Bourne shells don't require command names to do redirection.) The above
is quite quick, since it's the Bourne shell emptying each file; no
additional process needs to be created. The loop makes assumptions:
hidden pathnames are missed, as are pathnames in sub-directories, and
every pathname matched by "*" must be a file (not a directory); but, it
does work fine on names containing blanks or other special characters.
If you want a real solution that handles pathnames with arbitrary
characters in their names (including blanks, TABs, newlines, and GLOB
characters), handles hidden files (leading period), and works only on the
files (not on symlinks, directories, pipes, special files, etc.) under
the current directory, you use something like this:
find . -type f -print0 | xargs -0 -n1 cp /dev/null
The above descends into all directories. If you don't want the descent,
add "-maxdepth 1" to the "find". The above pipeline is slow, since
it executes one "cp" command for every file found; but, it makes no
assumptions about names, finds even hidden names, and has no limitations.
Add a "-name" clause to "find" to alter what files are emptied.
Beware of scripts (and script writers) that make many assumptions.
They will bite you some day. With Unix/Mac/Windows inter-operating,
file names do have many strange (to Unix) characters in them.
-IAN! Ian! D. Allen Ottawa, Ontario, Canada
EMail: idallen at idallen.ca WWW: http://www.idallen.com/
College professor via: http://teaching.idallen.com/
Electronic Freedom Foundation supporter: http://eff.org/
More information about the OCLUG