Day 17 of my ALX Software Engineering Journey: Shell Input/Output Redirection
It’s the third week of this journey, proud to have come this far. Today our projects were centered on shell input/output redirection commands. These commands are commands that route the output of a command to a file or they could also route the input of a file to a command. The standard output in a shell is the terminal, this is where results are displayed while the standard input is through the keyboard.
Let’s look at how we can redirect the output of a command below
ls > file.txt
This command above lists the content of a file or directory into the file.txt
instead of the terminal so we can say the output has been redirected into the file.txt
Assuming we would want to redirect input to a command we do it this way
ls < file.txt
We use the contents of the file to process the command ls
and it displays the content on the terminal.
Concatenation of commands with pipeline symbol
The pipeline symbol can be used to concatenate commands and when these commands are concatenated outputs or inputs of commands can be redirected into other commands. This is what I mean
ls -l | head > file.txt
Following this command above, the output of the long list of the content of this file or directory has been piped to the head command and the head command lists the content of the first 10 lines of a file or directory. This output is further redirected into the file.txt
file
This and many more are several ways I learned how to redirect input and output in the shell. You can look up more ways to combine it with options and flags here
File Descriptors
These are numbers used to denote the files where standard output, input or error documents are sent to. They are 0, 1 and 2 where 0 stands for the. 1 stands for . and 2 stands for the file description for error files.
Other commands I learned today include
tail: this command lists the content of the last 10 lines of a file or directory. It can be used with options such as (-c which lists the last 10 characters ).
find: this command is used to locate a file or directory. It’s a very powerful command that when used with flags can be very specific to find whatever you’re looking for.
wc : this command lists the number of lines, rows and characters in a file.
sort: this command is used to sort the contents of a file in alphabetical order. It can also be used to sort numerical data in ascending or descending order with the use of no flags and
-r
flag respectivelyuniq : this command is used to delete repeated lines in a command. It is worthy of note this command doesn’t affect the version of the file but it only makes this change in its display on the terminal.
rev : this command is used to print the content of a file or directory in reverse.