The files may be cleared using
cat /dev/null > filename. Is it the same special file that is working whenever cat > filename is done?
- 44,715
- 21
3 Answers
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
-
1I don't think that's quite right, cat > filename is not using a here-doc. Pretty sure that you only get a heredoc when you use << .... – Stephen Jul 25 '15 at 16:52
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.
- 5,833
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/nulland printing its "content" (nothing) tostdout; at the same time a file descriptor forfilenameis opened, the file is truncated and thestdoutofcatis redirected to it (hence nothing is actually redirected to the file)cat > filename: you're printing nothing tostdout, because you're not specifying a file and you're not even passingstdin(which has the effect of makingcatwaiting undefinetly); at the same time a file descriptor forfilenameis opened, the file is truncated and thestdoutofcatis 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
- 35,891
-
@souravc I don't understand your point, what are you trying to say? It indeed doesn't print anything to
stdout, for a reason or for another. However it doesn't "use heredocs", the point iscat > filenamejust waits until "something happens" (which is not really clear to me about what it is) but it's not restricted to an empty herdoc, you could also use an empty file (<emptyfile cat > out) an empty herestring (<<<"" cat > filename) or an empty pipe (echo -n | cat > filename) – kos Jul 25 '15 at 16:19 -
@souravc Actually thinking about it twice I know what it is: the point is that the
stdinfile descriptor is not opened at all incat > filename, while it is in all the other methods (andstdinalways contains an empty string). – kos Jul 25 '15 at 16:37 -
1yes I got your point. When tested the command I did not found any temporary file associated with the process. If it would be a heredoc it must accompany some temp file. – sourav c. Jul 25 '15 at 18:24
touch filename? – Pilot6 Jul 25 '15 at 17:02touch filenamecreates a 0-length file namedfilenameiffilenameis not present, but iffilenameis present it will just update itsmtimeattribute (last modification time) – kos Jul 25 '15 at 17:09> filenamealso clears the file – jet Jul 25 '15 at 17:52