Ade Malsasa Akbar contact
Senior author, Open Source enthusiast.
Wednesday, June 6, 2018 at 23:39


Continuing the second part, in this part you will learn how to combine two commands in one (called pipelining) and also to store data from memory into text file (called redirection). These two techniques are the basic of all Unix-like systems and applicable on all GNU/Linux distros. Go ahead and practice them all!

Subscribe to UbuntuBuzz Telegram Channel to get article updates directly.

Read also First Part: Navigation and Second Part: Copy & Delete of the most basic command lines for Ubuntu beginners.

The Keys


There are two keys you will use in this exercise: the vertical pipe sign ( | ) and the greater-than sign ( > ). They are pipeline and redirection signs. You get the former by pressing Shift+Backslash, and the latter by pressing Shift+Period, for most keyboards.

The Commands Used


  • ps aux
  • cat
  • ls
  • ping
  • grep
  • awk

Command 1


$ ps aux
$ ps aux | grep bash
$ ps aux | grep shell

Command 2


$ nautilus
$ ps aux
$ ps aux | grep nautilus

$ ls
$ ls | grep D
$ ls | grep M
$ ls | grep V
$ ls | grep P
$ ls -R | grep pdf
$ ls -R | grep mp3

What do you see?

$ soffice --writer
$ soffice --calc
$ soffice --impress
$ ps aux | grep writer
$ ps aux | grep calc
$ ps aux | grep impress

Close all LibreOffice windows, and try again:

$ ps aux | grep writer
$ ps aux | grep calc
$ ps aux | grep impress


Command 3


Turn on your internet access here.

$ cat /etc/resolv.conf
$ cat /etc/resolv.conf | grep nameserver


Command 4



$ ping gnu.org
Wait 5 seconds
Press Ctrl+C

$ ping kernel.org
Wait 5 seconds
Press Ctrl+C

$ ping localhost
Wait 5 seconds
Press Ctrl+C

Command 5


$ ping gnu.org | awk '{ print $8 }'
Wait 5 seconds
Press Ctrl+C

$ ping kernel.org | awk '{ print $8 }'
Wait 5 seconds
Press Ctrl+C

$ ping localhost | awk '{ print $8 }'
Wait 5 seconds
Press Ctrl+C

Turn off your internet access here.

What do you see?

Command 6


$ dpkg -l

What do you see?

$ dpkg -l | grep firefox

What do you see?

$ dpkg -l | grep thunderbird

What do you see?

$ dpkg -l | grep kernel
$ dpkg -l | grep kernel | grep image

What's the difference?

Command 7


$ cat /etc/apt/sources.list
$ cat /etc/apt/sources.list | grep security
$ cat /etc/apt/sources.list | grep updates

Command 8


echo "Hello, I am a message!"
echo "Hi!"
echo "This is a data!"

Command 9


echo "this was written on Terminal screen" > text.txt
echo "this text become a file by redirection" >> text.txt
echo "take data from memory and store it as file" >> text.txt

echo "create new text!" > file1.txt
echo "single sign means to replace" > file2.txt
echo "double sign means to append" >> file2.txt
echo "this is added as the last line" >> file2.txt

cat text.txt
cat file1.txt
cat file2.txt

Command 10


$ cat /etc/apt/sources.list
$ cat /etc/apt/sources.list > file3.txt
$ cat file3.txt

$ cat /etc/resolv.conf
$ cat /etc/resolv.conf > file4.txt
$ cat file4.txt

$ cat cat /usr/share/doc/firefox/copyright
$ cat /usr/share/doc/firefox/copyright > file5.txt
$ cat file5.txt

$ cat file3.txt
$ cat file4.txt
$ cat file5.txt

Try to copy the content of file5.txt and paste it into LibreOffice Writer and save as PDF. You got a PDF!

Command 11


man apt
man cp
man rm
man ls
man wget

Press q to quit every manual.

man apt > apt-book.txt
man cp > cp-book.txt
man rm > rm-book.txt
man ls > ls-book.txt
man wget > wget-book.txt

Again, try to copy the books into LibreOffice Writer and export them as PDF. Otherwise, you can also print them to papers!

Command 12


$ ps aux | grep bash
$ dpkg -l | grep firefox
$ dpkg -l | grep kernel
$ dpkg -l | grep gnome

$ ps aux | grep bash > file6.txt
$ dpkg -l | grep firefox > file7.txt
$ dpkg -l | grep kernel > file8.txt
$ dpkg -l | grep gnome > file9.txt

Command 13


$ cat file1.txt file2.txt file3.txt > joined-file1.txt
$ cat file4.txt file5.txt file6.txt > joined-file2.txt
$ cat file7.txt file8.txt file9.txt > joined-file3.txt

Open up joined-file one, two, and three with Gedit instead. What do you see?

Command 14


$ cat joined-file1.txt joined-file2.txt joined-file3.txt > final-file.txt

Again, open up the final-file with Gedit. Also try to open it up with LibreOffice and export it as PDF.

Notes


  • Ctrl+C is cancel in Terminal: to cancel running command.
  • cat command is not an animal, it is concatenation.
  • echo command is a text printer to Terminal screen. The printed text are stored in computer memory (temporary), not file (permanent).
  • dpkg -l is a command to list all software packages installed on Ubuntu system.
  • ps aux is a command similar to Windows Task Manager, to show all currently running programs.
  • grep is a command to search text based on your keyword (texts found are marked as red).
  • awk is a command to read and manipulate text in beautiful way.


Summary


  • Commands can be combined one with another.
  • Combining commands is achieved by using pipe ( | ) sign. Thus it's called pipelining.
  • Text from Terminal screen can be stored (redirected) into file.
  • Redirecting data into file is achieved by using redirection ( > ) sign. 
  • You can create TXT version of Ubuntu commands' manuals by using redirection. Once created, TXT can be printed or saved as PDF with LibreOffice! This way you can read Ubuntu manuals you want in papers.
  • Pipelining and redirection can be combined, too. 
  • You can combine any pair of commands and any number of commands as you wish (not limited to 2!) and the result is infinite number of solutions you can achieve.
  • cat command can be used to join split files (part1, part2, etc.) into one. 

to be continued...



This article is licensed under CC BY-SA 3.0.