Intro to the Command Line: Part I

From srevilak.net
Jump to: navigation, search

Intro to the Command Line: Part I

We tried out a few different commands this evening:

  • ls to list files.
  • cd to change directories
  • pwd to see what directory you're currently in
  • more and less to examine the contents of files
  • id to see what your username is, and to find out which groups you're in

The general form for a command is

 COMMAND-NAME  OPTIONS  ARGUMENTS

COMMAND-NAME is the name of a command, like ls or cd.

ARGUMENTS indicate what the command should operate on. For examine, ls /etc has an ARGUMENT of "/etc", meaning that ls should show files in the /etc directory.

OPTIONS affect the command's behavior. For example, in ls -l /etc, the "-l" is an option that tells ls to produce a "long" listing.

In command lines, most shells recognize "~" as a shortcut meaning "my home directory". So, ls ~ lists the files in your home directory.

If you want to think in terms of grammar, COMMAND is a verb, OPTIONS are adverbs, and ARGUMENTS are direct objects.

For many commands, ARGUMENTS are optional. For example cd (without any arguments) is equivalent to cd ~. ls (without any arguments) is equivalent to ls . ("." means "the current directory").

How can you tell which options a command accepts? The manual pages are a good way to find out. For example:

 man ls

Shows the manual page for the ls command.

Of course, man is just a command; it's got a manual page, which you can see with man man.

Many commands accept --help or -h as an option, and will respond with a brief summary of their command line syntax.

Man pages are written to be reference material; they're generally not tutorials (though some of them are written as tutorials).

Other sources of documentation include TDLP, the linux documentation project, and your distribution's web site. And of course, searching the fine web is another way to find documentation.

Finally, we talked a little about environment variables, and the $PAGER environment variable in particular. A "pager" is a program for viewing files one page at a time (more and less are two popular pagers). As an illustration, compare this

 export PAGER=more
 man ls

with this

 export PAGER=less
 man ls

If you define a $PAGER program, man will use it to display manual pages one screen at a time. The former shows the ls manpage, paged through more. The latter shows the ls manpage, paged through less. You can also do silly things like

 export PAGER=tac
 man ls

to see the ls manpage displayed with the lines reversed.