Difference between revisions of "CSC231 Bash Tutorial 3"
(→The wc Command) |
(→Example 2: Counting stuff) |
||
Line 99: | Line 99: | ||
ls /etc | wc -l | ls /etc | wc -l | ||
+ | |||
+ | <br /> | ||
+ | {| style="width:100%; background:limegreen" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge #1:== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | <br /> | ||
+ | How many users are registered users on '''aurora'''? Hints: all users have their home directory in /Users. The exception is student accounts, which are located somewhere else (because they are erased regularly, between semesters). | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:limegreen" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge #2:== | ||
+ | |} | ||
+ | [[Image:QuestionMark2.jpg|right|120px]] | ||
+ | <br /> | ||
+ | How many Molecular-Biology accounts are registered users on '''aurora'''? Hints: they all start with "molebio" followed by 2 numbers. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | <br /> |
Revision as of 10:52, 16 February 2017
--D. Thiebaut (talk) 10:23, 16 February 2017 (EST)
In this lab you will learn about the manual pages, and pipes.
Contents
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) |
- 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 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.
Example 2: Counting stuff
- How many lines are in the /etc directory? Well, we can do an ls and pipe the output to wc and get what we want:
ls /etc | wc
- We see that the number of lines (file name) is the same as the number of words. That's because file names in Linux normally do not contain spaces.
- If we wanted to be accurate and get only the number of files, and not three numbers:
ls /etc | wc -l
Challenge #1: |
How many users are registered users on aurora? Hints: all users have their home directory in /Users. The exception is student accounts, which are located somewhere else (because they are erased regularly, between semesters).
Challenge #2: |
How many Molecular-Biology accounts are registered users on aurora? Hints: they all start with "molebio" followed by 2 numbers.