Saturday, April 11, 2015

Day 8: The Linux Command Line Ch6 Notes


Chapter 6 - Redirection

This is where Linux begins to rock.

Commands:
cat - Concatenate files
sort - Sort lines of text
uniq - Report or omit repeated lines
grep - Print lines matching a pattern
wc - Print newline, word, and byte counts for each file
head - Output the first part of a file
tail - Output the last part of a file
tee - Read from standard input and write to standard output and files

Things rembember: 
1. Everything in Unix/Linux system is either a file or a process.
2. There are three such files always open:
  • standard input
  • standard output
  • standard error
The Shell uses so called file descriptor to refer to them. 

0 = standard input (default is keyboard)
1 = standard output (default is screen)
2 = standard error (default is screen) 

Piping is the ability of of redirecting output of one command to become input of another. The force is strong with this one.


Lab 2

1. Create a calendar for current year in a text file that can be later printed (current year is 2015).

2. Redirect the output of 'ls -l /usr/bin' to test.txt file. Check it contains entries. Now, remove the content from this file using redirection command.

3. Redirect the output of 'date' command to the file called now.txt. Then using echo command and redirection, add "It's a glorious day!" line to it.

4. How would you redirect a program's standard output and error to a single file?

5. Create a file called fruits.txt with the following content:
banana
kiwi
apple
mango
raspberry
orange

Then, Display the content on a screen.

6. Display the content of the file sorted alphabetically.

7. Display the first fruit from the file after it has been sorted alphabetically.


8. Display the last fruit from the file after it has been sorted alphabetically.


Lab 2 Solution

1. Create a calendar for current year in a text file that can be later printed (current year is 2015).

$ cal 2015 > year2015.txt

or 

# if you want days of the week to be on the left use the following command

$ ncal 2015 > year2015.txt

2. Redirect the output of 'ls -l /usr/bin' to test.txt file. Check it contains entries. Now, remove the content from this file using redirection command.

$ ls -l /usr/bin > test.txt
$ cat test.txt

# now remove the content

$ > test.txt
$ cat test.txt

3. Redirect the output of 'date' command to the file called now.txt. Then using echo command and redirection, add "It's a glorious day!" line to it.

$ date > now.txt
$ cat now.txt
$ echo "It is a glorious day." >> now.txt
$ cat now.txt

4. How would you redirect a program's standard output and error to a single file?

2>&1

William's Note!
"Using this method, we perform two redirections. First we redirect standard output to the file ls-output.txt and then we redirect file descriptor 2 (standard error) to file descriptor one (standard output) using the notation 2>&1.".

5. Create a file called fruits.txt with the following content:

banana
kiwi
apple
mango
raspberry
orange

Then, Display the content on a screen.

pi@raspberrypi ~ $ cat fruits.txt
pi@raspberrypi ~ $ 

6. Display the content of the file sorted alphabetically.

pi@raspberrypi ~ $ cat fruits.txt | sort
pi@raspberrypi ~ $ 

7. Display the first fruit from the file after it has been sorted alphabetically.

pi@raspberrypi ~ $ cat fruits.txt | sort | head -1
pi@raspberrypi ~ $ 



8. Display the last fruit from the file after it has been sorted alphabetically.

pi@raspberrypi ~ $ cat fruits.txt | sort | tail -1
pi@raspberrypi ~ $