Interacting with a U..

Download Report

Transcript Interacting with a U..

Interacting with a UNIX computer:
Navigating through the directory tree
Convenience of the current directory
• We working with some file(s), it would be nice that we can
access the file with the least number of key strokes possible
• Fact in UNIX:
• All applications in UNIX can access a file by its
name when the file is in the current directory
(That's because the filename itself is the relative
path to the file - which will be explained later)
Convenience of the current directory (cont.)
• In order to take take advantage of this convenience, we
must change the current directory to the directory that
contains the desired file(s)
We will learn how to do this right now...
Task: Changing the current (working)
directory / Navigate the directory tree
• The command (= application) that is used to change the
current directory to another directory is:
cd NEW-Current-DIRECTORY-PATH
cd is an abbreviation of the words change directory
The New-Current-DIRECTORY-PATH can be an absolute
directory path or a relative directory path
Task: Changing the current directory /
Navigate the directory tree (cont.)
• Example of the "cd" command:
cd /home/cheung/cs170 (absolute path)
Task: Changing the current directory /
Navigate the directory tree (cont.)
• Example of the "cd" command:
cd cs170 (relative path)
Concept: Arguments of a command
• In the command below:
cd
/home/cheung/cs170
The first word cd is the name of the command (is also the
name of the computer application that will be run)
The second "word" /home/cheung/cs170 is the argument of
the command
Concept: Arguments of a command (cont.)
• Number of arguments:
• Some commands have 1 argument (E.g.: cd)
• Other commands have no (0) argument (E.g.:
pwd)
• Yet other commands have multiple (2 or more)
argument
Concept: Arguments of a command (cont.)
• Purpose of arguments:
• Arguments provide information to the command
(application)
• It allows the command (application) to be more
flexible (can do more)
Concept: Default argument
• Some commands (applications) have an assumed argument
when it is not given any argument
• Default arguments of the commands that you have learned
so far:
Command (application) Default argument
ls
the current directory
cd
the home directory
Concept: Default argument (cont.)
• Example: default argument for ls
When ls is not given any argument, it will list the content of
the current directory
Concept: Default argument (cont.)
• Example: default argument for cd
When cd is not given any argument, it will change the
working directory of the home directory
Concept: Special directories
• There are a number of special directories in UNIX that have
special notations
• Special directories:
Symbol
/
~
.
..
Directory represented by the symbol
The root directory
The home directory of the user
The current (working) directory
The parent directory of the current directory
The parent directory is the directory that is immediately
above the current directory in the directory tree.
The parent directory of the root directory / is itself (/)
Concept: Special directories (cont.)
• Examples
– Suppose the home directory is /home/cheung and
– current (working) directory is /home/cheung/cs170
then
Command
ls /
ls ~
ls .
ls ..
ls ../..
ls ../../..
ls ../../../..
Directory represented by the symbol
root directory
/home/cheung (= home directory of the user)
/home/cheung/cs170 (= current (working) directory)
/home/cheung (= the parent directory of the /home/cheung/cs170)
/home
/
/ (because the parent directory of the root directory / is itself
Exercise: Practice "ls" and "cd"
• Practice with the applet on the webpage:
• http://192.168.1.3/~cheung/teaching/web/170/Syllabus/02/
UNIX-dirs.html
Exercise: Practice "ls" and "cd"
• ls Exercise:
(The current directory = /home/bob)
• List the content of the current directory
• List the content of bob's "photos" directory using a
absolute path
• List the content of bob's "photos" directory using a
relative path
• List the content of sally's home directory using a
absolute path
• List the content of sally's home directory using a
relative path
Exercise: Practice "ls" and "cd“ (cont.)
1.
2.
3.
4.
5.
ls
ls /home/bob/photos
ls photos
ls /home/sally
ls ../sally
Exercise: Practice "ls" and "cd“ (cont.)
• cd Exercise:
(You starts with current directory = /home/bob)
• Starting in bob's home directory, use a absolute path to change the
current directory to sally's home directory Now list the content of
the current directory
• From sally's home directory, use a relative path to change the
current directory to bob's home directory Now list the content of
the current directory
• From bob's home directory, use a relative path to change the
current directory to sally's "music" directory Now list the content of
the current directory
• From sally's "music" directory, use a relative path to change the
current directory to bob's "photos" directory Now list the content of
the current directory
Exercise: Practice "ls" and "cd“ (cont.)
1.
2.
3.
4.
cd /home/sally
ls
cd ../bob
ls
cd ../sally/music
ls
cd ../../bob/photos
ls
Task: Create a directory
• The command (= application) that is used to create a new
directory:
mkdir DIRECTORY-PATH
You can use an absolute or a relative directory path.
Task: Create a directory (cont.)
• Example: create a sub-directory named hw1 inside
/home/cheung/cs170
Output of the ls command
Browsing the directory
with a File Explorer
Task: Create a directory (cont.)
Before the mkdir command, the cs170 directory contains:
myFile1 and myFiles2
After the mkdir command, the cs170 directory also
contains: hw1
Concept: command options
• Many of the UNIX commands (applications) have options
that can modify the behavior of the command (application)
• Options for a UNIX command are always specified in the
following manner:
• Options are usually single character items
• Options are specified immediately after the command
name
• You can specify more than one options
• The arguments for the command are given after the list
of options
• Options are preceded by the "−" symbol
Concept: command options (cont.)
• Example: what a UNIX command looks like
command -a -b -c arg1 arg2 arg3 arg4
The options of the command are: "-a -b -c" (3 options)
The arguments of the command are: "arg1 arg2 arg3 arg4"
(4 arguments)
Some commonly used options of the ls
command
• Some useful options of the ls command:
Option
-t
-r
-l
-F
Effect of the option
List files ordered by the time of creation (latest first)
List files in the reverse order
Long listing - give detailed information on files and directories
Append "/" to a directory name and "*" to the name of an executable application
Some commonly used options of the ls
command
• Example:
Output of the ls command
Browsing the directory
with a File Explorer
You can see that the output format is changed by the various options
specified to the ls command
The cd command has no options
• The cd command does not have any options....
Finding out the options and arguments of a
command
• To find out how to use a command, you can access the
manual page for a command by running the man
application.
• Example: ls
man ls
Finding out the options and arguments of a
command (cont.)
• You will see:
User Commands
NAME
ls(1)
ls - list contents of directory
SYNOPSIS
/usr/bin/ls [-aAbcCdeEfFghHilLmnopqrRstuvVx1@][file]...
DESCRIPTION
For each file that is a directory, ls lists the contents of the
directory. For each file that is an ordinary file, ls repeats its name
and any other information requested. The output is sorted
alphabetically by default. When no argument is given, the current
directory (.) is listed. When several arguments are given, the
arguments are first sorted appropriately, but file arguments
appear before directories and their contents.
Finding out the options and arguments of a
command (cont.)
• The synopsis shows you how to use the application (what
options and arguments you can specify).
You need to remember that the format of every UNIX
command is:
command-name -options
arguments
Finding out the options and arguments of a
command (cont.)
Now let's read the synopsis:
/usr/bin/ls [-aAbcCdeEfFghHilLmnopqrRstuvVx1@]
[file]...
meaning:
1. The absolute path of the ls application is
/usr/bin/ls
2. The list -aAbcCdeEfFghHilLmnopqrRstuvVx1@
are the options
3. The word file is the argument.
Finding out the options and arguments of a
command (cont.)
The [] around [file] means that you may specify the "file"
as an argument (and you may leave it out)
The ... after "[file]" means that you may repeat the
argument again (with another name for file) or you may
stop.
Some possible forms that you can run ls as:
ls
or: ls A
or: ls -l A
or: ls -l -t A
or: ls A B
Task: Delete a directory
• The command (= application) that is used to delete a
directory is:
rmdir DIRECTORY-PATH
You can use an absolute or a relative directory path.
Task: Delete a directory (cont.)
• Example: delete the sub-directory named hw1 inside
/home/cheung/cs170
(I made a mistake in the first attempt, I tried to remove the
directory /home/cheung/hw1 which did not exists. I forgot
the cs170 part)
Task: Delete a directory (cont.)
• Mistakes are less likely by using a relative path:
Task: Delete a directory (cont.)
• Note:
• The UNIX system will not allow you to delete a directory if the
directory is not empty
So if you have created some files inside the hw1 directory,
you cannot delete it.
You will need to delete all files (and sub-directories) from a
directory before you can delete that directory.
• Later, you will learn how to delete files from a directory.
Task: Rename a directory
• The command (= application) that is used to rename a
directory is:
mv Old-Name New-Name
mv is the acronym for move
Task: Rename a directory (cont.)
• Extremely important note:
• The directory New-Name must not exist
Otherwise, you will perform a entirely different
operation !!!
(Explained below)
It is advisable to use a relative directory path to rename a
directory (you will know why very soon)
Task: Rename a directory (cont.)
• Example: rename the sub-directory named hw1 inside
/home/cheung/cs170 to proj1
(I used the −F option to let you see which names are files and
which are directories)
Task: Move a directory to somewhere else
• Suppose you want to create a directory handouts inside
/home/cheung/cs170/proj1
But instead, you created the directory inside
/home/cheung/cs170 (inside a wrong folder (directory))
• Graphically:
Task: Move a directory to somewhere else
(cont.)
• Solution:
• move the directory handouts into the folder
(directory) proj1
Task: Move a directory to somewhere else
(cont.)
• The command (= application) that is used to move a
directory X into some directory D is:
mv X D
(This has the same effect by using a file browser and do
this: drag the item X into the folder D )
Task: Move a directory to somewhere else
(cont.)
• Extremely important note:
• The directory D must already exist
Otherwise, you will perform a rename operation !!!
(You would have renamed the directory X to D)
Task: Move a directory to somewhere else
(cont.)
• Example: move the directory named handouts inside
/home/cheung/cs170/proj1
Command line vs. Drag and drop
• Drag and drop is easy to learn
But it takes a long time go to one place and drag the thing
into another folder.
• Using a command line is hyper fast, but: only after you
have become familiar with the Computer System
Command line vs. Drag and drop (cont.)
• Bottom line
• If you want to become a Computer Expert, you have to
learn to do things fast (or else, your career working with
a computer will be painfully slow and unproductive)
• You will need to learn command line interaction with a
computer if you want to be a Computer Expert