Changes

Jump to: navigation, search

ULI101 Week 3

10,751 bytes added, 09:15, 31 August 2017
Created page with "* File name expansion * Shell basics * Command execution in detail * Recalling and editing previous commands * Quoting = Pathnames = * A pathname is a list of names that wil..."
* File name expansion
* Shell basics
* Command execution in detail
* Recalling and editing previous commands
* Quoting

= Pathnames =

* A pathname is a list of names that will lead to a file.
* Essentially they are directories, but a file name itself is a path as well
* The concept of a pathname relates to every operating system including Unix, Linux, MS-DOS, MS-Windows, Apple-Macintosh, etc.

<source lang="bash"># Directory pathname:
/home/username/ics124/assignments/

# File pathname:
/home/username/ops224/assignments/assn1.txt
</source>
= Absolute vs Relative Pathnames =

=== Absolute Pathname ===

* A pathname that begins from root.
* The pathname begins with a forward slash, for example: <code>/home/someuser/unx122</code>

=== Relative Pathname ===

* A pathname that is &quot;relative&quot; to the location of the current or &quot;working&quot; directory.
* For example, if we are in your home directory, issuing the command <code>mkdir uli101</code> will create the uli101 directory in your home directory.
* Rules:

<ol>
<li><p>A relative pathname does NOT begin with a slash.</p></li>
<li><p>A relative pathname can use the following symbols can be used at the beginning:</p>
<ul>
<li><code>..</code> parent directory (up one directory level)</li>
<li><code>.</code> current directory</li></ul>
</li>
<li><p>Not all relative pathnames begin with <code>..</code></p></li>
<li><p>When using relative pathname, make sure you know your present working directory.</p>
<source lang="bash"># Change to another directory branch from parent directory: cd
../ipc144

# copy sample.c file from joe.professor's home directory to your current directory:
cp ../joe.professor/uli101/sample.c .
</source></li></ol>

= Relative-to-Home Pathnames =

=== You can specify a pathname as relative-to-home by using a tilde and slash at the start, e.g. <code>~/uli101/notes.html</code> ===

=== The tilde ~ is replaced by your home directory (typically ''home/your.account'') to make the pathname absolute. ===

=== You can immediately place a username after the tilde to represent another user’s home directory, for example: ===

* <code>~jane.somebody</code> expands to <code>/home/jane.somebody</code> but <code>~</code> expands to <code>/home/your_home_dir</code>
* similarly <code>~uli101</code> expands to <code>/home/uli101</code> but <code>~/uli101</code> expands to <code>/home/your_home_dir/uli101</code>

= Which Type of Pathname to Use? =

So far, we have been given many different types of pathnames that we can use for regular files and directories:

* Absolute pathname (starts with <code>/</code> )
* Relative pathname (doesn’t start with <code>/</code>)
* Relative-to-home pathname (starts with <code>~</code>)

You can decide which pathname type to use to make it more convenient (eg relative - usually less typing or absolute if you don’t know what directory you are currently located in).

= Making Directories =

Building directories is similar in approach to building a house:

* Begins from a foundation (eg home directory).
* Need to build in proper order (add on addition to house in right location). Use a logical scheme.
* When building directories from different locations, must provide proper absolute or relative pathname.

= Planning Directories =

Good directory organization requires planning:

* Group information together logically.
* Plan for the future: use dated directories where appropriate (<code>~/christmas/2001</code>, <code>/christmas/2002</code>)
* Too few directories = excessive number of files in each; too many directories = long pathnames.

== Where to build directories? ==

* Want to build a directory called tmp that branches off of your home directory?
* Verify which directory you are located (either look at directory from command prompt or issue the command pwd)
* Type mkdir tmp at the Unix prompt, followed by ENTER
* Optionally you can verify that directory has been created using ls or ls -ld commands)

== Creating Parent Directories ==

By default, a directory cannot be created in a nonexistent location - it needs a parent directory To create directory paths with parent directories that do not exist (using a single command) use the <code>-p</code> option for the mkdir command

<source lang="bash">
# This would create the parent directory mur and then the child directory dir1.
# The -p means "create all the directories in the Path".
$ mkdir -p mur/dir1

</source>
== Removing Directories ==

Removing directories is reverse order of building directories

* Issue command rmdir directory
* rmdir cannot remove directories containing files or other subdirectories.
* rmdir cannot remove directories that are anyone's current directory.
* Need to step back to at least parent directory to remove an empty directory.

=== Removing Sub-trees ===

* To remove a sub-tree (a directory and all of its contents including sub-directories) use <code>rm -r</code> directory (or <code>rm -R</code> directory).
* The can use the <code>rm -rf</code> command (<code>-f = force</code>) to complete delete files and directories recursiverly, even if they are protected from delete

<blockquote>* Remove files only if you are absolutely sure what you are doing.
* rm -r can erase large numbers of files very quickly. Use with extreme care!
* Backing up your data is a very good idea.
</blockquote>
= Filename Expansion =

