Input Output Redirection in Linux/Unix is a powerful feature that allows you to manipulate the input and output of commands and scripts. In this tutorial, we will cover the basics of input/output redirection, as well as some practical examples.
- Redirecting Output
To redirect output from a command, you can use the ‘>’ symbol followed by the name of the file you want to write to. For example, the command ‘ls > file.txt’ will write the output of the ‘ls’ command to a file named ‘file.txt’ in the current directory.
Here’s an example:
$ ls > file.txt
This command will list the files and directories in the current directory and write the output to a file named ‘file.txt’.
- Appending Output
If you want to append output to an existing file rather than overwrite it, you can use ‘>>’ instead of ‘>’. For example, the command ‘ls >> file.txt’ will append the output of the ‘ls’ command to the end of the ‘file.txt’ file.
Here’s an example:
$ ls >> file.txt
This command will append the output of the ‘ls’ command to the end of the ‘file.txt’ file.
- Redirecting Input
To redirect input to a command from a file, you can use the ‘<‘ symbol followed by the name of the file. For example, the command ‘sort < file.txt’ will sort the contents of the ‘file.txt’ file.
Here’s an example:
$ sort < file.txt
This command will sort the contents of the ‘file.txt’ file and display the sorted output in the terminal.
- Combining Input and Output Redirection
You can also combine input and output redirection to read from one file and write to another. For example, the command ‘sort < file1.txt > file2.txt’ will sort the contents of ‘file1.txt’ and write the sorted output to ‘file2.txt’.
Here’s an example:
$ sort < file1.txt > file2.txt
This command will sort the contents of ‘file1.txt’ and write the sorted output to ‘file2.txt’.
- Using Pipes
Pipes are another powerful feature in Linux/Unix that allow you to send the output of one command to the input of another command. To use a pipe, you can use the ‘|’ symbol between two commands. For example, the command ‘ls | grep file’ will list the files and directories in the current directory and then filter the output to only show files that contain the word ‘file’.
Here’s an example:
$ ls | grep file
This command will list the files and directories in the current directory and filter the output to only show files that contain the word ‘file’.
Conclusion:
In this tutorial, we have covered the basics of input/output redirection in Linux/Unix, including redirecting output, appending output, redirecting input, combining input and output redirection, and using pipes. These features are incredibly useful for manipulating data and automating tasks in the terminal.
It’s important to note that these examples are just the tip of the iceberg when it comes to input/output redirection in Linux/Unix. There are many more advanced features and use cases that you can explore, including redirecting errors to a separate file, using multiple pipes in a command, and more.