-1

The files may be cleared using cat /dev/null > filename. Is it the same special file that is working whenever cat > filename is done?

sourav c.
  • 46,120

3 Answers3

2

The short answer is No.

These are two different techniques of Clearing a text file using cat /dev/null > file and
cat > file followed by Ctrl + C.

Redirection > is used to redirect output to some file. For example date will show the current time and date on std out

$ date
Sat Jul 25 20:56:09 IST 2015

If you use,

$ date>/path/to/file

Then it will redirect stdout to file.

$ cat /path/to/file
Sat Jul 25 20:56:23 IST 2015

How cat /dev/null>file clears the content of file

It just redirect the content of null device /dev/null to the file. So the contents are overwritten and it becomes empty.

How cat > file clears the content of file

cat Concatenate file(s), or standard input, to standard output. Take the following example,

$ cat
some text          # by user on stdin
some text          # stdin to stdout concatenated by cat
some other text    # by user on stdin
some other text    # stdin to stdout concatenated by cat
^C                 # Ctrl+C to terminate.

The command cat > file will wait for stdin and will redirect and overwrite the file. It need to be stopped using Ctrl + C.

If you do not enter anything into the stdin and terminate the process with SIGINT (Ctrl+C) it will overwrite the file with a blank.

The following example is there for more clarification:

$ cat > file
some text
some other text
^C
$ cat file
some text
some other text
sourav c.
  • 46,120
1

No special file has any useful purpose in either case.

cat /dev/null is exactly a no-op, this command does absolutely nothing of interest and it can be replaced by any command that doesn't output anything.

You can for example clear a file's content with any of these commands:

printf > filename
true > filename
: > filename

or even, with many shells, by using no command at all but the redirection:

> filename

On the other hand:

cat > filename

is redirecting stdin, i.e. everything you type on your keyboard, to filename. If you interrupt the cat command before typing anything, the result would also be an empty file but that is a quite inefficient method to do it.

jlliagre
  • 5,983
0

No, what is clearing the file is not /dev/null, but the > redirection, which intrinsically truncates the output file itself before redirecting the output of cat.

  • cat /dev/null > filename: you're opening /dev/null and printing its "content" (nothing) to stdout; at the same time a file descriptor for filename is opened, the file is truncated and the stdout of cat is redirected to it (hence nothing is actually redirected to the file)
  • cat > filename: you're printing nothing to stdout, because you're not specifying a file and you're not even passing stdin (which has the effect of making cat waiting undefinetly); at the same time a file descriptor for filename is opened, the file is truncated and the stdout of cat is redirected to it (hence nothing is actually redirected to the file)

They do exactly the same thing (in terms of clearing the file) but the first opens an extra file descriptor compared to the second and the second will wait undefinetly for something to come into cat's stdin.

Methods equivalent to the first one (and to the second one result-wise, but without the hassle of the undefinetly waiting cat) would be:

$ echo -n | cat > file # empty pipe
$ <emptyfile cat > file # empty file
$ <<<'' cat > file # empty herestring
$ <<EOF cat > file
>EOF # empty heredoc
kos
  • 41,268