Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

using them as described so far. The good news is that it is possible using
redirection.


Commands take their input and give their output in a standard place. This is
called standard input and standard output. By default, this is configured as
the output of a keyboard for standard input because it comes in to the
computer from the keyboard and the screen for standard output because the
computer displays the results of a command to the user using that screen.
Standard input and standard output can be redirected.


To redirect output, you use > on the command line. Sometimes people read
this as “in to.” Here is an example:


Click here to view code image
matthew@seymour:~$ cat /proc/cpuinfo > file.txt


You know the first part of this line reads the information about the CPU from
the file /proc/cpuinfo. Usually this would print it to the screen, but here
the output is redirected into a file called file.txt, created in your /home
directory, because that is the directory in which you issued the command. You
can now read, edit, or use this file in any way you like.


CAUTION
Be aware that you can overwrite the contents of a file by using a redirect in
this manner, so be certain that either the destination file you name does not
exist or that its current contents do not need to be preserved.

What if you want to take the contents of an existing file and use that data as
an input to a command? It is as simple as reversing the symbol from > to <:


Click here to view code image
matthew@seymour:~$ cat < file.txt


This displays the contents of file.txt on the screen. At first glance, that
does not seem useful because the command is doing the same thing it usually
does: It is printing the contents of a file to the screen. Perhaps a different
example would be helpful.


Ubuntu uses a software packaging system called apt, which is discussed in
Chapter 9, “Managing Software.” By using a command from the apt stable,
dpkg, you can quickly list all software that has been installed using apt on a
system and record that info into a file by using a redirect:


Click here to view code image
matthew@seymour:~$ sudo dpkg --get-selections > pkg.list

Free download pdf