CS 3723
 Programming Languages 
  Unix Basics  


Conventions:
  • Unless otherwise noted, every command line should be completed with a RETURN (<-| Enter on keyboard).
  • All Unix commands must be typed in lower case.
  • Unix commands often have options, signaled by a "-" followed by the name of the option.
  • Unix commands below are in bold green, comments, following a # are in bold orange.


man   # the on-line manual with a complete list of options available for each command.

    man printf


pwd   # print working directory: tells absolute path from root to where you currently are in the file system.


cd   # change directory.

    cd path/directory   #  where path/directory is either an absolute or relative pathname to the desired directory.
    cd ~   # takes you to your home directory.
    cd ..   # takes you to the parent directory of the current directory.


mkdir   # make directory.

    mkdir directoryname


rmdir   # remove directory. (Note: The directory must be empty to be removed.)

    rmdir directoryname


ls   # list files in the current directory in a column format, with "/" after directories, and "*" after executable files.

    ls -F    #  places "/" after directories, and "*" after executable files.
    ls -l    #  gives a long listing, showing permissions, size, date last changed, etc.
    ls -a   #  include hidden files (filenames starting with a ".")
    ls -Fla   #  do all three options above at once.


cp   # copies a file, with name either absolute or relative.

    cp source-file destination-file


rm   # removes a file, but not a directory. See rmdir above.

    rm filename


rm -r   # removes a directory and recursively everything below the directory.
         # Useful, powerful, dangerous command!

    rm -r /home/wagner # this is a joke; on my UTSA Unix system, this removes everything I've got!
    su; rm -r / # need superuser password and a suicide hot-line


mv    # move or rename a file or a directory; this can be a dangerous command if you make typo!

    mv path/oldname path/newname    # can be used to rename if the path is the same.


more   # lists the contents of a file and pauses at each screenful.

    more filename    # a space gives the next screen, or q to quit.


cat   #  concatenates files and prints to the screen.

    cat filename   #  prints the contents of filename to the screen.
    cat -n filename   #  adds line numbers.
    cat filel file2 > file3    #  append file2 to the end of filel and save as file3.


script   # records all interactions in a window and saves in a file.

    script filename   #  to stop recording, use Ctrl-D.


history   #  lists previous commands by number.

    !n   #  executes the nth command when n is an integer, !4
    !v   #  exectues the last command that started with the character v
    !!   #  executes the last command 


Ctrl-c   # emergency stop for a process.


Ctrl-d   # end of file.


Sample Interactive Session (on Linux):
    User input in green, comments in orange, system responses in black. (Instead of using % to show the Unix prompt, here I show the actual prompts.)

    neal@linux:~> pwd                                   # current directory at home
    /home/neal
    neal@linux:~> ssh wagner@gateway19.cs.utsa.edu      # secure shell to UTSA
    wagner@gateway19.cs.utsa.edu's password:********    # type password
    [wagner@gateway19 ~]$ mkdir example                 # create a new directory
    [wagner@gateway19 ~]$ ls -F                         # list current directory contents
    example/                                            # actually, there's more junk here
    [wagner@gateway19 ~]$ cd example                    # change to the new directory
    [wagner@gateway19 ~/example]$ pwd                   # print the current path
    /home/wagner/example
    [wagner@gateway19 ~/example]$ vi goodmen.txt        # use vi to create new file
    iNow is the time for all good men                   # initial "i" is for "insert"
    to come to the aid of their party.
    [Esc] ZZ                                            # type "Esc" key, followed by "ZZ"
    [wagner@gateway19 ~/example]$ ls -l                 # long listing
    total 1                                             # 1 file in this directory
    -rw-r--r--  1 wagner news 69 Dec 30 17:24 goodmen.txt   # 69 chars in file
    [wagner@gateway19 ~/example]$ cat -n goodmen.txt    # print to terminal with line #s
         1  Now is the time for all good men
         2  to come to the aid of their party.
         3
    [wagner@gateway19 ~/example]$ rm goodment.txt       # remove file. Oops, misspelled!
    rm: cannot remove `goodment.txt': No such file or directory
    [wagner@gateway19 ~/example]$ rm goodmen.txt        # remove the file.
    [wagner@gateway19 ~/example]$ ls -l                 # check listing
    total 0
    [wagner@gateway19 ~/example]$ cd ..                 # back to parent directory
    [wagner@gateway19 ~]$ rmdir example                 # remove the directory
    [wagner@gateway19 ~]$ logout                        # terminate secure shell connect.
    Connection to gateway19.cs.utsa.edu closed.
    neal@linux:~>                                       # Ahhh.  Back at home!
    


Redirection of input and output: At the command line, Unix allows redirection operators: < , > , and >>

    < filename    #  directs "filename" in as if it were the standard input from the terminal.
    > filename    #  directs standard output to "filename" rather than the terminal.
    >> filename   append standard output to "filename".
    cat file1 file2 > combinedfile   #  "combinedfile" is just "file1" followed by "file2"

Pipes on the command line: Two commands can be connected with a "pipe" by putting | between them:
    who | sort   #  produces a sorted list of users of your computer
    ls -l | more   #  a long listing that lets you look one screen at a time

Revision date: 2013-01-12. (Please use ISO 8601, the International Standard Date and Time Notation.)