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 orange.


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

    man printf


pwd   # print working directory: give absolute path from root to current directory.


cd   # change directory.

    cd path/directory   # where path/directory is an absolute or relative pathname.
    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 current directory, with "/" for directories, and "*" for executables.

    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 # a joke; at UTSA, 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 file1 file2 > file3    #  append file2 to the end of file1 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   #  executes 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         # print current working directory
    /home/neal
    neal@linux:~> ssh wagner@elk02.cs.utsa.edu  # secure shell to UTSA
    wagner@elk02.cs.utsa.edu's password: ****** # type password
    elk02:~> mkdir example    # create a new directory
    elk02:~> ls -F            # list current directory contents
    example/                  # (actually, there's lots more junk here)
    elk02:~> cd example       # change to the new directory
    elk02:~> pwd              # print the current path
    /home/wagner/example
    elk02:~> 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"
    elk02:~> ls -l            # long listing
    total 1                   # one file in this directory
    -rw-r--r--  1 wagner news 69 Dec 30 17:24 goodmen.txt   # 69 chars in file
    elk02:~> 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
    elk02:~> rm goodment.txt  # remove file. Oops, misspelled!
    rm: cannot remove `goodment.txt': No such file or directory
    elk02:~> rm goodmen.txt   # remove the file.
    elk02:~> ls -l            # check listing
    total 0
    elk02:~> cd ..            # back to parent directory
    elk02:~> rmdir example    # remove the directory
    elk02:~> logout           # terminate secure shell connect.
    Connection to elk02.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: 2014-05-21. Please use ISO 8601, the International Standard.)