Assuming file2 already exists, the command

> file1 < file2 cat

appears to be copying the content of file2 to file1.

But I cannot understand this structure.

I understand that "Nothing" is being directed to file1, (creating or erasing its content) Then the content of file2 is being directed to file1.

Why is "cat" after file2? how does it know to "cat file2" if the operands are not in the correct order?

share|improve this question
    
> is not nothing, it's output – Dmitry Grigoryev 19 hours ago
6  
What "correct order"? Can you provide a link to some resource / learning material where a "correct order" is described? – BoundaryImposition 16 hours ago
3  
When asking questions like this, you should specify the shell; different ones have different edge cases, especially around redirection. – chrylis 10 hours ago
up vote 54 down vote accepted

Before the shell executes the cat command on the command line, it looks for redirections.

There are two redirections:

  1. >file1 This will make the command's standard output go to file1.
  2. <file2 This will make the command's standard input come from file2.

The fact that these redirections are placed in a wonky location on the command line doesn't matter.

$ >file1 cat <file2

is the same as

$ cat <file2 >file1

which is the same as

$ <file2 >file1 cat

etc.

Note that the cat utility in all of these instances is executed without any operands. The redirections are not operands to the cat command, they are instructions to the shell to set up redirections into and out of the command (connecting its standard input and output to files).

The difference between cat file and cat <file (or, if you will, <file cat) is that in the first case, the cat utility itself is opening the file, which is given as an operand on the command line, for reading, while in the second case, the shell will open the file and connect cat's input stream to it1. In the second case, cat will notice it wasn't given a file operand and will automatically switch to reading from its standard input (this is a feature of cat, not something that all utilities do).

1 See also the question "cat gives different error when opening non-existing file".


The fact that the shell sets up the redirections before even executing the command on the command line is why things like these fail and you end up with an empty output file:

$ sort file >file

Here, the shell will truncate (empty) the file file before executing sort file and connecting sort's standard output to the file. The sort utility will then open file and sort its contents (which is nothing). The result (nothing) is passed through the standard output stream to file.

share|improve this answer
10  
@Steve The freedom to move redirections around can be used to make some commands clearer, like a pipeline with input redirection at the beginning <in foo | bar >out puts everything in logical order. I'm also fond if echo >&2 Something bad happened for error output from shell scripts. But >out <in cat is just obfuscation – Wumpus Q. Wumbley 19 hours ago
1  
@Wossname The most common issue with permissions and redirections is probably conjunction with sudo, e.g. sudo grep something file1 >file2 when in a directory only writable by root. The redirection fails even before the sudo command is invoked. – Kusalananda 19 hours ago
1  
@Wossname Whether the shell or the utility opens a file is less of a performance issue in general. Repeatedly appending to an output file is slow if it requires the shell (or the utility) to re-open the file for each additional bit of data. In those cases it's better to open the file for appending with >> once and then iterate over the utility. This is most often done by placing >>outfile after the done in the loop, rather than with the invocation of the utility. But, as I said, the difference between letting the utility or the shell open a file is, I assume, minimal. – Kusalananda 19 hours ago
2  
How might sort file >file be correctly accomplished? – setht 18 hours ago
6  
@setht With sort -o file file. – Kusalananda 18 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.