Linux Command Line Basics : Lesson 2 : Shell Commands

1. L2 Intro


You've used sevral shell commnds and you've seen their results.
In this lesson, you'll learn some more shell commands and get practice using them.

2. Quiz: Filenames and Contents


3. Command History

If you type a command and you don't get it right, or if you just want to recall something that you've used before, try using the up arrow key.
The shell keeps a history of commands that you've run so you can walk back through them using the arrow keys. Then you can edit a previous command line that you've used and maybe get more success.

The 'history' command prints out the whole history.
Another is the keystroke control R.
This is a special function called reverse i search, which lets you search your shell history.
This is awfully handy if you used some command long ago or just yesterday and it's long command you want to recall it now.

4. Some Common Commands

For example, ls lists the files in the current directory.

If you didn't download things.zip earlier you can do it now by running this command inside your VM's shell:
curl -o things.zip http://udacity.github.io/ud595-shell/stuff.zip

Let's go ahead and unzip that so we have more files to work with.
The unzip command typically takes one argument.



The file name of the file to unzip.
As it works, unzip lists the files and directories it's uncompressing.

Another useful command is cat, give cat a list of file names and it'll print out the contents of the files.
It's an abbreviation for concatenate.
Cat, concatenate files, and outputs the result.
You can also use cat to read a single files.

I was just using one of the shells built in time saving features, tab completion.
If you press the tab key while typing a word, the shell will try to autocomplete it for you.
If i hit tab again, the shells will tell me all the files that match.

There are also commands for analyzing files.
wc is the word count program.
It tells us how many lines, words, and byte are in a file.



Another command for analyzing files is diff.
Diff compares files and shows you how they differ.
This can be useful if you have multiple versions or editions of the same file and you want to know what changes have been made.

5. Manual Pages

Manual entries are called man pages, and this is cowsay's man page.



Man pages follow a common format.
The first section includes the command's name and a brief description of the command.

The next section is the synopsis.
The synopsis explains how to the command's flags and arguments.
Options surrounded by square brackets are optional.
Some options require additional information.



The description describes all of the options in detail.

The later sections of the man page include more detailed information about the command.

When you're done referencing the manual, you can press q to exit.

6. Quiz: Manual Pages and cowsay


7. Quiz: Using the Manual for Serious Purpose

In fact, ls by default doesn't list any files whose names start with a dot.
This is because files with names starting with dot are often used on Linux for caching and configuration and other things that you don't normally care about.
So the shell will hide them by default because they're not usually interesting.


8. Quiz: Options to ls

You can find out whether a file is a file or directory by looking at this left most column.
There's going to be a d for each directory and a dash for every regular file.



9. Quiz: Researching Commands 


Really, don't run this command. This isn't a joke. Look it up on a search engine and using the manual, and think about what it does.

Another useful tool for researching commands is the apropos command. You can use apropos to find commands relevant to particular keywords.
For example apropos working directory provides a list of commands that somehow work with the current working directory.
apropos is good for refreshing your memory, or for finding new commands to explore!

10. Line Based Programs

Ping lets you test whether another machine on the internet is alive, and how long it takes to send a message to that machine and back.


Ping start and it prints out a line for each successful echo, but it doesn't stop on its own and give you your shell back.

One of the most common ways to tell a program like this to stop to Ctrl+C which sends the interrupt signal.

But some programs have a different behavior.

This allows programs to be chained together on to a pipeline which is a little bit beyond the scope of this course.



When you run a program like this from the terminal, it'll read from your keyboard input and write back to the terminal screen. And very often when your input is done you want to send it and end of file character which you do by typing Ctrl+D.


You can usually use them on files as well as on text that you type in or paste into the terminal.

11. Quiz: Waiting for Input

bc is a simple calculator program, you can use it to do arithmetic like this.
It knows about order of operations and parenthesis and things like that.


12. Full Screen Interactive Programs: less

You can use less to display any file like not just man pages.
Scrolling up and down with arrow keys works the same way as it did before, and man you can also scroll down a page at a time by using the D key or  hitting the Space Bar.
You can scroll up one page at a time with a U key.
If you wan to skip to very last line of the file, you can use the right angle bracket for that.
To get back up to the first line use the left angle bracket.



Less also has built-in search.
Type slash and then the string you'd like to search for.
You can find the next occurrence of the search term by pressing n.
To go back to the previous occurrence, use capital N.
Search terms are case sensitive.

If you're familiar with regular expressions you can use them here unless the search is actually looking for lines that match your pattern.
The regular expression library will interpret other characters in ways you might not expect.

13. Quiz: Editing Files in nano

You can also edit files in the terminal.
This is really handy if you're logged into a remote system and you want to change a configuration file or something.
Instead of pulling the file back to your desktop and editing it with Sublime or another graphically based text editor, you can use an editor that runs right in the terminal.

Like vim and emacs and joe, and many others.
But one of the easiest ones is built into ubuntu, and it's called nano.
It takes the file to edit as a command line parameter.



14. Lesson Outro


In this lesson, you'll work with a number of different programs to interact with a terminal in different ways. Some that display output and immediately exit, some that work with input one line at a time, and some that are fully interactive.

0 개의 댓글:

댓글 쓰기