* Many of the commands discussed so far make reference to a specific filename - e.g. and regular file to store data, or a directory.
* Sometimes the user may not know the exact name of a file, or the user wants to use a command to apply to a number of files that have a similar name

For example: <code>work.txt, work2.txt, work3.txt</code>

* Special characters can be used to expand a general filename and use them if they match. You may have heard about “Wildcard Characters” - this is a similar concept.
* Symbols:

{|
| * (star/asterisk)
| Represents zero or more of any characters.
|-
| ? (question mark)
| Represents any single character.
|-
| [ ] (character class)
| Represents a single character, any of the list inside of the brackets. Placing a ! Symbol after first square bracket means “opposite”). Ranges such as [a-z] or [0-3] are supported.
|}

* To demonstrate filename expansion, let’s assume the following regular files are contained in our current directory:

<source lang="bash">$ touch work1.txt work2.txt work3.txt work4.c worka.txt working.txt
$ ls
work1.txt work2.txt work3.txt work4.c worka.txt working.txt
</source>
* Note the results from using filename expansion:

<source lang="bash">$ ls work*
work1.txt work2.txt work3.txt work4.c worka.txt working.txt

$ ls work?.txt
work1.txt work2.txt work3.txt worka.txt

$ ls work[1-3].txt
work1.txt work2.txt work3.txt

$ ls work[!1-3].txt worka.txt
</source>
= UNIX shell =

* Command interpreter for UNIX
* Acts as a mediator between user and UNIX kernel
* Processes and/or executes user commands
* More than one command can be executed on one command line when separated by a semi-colon
* You will be learning approx. 30 Unix commands in this course. This is a small, compared to the the 1000+ Unix commands out there
* The term command and utility mean the same in Unix UNIX shell
* There are several kinds of shells available for UNIX
* Most popular shells are:

C shell (this is not the C programming language)

Korn shell - used with Unix

Linux machines most often use the BASH shell (Bourne-Again Shell)

* Each user on one machine can run a different shell
* UNIX scripting = UNIX shell programming

= Why command line? =

* Why don’t we just use the GUI (KDE, Gnome or some other window manager)? - GUI may not always be available
* What if something is broken?
* What if you are connecting through a terminal remotely? - GUI is for regular users
* Many administrative tools are hard to find in the menus - Command line is more efficient
* Tasks are completed faster
* Less system resources are wasted - Command line allows you to automate repeating tasks through scripting
* Writing scripts requires you to know commands

= Command Execution =

* While command is being executed the shell waits for it to finish
* This state is called sleep
* When the command finishes executing the shell brings back the prompt
* It is possible to get the command prompt before the command finishes
* This requires executing a process in the background

= Command Line Syntax =

* A line which includes UNIX commands, program and shell script names and their arguments is called a command line
* Typical command line execution would include:
* Command line parsing
* Breaking it up into tokens
* Executing tokens
* Command line tokens are separated by whitespace
* Command line is actually executed when the Enter key is pressed

= Command Editing =

* Previously executed commands can be recalled
* The Bourne shell uses the up/down arrow keys to accomplish that
* Other shells may use some other mechanism, for example Korn shell uses vi-style command editing
* Recalled commands can be easily edited before re-executing
* Useful BASH keyboard shortcuts:

{|
| Go to the beginning of the line
| CTRL+A
|-
| Go to the end of the line
| CTRL+E
|-
| Erase Characters
| Backspace or CTRL-Backspace or CTRL-h
|-
| Delete a word before the cursor
| CTRL-w
|-
| Delete everything from to the beginning of line
| CTRL-u
|-
| Clear Screen
| CTRL-l
|-
| Search for a keyword in previous commands
| CTRL+R
|-
| Auto complete file/directory names
| Tab
|}

= Quoting in UNIX =

* Sometimes it may be necessary to use characters that have special meaning to the shell
* In such cases such characters may need to be quoted
* There are several ways of quoting special characters in UNIX, including:

{|
| Double quotes (“ “)
| quote a group of characters
|-
| Single quotes (‘ ‘)
| quote a group of characters
|-
| Backslash quote (
| quote the one character immediately following the backslash
|}

* Can prevent variable substitution when the $ character is quoted

<source lang="bash"># shows all filenames in your pwd
$ echo *

# displays the character *
$ echo \*
</source>
* To quote a   another is used (<br />
) ‘ ‘ Quotes
* Forward single quote - different than the back tick (backward single quote)
* Quotes all that is inside, preventing wildcard and variable substitution

<source lang="bash"># shows all hidden files in pwd
echo .*

# displays the two characters '.' and '*'
echo '.*'
</source>
= Double Quoting =

* Commands such as echo can have their arguments quoted using double quotes
* Such quoting can preserve and/or include whitespace
* Variable substitution takes place
* Double quotes do not:
* Prevent shell variable substitution
* Stop escape characters interpretation
221
edits

Navigation menu