Advanced Command Line

From FreekiWiki
Revision as of 13:43, 28 July 2007 by Rfs (talk | contribs)
Jump to navigation Jump to search

Introduction and Prerequsites

This class is intended to be an extension of the Basic Linux Command Line for Builders class. It will review how to read a command and then explain the various option types. We'll then learn a few ways to interact with files and directories. You'll then learn about I/O redirection and finally some features of shells.

The instructor will expect that the students understand basic command line syntax as well as the advanced techniques of tab-completion and history usage, both of which are taught in the basic command line class. It's OK if you don't remember everything, but the more you can remember the more new stuff we'll be able to discuss.

Deconstructing a Command

Let's break down a command (a fairly complicated one) and see what it's doing and how to read it. The command we'll deconstruct is the following,

ls -la --sort=size -r /lib/modules/2.6.18-3-686/mod*

First let's examine what each element in this command is.

  • ls - This is the command you're going to run, it is an executable program which exists on your system (i.e. you can find the ls command, try 'which ls'.
  • -la - These are two short-hand options; the -l and the -a options. These options list using the long format and list all of the contents of a directory, respectively.
  • --sort=size - This is a long-hand option that takes an argument. In other words, we're telling the ls command to sort the output and specifically we're telling it to sort according to the file's sizes.
  • -r - This is another short-hand option that could have been combined with the other short-hand options, but it wasn't. It tells the ls command to list in reverse.
  • /lib/modules/2.6.18-3-686/mod* - This is the path which we wish to list. Note the '*' at the end; it's the wildcard character. It instructs ls to list only files within the /lib/modules/2.6.18-3-686/ directory that begin with "mod" and have zero or more characters following it. This means that these are all valid matches for the wildcard; "mod", "modd", "mod182", "modABC123" and "modihdasfkahsldkjhfhdsflkahsdlkhfalhdsflkajhslk3246367".

So, now that we know what each of the elements are how would we read this command? We'll you'd read it in a very similar way to the way that you'd read a sentence in the English language. The subject of our sentence is the computer, because it's always the thing which is taking the action. The verb in the sentence is the command. We're asking the computer to do something and the command is what we're asking it to do. The options are the adverbs, they modify the verb, similarly to if I said, "I slowly walked to the store", "slowly" is the adverb which modifies the verb. I didn't say that I walked to the store, I said that I slowly walked to the store. Lastly, the object to which we're acting upon is the argument or the target of the command.

Examining Files

In Linux there is no enforcement of file type extensions.In other words, if a file is named foo.txt there is no guarantee that the file is a text file as the extension .txt would imply. Generally it is a good idea to give files appropriate names and sometimes that means using .xxx at the end of the file name, but it's not required. This means that if you aren't familiar with a file (i.e. you didn't create it or haven't used it before) you need a way to figure out what it is.

  • file - This command is used by passing the command file a file which you're curious about. For example, "file foo.txt" would tell you what foo.txt is.

If you have a text file that you'd like to

  • cat -

Copying, Creating, Moving and Removing

Files

  • touch
  • cp
  • mv
  • rm

Directories

  • mkdir
  • rmdir
  • cp -r
  • mv

BASH

  • programming language
  • environment variables
  • PATH and tab completion
  • /etc/init.d/networking

I/O Redirection

  • ls /media /foo 1> out.txt 2> err.txt
  • '>'
  • '>>'
  • tee

Process Management

    • ps (only user's processes)
    • ps -axf (all processes)
    • xeyes
    • suspend
    • bg and fg
    • kill
    • jobs

Extras

  • grave accent