Linux Guide/Using the shell

Introduction edit

The command line interface (CLI or terminal) may seem intimidating at first, but it's important to remember that the command line is really, truly your friend. An army of tools are at your disposal that can take what would be a tedious and lengthy job (like removing the last four characters from every line of a lengthy file) and turn it into a two minute job.

For every Linux distribution the command line prompt will look a little different. For example, on one system you might see your username, the '@' symbol, the machine name, your current directory and the prompt.

       user@linux ~/$ 

This is a very common prompt. You may also see your username, a space, the fully qualified domain name of the computer, the full path to your present working directory followed by the prompt. In the example above 'user' is the username and 'linux' is the name of the host(computer) you are logged on.

       user /home/user$ 

The prompt varies from system to system based on a number of things. For example, it may be the default configuration set by the creators of your particular Linux distribution. It could also have been configured by the person who administers the computer. Maybe you, yourself, configured how you wanted your prompt to look.

The way you configure the look of your command prompt depends on what shell you use and the shell is the piece that most people commonly refer to as "the command line" when, in reality, it is simply a piece of software that provides an interface to the services of a kernel. The distinction between a 'shell' and the 'command line' is simply that a shell refers to a specific piece of software (e.g BASH, Tch, ksh, etc) that provides a command line interface. Most modern Linux systems use BASH (Bourne Again SHell) as the default shell.

A Little History edit

The commands used at the command line may seem a little cryptic due to their tendency to be very short. This is because the roots of the Linux command line are from systems where a single letter entry could take a significant amount of time to travel from a terminal, to a central server and back to the terminal where it was printed onto a roll of paper. In those old systems, the shorter the input was, the better as it meant less time waiting to issue your command and receive output. The best thing you can do to remember what commands stand for is to find out what word the command is an abbreviation for. This can go a long way to remembering the command later.

Some Useful Terminal Commands edit

Summary of Common Commands edit

  • ls - This command lists the contents of your present working directory.
  • pwd - Shows you what your present working directory is.
  • cd - Lets you change directories.
  • rm - Removes one or more files.
  • rmdir - Remove an empty directory.
  • mkdir - Make a directory.
  • ps - Provides a list of currently running processes.
  • cp - Copy a file.
  • mv - Move a file (this is also used to rename a file, "moving" it from one file name to another.)
  • grep - The global regular expression print program lets you search through a file or output of another program.
  • find - Find a file on the filesystem
  • man - Displays the manual for most commands (including 'man').
For help about a command, try man <command> which will bring up the manual for it. Note that some commands are built into your shell and do not have a man page, use your interpreter internal command (should be help <command>).

Using Common Commands edit

Using ls edit
       user@linux ~/$ ls
       Afilez  Report.doc  aFilea  finances.xls  myPic.jpg  report1.doc  vacation pictures

You may notice from the example, default output of 'ls' that the files are not in strict alphabetical order. This is because the output is sorted on the numeric ASCII value of each letter. Because upper case letters have a lower ASCII value than lowercase letters, the returned result will always be files starting with upper case letters first. For example, if we add the file AZTRIP to the directory we will get 'AZTRIP' as the file listed first followed by 'Afilez' because the ASCII value of 'Z' is numerically lower than the ASCII value of 'f'.

If the ASCII values of letters has you confused, the important thing to remember here is that uppercase letters trump lowercase letters and will appear first in listings. The result is that files are first sorted by case and sorted alphabetically second.

The default output of ls isn't as useful as it could be. For example, given the output in the example, we don't know what are files and what are directories. We can likely guess that "Afilez", "aFilea" and "vacation pictures" are all directories as they don't have an extension after the file name. To confirm this, we can pass the 'l' parameter to 'ls' to get "long listing format" output.

       user@linux ~/$ ls -l
       -rw-r--r-- 1 adrong mkgroup-l-d 163328 Jul 24 11:42 AZTRIP
       -rw-r--r-- 1 adrong mkgroup-l-d  11264 Jul 24 11:42 Afilez
       -rw-r--r-- 1 adrong mkgroup-l-d  22528 Jul 24 11:43 Report.doc
       -rw-r--r-- 1 adrong mkgroup-l-d  24576 Jul 24 11:42 aFilea
       -rw-r--r-- 1 adrong mkgroup-l-d 300032 Jul 24 11:43 finances.xls
       -rw-r--r-- 1 adrong mkgroup-l-d  47104 Jul 24 11:41 myPic.jpg
       -rw-r--r-- 1 adrong mkgroup-l-d  17920 Jul 24 11:43 report1.doc
       drwxr-xr-x 1 adrong mkgroup-l-d      0 Jul 24 11:24 vacation pictures

