LinuxCommandLibrary

less-than

View file content, one page at a time

TLDR

Redirect a file to stdin (achieves the same effect as cat file.txt |)

$ [command] < [path/to/file.txt]
copy

Create a here document and pass that into stdin (requires a multiline command)
$ [command] << [EOF] <Enter> [multiline_data] <Enter> [EOF]
copy

Create a here string and pass that into stdin (achieves the same effect as echo string |)
$ [command] <<< [string]
copy

Process data from a file and write the output to another file
$ [command] < [path/to/file.txt] > [path/to/file2.txt]
copy

Write a here document into a file
$ cat << [EOF] > [path/to/file.txt] <Enter> [multiline_data] <Enter> [EOF]
copy

Disregard leading tabs (good for scripts with indentation but does not work for spaces)
$ cat <<- [EOF] > [path/to/file.txt] <Enter> [multiline_data] <Enter> [EOF]
copy

SYNOPSIS

command < filename

DESCRIPTION

The less-than sign ('<') in Linux is a redirection operator. It's not a command in itself, but rather a fundamental part of the shell's syntax for controlling the flow of data. It redirects the standard input (stdin) of a command to come from a specified file instead of the keyboard. This allows programs that typically expect user input to read data from a file, enabling batch processing, automated tasks, and simplified data handling. The shell interprets '< filename' to mean 'take the contents of filename and feed it as input to the command preceding the '<''.
The command following '<' is executed using the file specified as input to it.
For example, `command < input.txt` will execute `command` with the contents of `input.txt` as its standard input. Essentially, the less-than sign allows you to 'inject' the contents of a file into a command, simulating user input.

CAVEATS

The file specified after '<' must exist and be readable by the user executing the command. If the file does not exist or is inaccessible, the shell will usually report an error. While the '<' operator redirects standard input, standard output and standard error are not affected and will still typically be displayed on the terminal unless redirected separately.

EXAMPLES

  • `cat < file.txt` : Displays the content of `file.txt` on the terminal. Equivalent to `cat file.txt`
  • `mail user@example.com < message.txt` : Sends an email to user@example.com, using the content of `message.txt` as the email body.
  • `wc -l < data.txt` : Counts the number of lines in `data.txt` and displays the result.

ERROR HANDLING

If the specified file doesn't exist, the shell will usually output an error message like `bash: file.txt: No such file or directory` before attempting to execute the command.

HISTORY

Input redirection has been a part of Unix-like operating systems since their inception. It's a core mechanism for separating programs from specific input sources, promoting modularity and reusability.

SEE ALSO

>(1), >>(1), |(1), tee(1)

Copied to clipboard