Introducing the Unix Command Line


  • Unix was born in 1969 at Bell Laboratories, a research subdivision of American Telephone and Telegraph Company (AT&T) Bell Labs. 
  • Unix was designed to allow multiple users to access a mainframe computer simultaneously.  Unix operating systems were developed mainly for mainframes, servers and workstations.  
  • Apple OSX is based on a Berkeley Software Distribution(BSD) Unix platform.

Unix Shell
  • The Unix shell is a command line interpreter, or  a program that forms a link between users and the computer. 
  • The shell accepts commands that you type into the computer, and then it executes them.
  • The shell contains over 100 built-in commands for your use.  At the shell %  prompt you can type your commands.
  • There are 2 widely used shells:
    • Bourne shell provided with the standard Bell Labs version of Unix
    • C shell developed at the University of California, Berkeley
Unix file structure

Everything in Unix is based on a file structure to include peripherals (USB, printers, scanners).  There are no drive letters.  Everything is indexed off the root “/”  
-USB drive example: 
/dev/sdb1 (Device) 
/media/external (Files)

Format of UNIX Commands : 
command options(s) filename(s)
  • The command is the name of the utility or program that we are going to execute. 
  • The options modify the way the command works. It is typical for these options to have be a hyphen followed by a single character, such as -a. It is also a common convention under Linux to have options that are in the form of 2 hyphens followed by a word or hyphenated words, such as --color or --pretty-print. 
  • The filename is the last argument for a lot of UNIX commands. It is simply the file or files that you want the command to work on. Not all commands work on files, such as ssh, which takes the name of a host as its argument. 
  • UNIX commands can be very simple one word commands, or they can take a number of additional arguments (parameters) as part of the command. 
  • In UNIX the command is almost always entered in all lower case characters. 
  • Typically any options come before filenames. 
  • Many times individual options may need a word after them to designate some additional meaning to the command. 
Things to Keep in Mind
  • There is no ‘undelete’
  • Shell commands are case-sensitive (CaPitaLizaTIoN mAttErs)
  • Do NOT use space, ?, *, \, / or $ in file names because these have special meanings to the shell
  • Filenames that begin with . are ‘hidden’
  • There is no ‘undelete’
Commands
  • ls : lists the contents of a directory
    • l : long directory listing
    • a : lists all files, including files which are normally hidden
    • F : distinguishes between directories and regular files
    • h : ?  Look it up using man
  • pwd : prints the current working directory
  • cd : changes directories
    • The difference between relative and absolute paths.
    • Special characters ., .., and ~.
  • mkdir : creates a directory
  • rmdir : removes a directory (assuming it is empty)
    • If you get an error that the directory isn’t empty even though it looks empty, check for hidden files.
  • touch : creates an empty file with the specified name, or if the file already exists it modifies the timestamp.
  • rm : removes a file.
    • f : force deletion
    • r : recursive deletion
  • mv - moves a file, or renames a file
    • f : forces overwrite, if the destination file exists
  • cp - copies a file, leaving the original intact
    • f : forces overwrite, if the destination file exists
    • r : recursive copying of directories
  • cat : shows the contents of a file, all at once
  • more : shows the contents of a file, screen by screen
  • less : also shows the contents of a file, screen by screen
  • head : used to show so many lines form the top of a file
  • tail : used to show so many lines form the bottom of a file
  • ps : lists the processes running on the machine. 
    • ps -u username lists only your processes. 
    • ps -a : lists all processes running on the machine.
    • The PID column of the listing, provides the information required by the kill command.
  • kill : terminates a process
    • kill process_id : sends a terminate signal to the process specified by the process_id (PID).
    • In cases where the terminate signal does not work, the command "kill -9 process_id" sends a kill signal to the process.
  • nice : runs a process with a lower priority.
Differences between
+ cp and mv
  • The cp command creates a new file with a new ID number and with its own link (name) to a directory.  The old file continues to exists.
  • The mv command creates new names for the existing files and dumps the old names.  It does not create a new file.
+The rm or del command
  • If you want to remove  a file, type rm file or del file
  • Once that you removed it, it’s gone for good.
The cat command
  • general form:  cat argument
  • The cat command is used to concatenate and display files.  Concatenate means to link together.
  • Typing cat file2 will display on the screen file2 assuming that this file2 already exists.
grep Command
  • In the simplest terms, grep (global regular expression print) will search input files for a search string, and print the lines that match it. Beginning at the first line in the file, grep copies a line into a buffer, compares it against the search string, and if the comparison passes, prints the line to the screen. Grep will repeat this process until the file runs out of lines.
  • grep “phrase_to_match" file
Example:
File name “targetfile.txt”
boot
book
booze
machine
boots
bungie
bark
robots
Command:
grep “boo” targetfile.txt

Result:
boot
book
booze
boots

Output redirects
grep science science.txt
grep science science.txt > newfile1.txt
grep science science.txt >>  oldfile1.txt
grep -B 1 -A 2 science science.txt > newfile1.txt 

">" A ‘redirect’ symbol that sends output which would normally go to the screen to a text file instead.

The UNIX Pipe (|)
  • The pipe (|) creates a channel from one command to another. Think of the pipe as a way of connecting the output from one command to the input of another command.
  • The pipe can be used to link commands together to perform more complex tasks that would otherwise take multiple steps (and possibly writing information to disk).
  • Examples:
    • Count the number of users logged onto the current system.
      • The who command will give us line by line output of all the current users.
      • We could then use the wc -l to count the number of lines...
      • who | wc –l
    • Display long listings in a scrollable page.
      • The lpq command will give us a list of the waiting print jobs.
      • lpq | less