The first character in the output above indicates the file type; where a dash indicates a file and the letter 'd' indicates a directory. From this output we see that the only directory is "vacation pictures"

Most systems have an alias on the ls command to include the '--color' flag so that the command highlights different file types with different colors. Directories are commonly shown in blue. If the output of your 'ls' command is always the same color, try "ls --color".
Another common parameter (also known as an "argument") to the ls command is 'a' which shows all files including any hidden files and folders that start with a period. Arguments may also be combined together in a single block of letters. For example, "ls -l -a" can also be expressed as "ls -la".
Using cd edit

The cd command lets you navigate from directory to directory on your system. The first thing you may need to know when using the cd command is that, unlike windows, Linux does not use the concept of drive letters. Instead, everything is somewhere within the root directory, '/', including other disk drives.

There's a couple ways to use cd to navigate to the directory you want. These are known as "full path" and "relative path".

Full path always begins with the root directory '/'. For example, if we're in our home directory '/home/user' and we want to navigate to the 'vacation pictures' directory, we would issue the following command.

       user@linux ~/$ cd /home/user/vacation\ pictures
       user@linux vacation pictures/$ 

You may note that our prompt changed a little to indicate the directory we're currently in. This will be the case on most systems, but as previously discussed, this depends on how your shell is configured. You may also note the backslash between "vacation" and " pictures". This backslash is known as the "escape character" and tells the cd command that the space is part of the directory name. If we omitted the backslash, the cd command would error and tell us that it could not find the directory "vacation". If you don't like typing backslash or have a directory name with many spaces in it, you can use quotation marks around the path.

Relative path can start a number of ways, but always describes how to get to a specific directory from the directory you're currently in. To do this, we need to know how to move to directories that immediately surround the directory. These directories are parent directories and sub-directories

A sub-directory is any directory that is contained within the directory we're currently. Given the output below of "ls -l", we see that chicago, florida and "new york" are all sub-directories to the "vacation pictures" directory.

       user@linux vacation pictures/$ ls -l
       drwxr-xr-x 1 adrong mkgroup-l-d 0 Jul 24 12:44 chicago
       drwxr-xr-x 1 adrong mkgroup-l-d 0 Jul 24 12:44 florida
       drwxr-xr-x 1 adrong mkgroup-l-d 0 Jul 24 12:44 new york

A parent directory is the directory that contains the folder that we are in. This makes the "vacation pictures" folder the parent of the "chicago", "florida" and "new york" directories.

To cd to a sub-directory, you can start the path name with "./" which indicates the directory we're already in, or you can immediately start with the name of the sub-directory. For example, both of the following commands will change you to the "florida" directory.

       user@linux vacation pictures/$ cd ./florida
       user@linux florida/$ 
       user@linux vacation pictures/$ cd florida
       user@linux florida/$ 

To cd to a parent directory, you always specify "../". The following command will return us to the "vacation pictures" directory.

       user@linux florida/$ cd ../
       user@linux vacation pictures/$ 

If we are already in the "chicago" directory and want to cd to "new york", we must first tell the system we want to go up one level to the parent directory and then to the "new york" directory. We can do this by saying "cd ../" and then "cd new\ york" or we can do it as one step using the command below.

       user@linux chicago/$ cd "../new york"
       user@linux new york/$ 
Notice how we used quotation marks around the path to cd to the "new york" directory. We could have, instead used the backslash before the space and omitted the quotes to get the same result.

