logoISU  

CS 256 - Principles of Structured Design

Summer 2021

Week 1:

Video and wiki links to setup your environment:

If you use a recent version of Windows 10 or MacOS or Linux, you can access the CS server using the ssh client via a terminal (command prompt) window:

ssh cs256##@cs.indstate.edu

where cs256## is your given CS256 account name. The first time you attempt to connect to the CS server you will be asked to accept the ssh key:

The authenticity of host 'cs.indstate.edu (139.102.14.201)' can't be established.
ECDSA key fingerprint is SHA256:gjTolt2aVb1A/bRvzt4grTFEWT5OfjOq6FTPxcf5VHY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 

This is normal, just say yes and continue to the password prompt and enter the password you've been provided to login:

cs25600@cs.indstate.edu's password:

You will not see anything as you type, this is a normal security measure to prevent any onlookers from seeing how long your password is.

Unix commands:

The development environment for this class will be the Unix/Linux environment where the system is interacted with via the terminal command line interface. When using this interface commands are typed at a command prompt, which for most of you will look something like:

cs25600@cs:/u1/class/cs25600>

Reading from left to right, includes your username@host (the machine you're logged into, in this case, the CS server,) then a colon followed by the path of the current working directory, which is the file path to your home directory on the server. This path will change as you use the cd command to change your working directory. When specifying paths you can use the special character . (dot or period) to specify your current working directory (i.e. the directory displayed in your prompt.)

/
└── u1
    └── class
        └── cs25600


Where your home directory is inside of the class directory which is inside of the u1 directory which is located at the root of the file-system (/).

Your prompt can be modified by editing the .cshrc file in your home directory, which is also the file to change if you want to permanently add aliases or change other aspects of your shell environment.

The commands you will be expected to know and use in this class are in brief:

Command What is does
passwd Change your password
* man Get help from the online manual
* handin Assignment checkout and handin system
* ls List files in a directory
* cd Change your current working directory
pwd Print current working directory path
* cp Copy files and/or directories
* mv Move/rename files and/or directories
* rm Remove/delete files and/or directories
* mkdir Make new empty directories
rmdir Remove an empty directory
cat Output a file or files to the screen
* more View a file or output one screen at a time
less Like more, but less
* chmod Change file permissions

Changing your password:

To change your password, at a terminal prompt, type:

passwd

  • You will be prompted for your old password then your new password two times.
  • You will not see the characters you type (this is normal, to prevent someone nearby from seeing how long your password is.)

Unix commands are words (usually) separated by spaces terminated by a newline or semicolon (;) The first word is the "command" name, the words, if any, that follow it are its "arguments" or "command line parameters", that tell the command what it needs to do.

command-name additional-parameters...

  • parameter words that start with a dash (-) are command line switches. They modify how the command itself works.
    Examples: -a, -r, etc.
  • Double dash arguments are full word switches, i.e.: --help, --version, etc.
Notes conventions:
word Replace word with what word represents.
[word] word is optional.
> at beginning of line represents the "command prompt".
!! Don't include it in the command itself !!

Get help on from the on-line manual pages.

You can get help on specific commands or topics using the Unix online manual, accessed via the man command. An optional section number can be inserted between the man command and the name of the command or topic you want to disambiguate between commands of the same name in different sections (for example there is both a Unix printf command (found in section 1) and the C printf function (found in section 3).

man [section#] command|system-call|C_library_function|config_file|topic

Examples What you get
man man Manual page for man itself
man ls Manual page for the ls command
man 3 printf Manual page for the C printf function

Handin

Your assignments will be checked out and submitted using the local handin system. You can read more about how to use handin via the CS wiki:

List files in a directory (folder):

> ls [-a] [-l] [--help] [path]

switches
-a List all files, including hidden files: files beginning with a dot (.)
-l Long listing
Examples: What you get
ls Short listing of all non-hidden files in the current directory.
ls -a Short listing of all files in the current directory.
ls -l Long listing of all non-hidden files in the current directory.
ls -l /etc Long listing of all the non-hidden files in the /etc directory

'ls -l' output decoded:

    -rw-r--r-- 1 user group   283 Jan 13 18:52 file
    │╲ ╱╲ ╱╲ ╱ │ ╲________╱  File ╲__________╱  └─── File-name
    │ U  G  O  │     │       Size      └──────────── Last modified time
    │  perms   │     └────────────────────────────── User and Group owners of the file
    │          └──────────────────────────────────── Hard link count (ignore it)
    └─────────────────────────────────────────────── File type
File types
- A regular file (such as a text file, i.e. files that contain user data)
d A directory (or folder)
l A symbolic link (a pointer to another file/directory

There are other file types, but as a normal user, we are not concerned with those.

Permission groups
U User permissions (used if the owner is accessing the file)
G Group permissions (used if a group owner is accessing a file)
O Other permissions (used in all other cases)

Permissions are scanned from left to right and the first permission group that applies is the permission group used to determine access to the file/directory. The permissions for each group are as follows:

Permissions within each permission group:

  • Permissions are in the order rwx (Read/Write/Execute(search)), if there is a dash (-) instead of rw or x then there is no permission allowed for that kind of access. i.e. if you see r-x there is no write access allowed, but read and execute are allowed.
Permissions
r Readable (file can be read or a directory can be listed )
w writable / modifiable (file can be modified and files can be added/removed from directories
x Executable (for files) / Search-able (directories, i.e. can 'cd' into)
- There is no permission for this specific permission.

Other commands:

> tree [directory]

  • Display a indented directory tree.
    Examples:
    tree ~

    • Displays a tree of your home directory.

    tree /

    • Displays the directory tree of the entire file-system. This will likely take a very long time and is really a mistake to attempt. You can abort a running program using the Ctrl-C key combination on your keyboard.

> cd [dir]

  • Change working directory. Your working directory (also referred to as . (dot)) is the starting point for all relative paths (i.e. paths that do not start with a slash (/)). It is also the directory that will be listed by ls by default.

    examples
    cd Changes to your home directory
    cd ~ Same thing
    cd .. Moves to the parent directory
    cd / Moves to the root file-system directory.

> pwd

  • Prints the absolute path for the current working directory.

Special files and directories:

"file" What it represents
/ root file-system directory
. Current directory
.. Parent directory
.* All "hidden" files (i.e. files starting with a dot (.), not normally listed unless -a option used.)
~ (tilde character) Your home directory.
~user user's home directory

File paths

Paths are directories and/or a file separated by forward slashes (/). Slashes therefor cannot be in a file-name. There are two types of "paths":

  • absolute paths, which start with a leading forward slash (/), and absolutely define the location of a file or directory in the file-system regardless of what the users current working directory is.
  • relative paths which do not start with a leading slash and specify files and directories relative to the current working directory. To compute the absolute path from given a relative path, prepend the absolute path of the current working directory to the relative path

File management commands:

> cp [-r] source... destination

  • Copy files from source to destination. If destination doesn't exist the file will be copied as destination. If destination is a directory the file or files will be copied into that directory with the same name as their source name.
    -r = Copy files/directories recursively. This will copy a directory and its entire contents to the destination. You cannot normally copy a directory without this option.
Examples
cp -r dir1 dir2 ~/dest/ Recursively copies all of dir1 and dir2 into the directory dest in your home directory
cp a.txt b.txt Copies a.txt as b.txt
cp a.txt b.txt c Copies text files a.txt and b.txt to directory c.

> mv source... destination

  • Rename / move files, works a lot like cp, but moves instead of copies (think cut-and-paste instead of copy and paste)
Examples:
mv a.txt b.txt Renames a.txt as b.txt
mv a.txt b.txt c Moves a.txt and b.txt into directory c.

> rm [-r] file...

  • Remove (delete) a file or files
    -r = Recursively remove files and directories. DANGEROUS.
Examples
rm a.txt b.txt Removes the files a.txt and b.txt.
rm -r c Completely removes the directory c.

> mkdir [-p] directory...

  • Make a new empty directory
    -p = Make parent directories if they don't exist.

> rmdir directory...

  • Remove an empty directory

Viewing files:

> cat [file(s)]

  • Prints (entire) file(s) to the screen, one after the other if more than one file is specified.

> more [file(s)]
> less [file(s)]

  • Used to view files or the output of another program (if followed by the pipe (|) operator) one screen page at a time.

    Examples:
    less file.txt Views the file file.txt one screen at a time
    ls -la | less Views the output of ls -la one screen at a time
    tree | less Output of the tree command one screen-full at a time

Keys (when viewing a file or man page):

Key What it does
q or Q Quit
space Go to next page
p or P Go to previous page
Arrow-keys Move up/down/left/right
/text Search for text in the document
/ Repeat last search.

Editors:

Text mode editors are editors that can be used in the terminal and do not require a graphical user interface. Those available are:

Editors
vi / vim Use vimtutor to learn vim.
emacs / jove Emacs based editors. Use teachjove to learn jove
ne The nice editor, use the escape key (twice) or F1 to pop up a drop-down menu system
nano / pico Includes simple menu at bottom, easy to learn and use.
In the nano/pico menu's the ^ represents the Ctrl-key, so ^X -> Ctrl-X

I recommend ne, jove or vim over nano or pico which are OK text editors, but not really geared toward source code editing. ne is probably the easiest to pick up as you need only remember to hit the escape key twice or F1 (if that works with your terminal setup) to bring up a drop-down menu to access the editors functions, it also includes nice features such as code highlighting / colorization and auto code indentation functionality.

Graphical editors:

Editors
kate Primarily a graphical Linux code/text editor, also available for Windows
komodo Like kate, but lets you display HTML in a sub-window, cross-platform: Download
atom Another cross-platform graphical editor. Download

You will need to use an editor to edit C source files and other text files for this class. You should invest some time in learning to use a good code editor which has things like code syntax highlighting and auto formatting.

Changing file permissions:

We likely will not be needing to change the permissions of files or directories in this class often, but occasionally it may be necessary to do so. We can do this with the chmod command.

> chmod [-R] mode file(s)...

  • Set permissions on file or files. Mode has two possible forms, octal (three, base-8 numbers) or ASCII:

    Group (1+) Mode (one of) Permssions (0+)
    u (user) + (add) r (read)
    g (group) - (subtract) w (write)
    o (other) = (set equal to) x (execute/search)
    a (all) t (sticky bit)
    s (setuid bit)
  • -R = Set permissions recursively (i.e. set the permissions on a directory and all its contents.)

Octal permissions expicitly set the permissions on a file. To compute the octal permissions you need three octal numbers (in the range 0-7,) one for each permission group. Each octal value is computed based on the which permissions are set:

Permission Value
r 4
w 2
x 1
- 0

Thus: rwx is 7 (4+2+1), r-x is 5 (4+0+1) and r-- is 4. Thus 754 → rwxr-xr--.

examples:

chmod 755 ~/public_html

  • Makes your public_html directory world accessible. 755 → rwxr-xr-x

chmod ug=rw,o=r file

  • Makes file read/write for user and group and read only for everyone else. Note that there is no spaces anywhere in the mode field, only commas (,) separating permission groups.

chmod a+x file

  • Makes file executable for everyone. Also note that the mode must come immediately after the chmod command itself, i.e. it needs to be the first parameter, it cannot come after the names of the files to be modified.

chmod -R og= dir

  • Removes other and group access to all the files in the directory dir. Note that any permission group not modified is left as-is. This is not possible to do when using an octal mode.

Network commands:

> ssh [user@]hostname

  • Secure login to a remote machine
    example:
    ssh cs25600@cs.indstate.edu

> sftp [user@]hostname

  • Secure FTP client (transfer files to/from the local host to the remote host)
    (interactive, like an ftp client)

> rsync [-a] [-v] source destination

  • Keep two directories in sync (transfers only changed files.) Source and or destination may be a local directory path or a remote path in the form:
    [user@]hostname:[remote-file-path]
    -a = archive mode (keeps permissions, timestamps and ownerships in sync as well)
    -v = Verbosity (shows what it is doing)

  • Examples:
    rsync -av . sbaker@cs.indstate.edu:dir/

    • Sends the contents of the local current working directory to the directory ~/dir on the machine cs.indstate.edu under the sbaker account.

    rsync -av sbaker@cs.indstate.edu:dir ~/

    • Syncs the directory dir on cs.indstate.edu under the sbaker account as dir in our local home directory.

Command aliases

> alias alias-name command-to-actually-run

  • Defines a command alias where alias-name is replaced by command-to-actually-run when alias-name is.
    Examples:
    alias d "ls -la"

    • Now d will execute ls -la. Any parameters following d are appended to the real ls -la command.

    alias df 'df -h'

    • Now the df command will always include the -h option.

To avoid using an alias you can use a backslash (\) at the beginning of the command to use the real command instead of an alias for that command.

To make an alias permanent you will need to edit your ~/.cshrc file with a text editor and add the new or changed alias to it.