[NTLUG:Discuss] How do I make all files in a directory of zero length?

Richard Cobbe cobbe at directlink.net
Wed Jun 6 20:12:51 CDT 2001


Lo, on Wednesday, June 6, MadHat did write:

> At 11:12 AM 6/6/2001 -0500, you wrote:
> >I want to go into a directory and make all the files of zero
> >length. What's the easiest way to do this?
> >
> >Do I need to save the filenames, delete the files, and then
> >touch the saved filenames?
> >
> >Is there a more direct way to achieve the result I'm looking for?
> 
> cat /dev/null > *
> 
> should work, but be careful.

Doesn't work with bash:

[minbar:~/foo]$ cat /dev/null > *
bash: *: ambiguous redirect

I'd use something like (assuming bash)

for a in * ; do
    cat /dev/null >| "$a"
done

This is basically George's suggestion, with three differences:
 1) you can type a for loop at the prompt, so you don't need to create (and
    check for) a shell script
 2) the | after the > allows you to write on top of existing files, even if
    you've `set -o noclobber', as I have.
 3) Putting quotes around the variable reference protects against funky
    characters in filenames.

The above is probably the most straightforward way, but there are others:
    for a in * ; do >| "$a" ; done
and so forth.

If you want to get dotfiles, use `for a in * .*' and ignore the errors
about . and .., which are both directories.

MadHat is correct---be careful!

Richard



More information about the Discuss mailing list