🎓 A1Learner
🔍
0 of 50 lessons completed

Input Redirection

Input redirection feeds a file to a command. Standard input is where a command reads from.

Count a file from input

wc < data/numbers.txt counts the file. The counts print with no file name.

$ cd ~/practice
$ wc < data/numbers.txt

Sort a file from input

sort < data/numbers.txt prints the numbers in order.

$ cd ~/practice
$ sort < data/numbers.txt

With and without <

wc with a file name prints the name. wc with < prints the same counts and no name.

$ cd ~/practice
$ wc data/numbers.txt
$ wc < data/numbers.txt

Example

$ cd ~/practice
$ grep INFO < data/logs.txt
$ wc < data/logs.txt

Key points

  • Use < to feed a file to a command as standard input.
  • With <, the command reads the file and prints no file name.
  • sort, wc, and grep can all read from standard input.
  • < points the other way from >. It feeds a file in as input.

In this example, grep reads the log file from standard input. It prints the two INFO lines. wc counts the same input as 5 lines, 21 words, and 170 bytes.

Note

< never changes the file. It only reads it.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does the less-than sign do?