Similar to "./" and "../", there's also "~/" which indicates the home directory of the user we're logged in as. If we wanted to cd to the "vacation pictures" folder while in the "/usr/lib" directory, we could do this with any of the following commands.

       user@linux lib/$ cd ~/vacation\ pictures
       user@linux lib/$ cd "/home/user/vacation pictures"
       user@linux lib/$ cd ../../home/user/vacation\ pictures
Using the cd command without specifying a directory will return you to your home directory.

File manipulation edit

Copy directory options:

       $cp -frvp /source /destination
        ^   ^^^^    ^        ^
        |   ||||    |        \----------- Path from root is best
        |   ||||    \-------------------- Path to copy from root best
        |   ||||
        |   ||||                          OPTION FLAGS---------------- 
        |   |||\------------------------- Keep permissions
        |   ||\-------------------------- Verbose
        |   |\--------------------------- Recursive files and folders
        |   \---------------------------- Force
        |                                 ----------------------------
        \-------------------------------- Copy command 
cp exam test
-copy file named "exam" to directory named "test"
cp exam .
-copy file named "exam" to current directory (note period at end of command line)

 cp prod/p* reports
   -copy all files beginning with "p" to reports directory
   -can reverse p* to *p for files ending with "p"

Copy img to floppy:

dd bs=2x80x18b if=/dev/fd0 of=/tmp/floppy.img

Copy source to make img:

dd if=/dev/cdrom of=filename.iso
                    ^------^_________ replace with file name


Remove directory:
       $rm -frv dir/
        ^   ^^^    ^
        |   ||| \---------------------- Remove directory
        |   ||| 
        |   |||                           OPTIONS-------------------- 
        |   ||\-------------------------- Verbose
        |   |\--------------------------- Recursively through all dir 
        |   \---------------------------- Force
        |                                 ---------------------------
        \-------------------------------- Remove command

rm *     <- Delete all files in directory
rm -r    <- Delete directory and contents

Command options for permissions
r = read
w = write
x = execute
X = execute only if user already has execute
s = set user or group id on execution
t = sticky +
u = permission granted to user who owns file
g = permissions granted to group members
o = permissions granted to users other than "u" or "g"


                   |||||||\\\----------- other users
                   ||||\\\-------------- group
                   |\\\----------------- owner
                   \-------------------- directory = "d" (possible: -, b, c, q)

                Octal Method: chmod 
    4 = read                   examples: 7 = 4+2+1  will show "rwx" 
    2 = write                            6 = 4+2    will show "rw-"
    1 = execute                          5 = 4+1    will show "r-x"
  Usage:  chmod 755 FileName          <--- change file permissions
  Usage:  chmod -R 755 DirectoryName  <--- change directory permissions recursively
  will show permissions "-rwxr-xr-x" 
         Note: first character in permissions show what kind of file
             - = file
             d = directory
    b, c, or q = device files

Usage format:

chmod -R ugoa =+- rwx filename
       ^ ^^^^ ^^^ ^^^     ^
       | |||| ||| |||     |
       | |||| ||| |||     \---------- file or directory
       | |||| ||| |||                 -----------------------------------
       | |||| ||| ||\---------------- Execute
       | |||| ||| |\----------------- Write
       | |||| ||| \------------------ Read
       | |||| |||                     -----------------------------------
       | |||| ||\-------------------- Remove
       | |||| |\--------------------- Add
       | |||| \---------------------- assign
       | ||||                         -----------------------------------
       | |||\------------------------ All
       | ||\------------------------- Others
       | |\-------------------------- Group
       | \--------------------------- User
       |                              -----------------------------------
       \----------------------------- Recursively through all directories
       Usage: chmod g+w filename

In addition, three extra permission may be assigned:

   For files:
         u +-= s  : Set UserID, run as if file-owner
         g +-= s  : Set GroupID, run as if file-group
         o +-= t  : Set "sticky bit", keep executable in swap-space - can only be set by root (no longer supported by Linux-kernel)  
   For directories:
         u +-= s  : Set UserID, files in directory inherits directory's ownership
         g +-= s  : Set GroupID, files in directory inherits directory's group
         o +-= t  : Set "sticky bit", limits the ability to remove a file in a world-writeable directory (like /tmp) to the file's owner.


