Advanced Command Line

From FreekiWiki
Revision as of 12:58, 11 August 2007 by Rwlove (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. You'll then learn a few ways to interact with files and directories followed by I/O redirection and finally some features of BASH.

The instructor will expect that the students understand basic command line syntax as well as the advanced techniques of tab-completion and history usage (up arrow), 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've determined that you have a plain text file and you'd like to see it's contents you can cat it.

  • cat - This command is used to examine the contents of one or more files. For example, "cat file1 file2 file3". This will just dump the contents of file1, file2 and file3 to the screen, in that order.

Copying, Creating, Moving and Removing

Now that you know how to identify files let's look at how to manipulate these files within the file system. WE'll also look at how to manipulate directories (a.k.a folders).

Files

  • touch - This command will update the time stamp on a file. If the file name specified doesn't exist it will create a new file.
  • cp - This command copies files from one location in the file system to another. Since this is a copy the original file still exists in its original location after the operation.
  • mv - This command moves files from one location in the file system to another. Since this is a move the original file will not exist in its original location after the operation.
  • rm - This command deletes one or more file(s) specified.

Directories

Folder manipulation can be a bit confusing. The main confusion usually occurs when performing an action on a non-empty directory. When there are contents in a directory an option-less cp or rm will fail. To successfully delete or copy a folder with contents you need the '-r' option. The -r option tells the commands to act [[1]], in other words to act on the folder and all of its contents.

  • mkdir - This command create a directory.
  • rmdir - This command deletes any empty directory. To delete a directory with contents run "rm -r <foldername>"
  • cp -r - This command copies a directory and its contents from one location to another.
  • mv - This command moves a directory to a new location.

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