Manipulate command output.
Outcomes
Run non-interactive commands and interactive commands.
Redirect standard input to another command.
Redirect standard output to a file and to a device.
As the student user on the workstation machine, use the lab command to prepare your environment for this exercise, and to ensure that all required resources are available.
[student@workstation ~]$ lab start files-stream
Instructions
On the command line, use the echo command as a non-interactive command to print a message to the command line:
[student@workstation ~]$ echo "Hi there!"
Hi there!Use the cat command as an interactive command:
[student@workstation ~]$ catType a string into the running cat session and confirm that your standard input is repeated to standard output.
[student@workstation ~]$ cat
goodbye
goodbyeYou typed the first string, and the second string is displayed by the cat command.
Press Ctrl+C to interrupt the cat session.
...output omitted...
goodbye
^CRedirect standard output to a file by using a greater than (>) symbol.
Use the cat command to confirm the contents of the file:
[student@workstation ~]$echo test > example.txt[student@workstation ~]$cat example.txttest
Redirect standard output to a file again by using a greater than (>) symbol.
Use the cat command to confirm that the contents of the file have been overwritten by the new value:
[student@workstation ~]$echo hello > example.txt[student@workstation ~]$cat example.txthello
Redirect standard output to the end of a file by using two greater than (>>) symbols:
[student@workstation ~]$ echo world >> example.txtUse the cat command to view the contents of the example.txt file that you created:
[student@workstation ~]$ cat example.txt
hello
worldRedirect standard output from the echo command to the input of the rev command:
[student@workstation ~]$ echo hello | rev
ollehIn one line only, redirect standard output from the rev command to the input of the tee command, and use the tee command to write the output to both your terminal and to a file.
Use the cat command to confirm success.
[student@workstation ~]$echo hello | rev | tee example.txtolleh [student@workstation ~]$cat example.txtolleh
Use the ls command on a directory that does not exist to generate an error message on standard error:
[student@workstation ~]$ ls void
ls: cannot access 'void': No such file or directoryRedirect standard error to a file.
Use the cat command to confirm the contents of the file.
[student@workstation ~]$ls void 2> example.txt[student@workstation ~]$cat example.txtls: cannot access 'void': No such file or directory
Redirect standard error to a file using two greater than (>>) symbols.
Use the cat command to confirm the contents of the file.
[student@workstation ~]$ls void 2>> example.txt[student@workstation ~]$cat example.txtls: cannot access 'void': No such file or directory ls: cannot access 'void': No such file or directory
Redirect standard error to the /dev/null device.
[student@workstation ~]$ ls void > /dev/nullThe command does not produce any output and the error messages are discarded.