VI/M uses 2 modes in which to process text. To process the various commands available use the "<esc>" key. To insert text simply type "i" to return to text insert mode.

For Starters/Basics------------------------

  Start Vi     vi     [From command line]
  Insert mode  i
  Command mode <esc>
  Save doc     :w   
  Quit app     :q
        Usage:  :wq  [will save and exit document]
  Right        l
  Left         h
  up           k
  Down         j

Sort all

   :1,$ !sort

Replace text

   :1,$s /and/AND/g
   ^^^^^   ^   ^  ^
   |||||   |   |  |
   |||||   |   |  \----------- Globally
   |||||   |   \-------------- Replace with
   |||||   \------------------ Find
   ||||\---------------------- substitute for text
   |||\----------------------- To last line
   ||\------------------------ separator
   |\------------------------- From line "1"
   \-------------------------- ":" operator to start command processing
Useful tid bit to remove EOL characters from DOS file 
[use key sequence "<ctrl v><ctrl m>" together for EOL char]

Select Ranges
 :1,$   = All lines in file
 :1,.   = All lines from start of file to current (included)
 :.,$   = All lines from current to end of file (inclusive)
 :.-2   = Single line 2 lines before current line
 :10,20 w abc <enter>    = Write lines 10,20 to new file abc
 :r abc       <enter>    = Reads contents of file abc into working file
 :10,20d      <enter>    = Delete block lines 10 ~ 20

See Line Numbers

 :set number             to disable  :set nonumber

Write lines to new file

 :1,10w abc     [puts lines 1~10 into file abc]

Change working file from current to file abc [be sure to save first]

 :e abc

Read abc file into working file after cursor

 :r abc

Execute command from prompt

 :!cmd    [where cmd is could be a bash command]
Example ->    :r !date   [inserts time and date at prompt]

Enable syntax option for programming source text highlighting

 :syntax enable      To disable = :syntax off

VI Navigation  [escape key = <esc>]
0  [zero] = Move to beginning of line
G         = Go to end of file
5G        = Move to beginning of 5th line
u         = Undo
.         = Repeat last command
x         = Delete character
dd        = Delete line
dw        = Delete word
d$        = Delete from cursor to end of line
$ then J  = Join/append following line with current
$ then a  = Append input at end of line
A         = Append input at end of line [same as above, but one less key stroke]
i         = Insert text at cursor
/         = Bottom of screen type search string and <enter> will move 
                 cursor to first occurrence

Printing edit

Work from Bash Prompt:

        lpr -P //PrintServer/PrinterName "file name"
        Other options:
        -#  Number of copies [example "-2" for 2 copies]
        -c  Make copy before print

  lpq -P PrinterName : Displays printer status report
  lprm 4      : Removes job #4 from printer
  pr          : Formats, paginates, prints header
                   -d : forces double spacing
                   -h : Customize header
                   -l : Change number of lines per page | default=66
          example: pr customers | lpr  <format and print file "customers">
          example: lprm 1006           <canceled job 1006>
  lpstat -s
    ex: lp -n 2 all files
    ex: lp -n 3 all files -d a3printer
    ex: lpstat -d a3printer

See also: CUPS

Disk System edit

Disk space usage: du -h <dir>
lists an estimate of total space usage for each subdirectory found in <dir> and the estimated grand total disk space used
File System (mounted) Disks space usage: df
lists all mounted disks and blocks total, used, free, and logical disk name
df .
lists only current working mounted disk, displaying total, used, free in blocks.
df -h
lists all mounted disks total, used, free, and logical disk name in human terms (KB,MB,GB,TB).
df -kh
lists all mounted disks total, used, free and logical disk name in human terms (KB, MB, GB, TB) and space used in % (percentages)[-k] option.
List partitions: fdisk -l <disk>
list all partitions on a hard drive e.g. fdisk -l /dev/hda