Tuesday Tiny Techie Tip

Creating files

Sometimes you need to create a file, but it's too much trouble to fire up an editor to do it. Here are some less obvious ways to create files without an editor (or cp(1)).
You may be familiar with the touch command as a way to make a file look as if it were modified today without actually modifying it, but it also has the convenient side effect of creating the specified file if it does not already exist:
% ls -l foo
foo not found
% touch foo
% ls -l foo
-rw-r--r--  1 jeffy           0 Feb 17 14:01 foo

touch is only useful if all you need is an empty file. If you need to get some stuff into the file it's not very helpful.

And you thought cat was just for looking at files.

Suppose you have some stuff on the screen that you'd like to get into a file. Try this:

% cat > foo
[copy stuff and paste it here]
% ls -l foo
-rw-r--r--  1 jeffy         136 Feb 17 14:05 foo

This works since if cat isn't given any arguments it takes it's input from stdin.* And since you've redirected stdout to your new file, whatever you type (or paste) on stdin shows up in that file. To tell cat that it has reached the end of the input, you have to type a Ctrl-D.

So you're really using the shell and redirection to create the file, and cat just as the funnel through which to pour the desired contents.

*Ever tried running just cat all by itself and then typing at it? It's like talking to an annoying younger sibling.

We just did this with cat, but you can always create a file by redirecting the output of a command to it.
% ypcat -k aliases | grep '^rich ' > foo
% cat foo
rich rich@EXUSER.

Boring but useful.

Tuesday Tiny Techie Tip -- 18 February 1997
Forward to (02/25/97)
Back to (02/11/97)
Written by Jeff Youngstrom

Up to the TTTT index

Tuesday Tiny Techie Tips are all © Copyright 1996-1997 by Jeff Youngstrom. Please ask permission before reproducing any of this material.