Difference between revisions of "CSC231 Bash Tutorial 3"

From dftwiki3
Jump to: navigation, search
(Example 1)
(Example 2)
Line 65: Line 65:
 
</tanbox>
 
</tanbox>
 
<br />
 
<br />
==Example 2==
+
==The '''wc''' Command==
 
<br />
 
<br />
 
* Here's a new command that is often useful: '''wc''', which stands for '''w'''ord-'''c'''ount.  It will count the number of words, or lines, or characters in a file.
 
* Here's a new command that is often useful: '''wc''', which stands for '''w'''ord-'''c'''ount.  It will count the number of words, or lines, or characters in a file.

Revision as of 10:42, 16 February 2017

--D. Thiebaut (talk) 10:23, 16 February 2017 (EST)


In this lab you will learn about the manual pages, and pipes.


Manual Pages


  • Linux contains its own documentation. It's called the Linux Manual Pages, and you access them using the man command.
  • Let's get the manual pages for some of the commands you know:
man ls
 
  • the line ls [OPTION]... [FILE]... indicates that the command takes optional arguments, or switches, and optionally the name of a file. The line below indicates that -a ls will not ignore entries starting with a dot.
  • Let's see how to apply this information (do not type the text in parentheses):
cd
ls             
ls *.asm         
ls -a         

(go back to your home directory)
(this will list all the files)
(this will list only the files ending with .asm)
(this will list all the files in your home directory, including the hidden files)

  • Other man pages you should look at:
man rm
man cp
man mkdir
man rmdir

  • If for some reason you want to use a command but you don't know its name, try searching the man pages for a keyword. For example, you forgot the command name hexdump, but remember that it displays information in hexadecimal...
man -k hexadecimal

If you go up the list, you should find hexdump.



The man pages were created at a time when search engines did not exist, and they were the quick way for Linux users to have access to the documentation of Linux commands. Nowadays you can probably get less cryptic and more informative examples by using Google to search for various commands, either by name, or by keywords. None the less, what you will get on Google will only be a variation on the original contents of the man pages.


Pipes


  • Linux applications/programs/commands work with streams of information. They get their input from different kinds of streams. The input stream is usually the keyboard, and we referred to it as stdin for standard input. The output usually goes to an output stream, which most often is the video display, and we refer to it as stdout for standard output.
  • In some cases, the information we need requires using many different commands, and using the output of one as the input of the other. This is where the idea of a pipe taking the output of one command and feeding it to the input of an other command comes in.


Example 1


  • Let's study the files contained in a large directory:
ls /etc

this will list all the files in the /etc directory. These are the preferences or options files for many of the apps on aurora. Unfortunately, we get too much information to look at.
  • We know that less can display the contents of a file one page at a time. So we will take the output of ls /etc and pipe it to less, as follows:
ls  /etc   |  less

Observe that the output of the ls command is now viewable one page at a time. You can go up and down with b and space keys. Quit with q.


The vertical bar | is the symbol use to take the output of the command on its left and feed it as an input to the command on its right.


The wc Command


  • Here's a new command that is often useful: wc, which stands for word-count. It will count the number of words, or lines, or characters in a file.
  • Let's see how it works. If you have gotten rid of Ulysses, get a fresh copy of it:
getcopy Ulysses.txt

  • Let's get its stats:
 wc Ulysses.txt

  • the output:
 32577  267949 1580927 Ulysses.txt

which means that Ulysses contains 32577 lines, 267j,949 words, andn 1,580,927 characters.
  • We can also specify that we want only one of these quantities, by adding a switch to wc:
  wc -l Ulysses.txt
  wc -w Ulysses.txt
  wc -c Ulysses.txt

Verify that these switches (-l, -w, and -c) are explained in the man page for wc.