1

command 1: cat file1 > file2 (success)
command 2: file2 < cat file1 (cat : No such file or directory)

I just wanted to know the general syntax of output and input redirections.
If redirection can happen to file or stream, why command 2 fails?

  • command 2 is not valid: file2 is the "executable" and your try to send it cat as input. What do you actual want to do? – cylgalad Feb 23 '18 at 8:27
  • 1
    Also basic shell stuff: > sends output to a file, < uses a file as input. – cylgalad Feb 23 '18 at 8:33
  • @cylgalad Got it now. Please put the second comment as answer. – Prem Feb 23 '18 at 8:37
  • @cylagad The command is not invalid (if file2 is a script), but it's not the same as the first one. – Kusalananda Feb 23 '18 at 9:12
2

The redirection in

cat file1 >file2

is an output redirection, while < specifies an input redirection.

The line

file2 < cat file1

is the same as

file2 file1 <cat

(it does not matter much where the redirection actually occurs as it's handled by the shell in its own parsing step and then removed from the actual command) which means "run file2 with file1 as argument, and redirect the standard input from the file cat".

The error comes from the shell trying to open cat as a file in the current directory. The error occurs before the shell tries to run the command file2.

Related: How is this command legal ? "> file1 < file2 cat"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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