470 likes | 539 Views
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:.
E N D
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 is an abbreviation of the words change directory The New-Current-DIRECTORY-PATH can be an absolute directory path or a relative directory path cd NEW-Current-DIRECTORY-PATH
Task: Changing the current (working) directory / Navigate the directory tree (cont.) • Example of the "cd" command: cd /home/cheung/cs170 (absolute path)
Task: Changing the current (working) directory / Navigate the directory tree (cont.) • Example of the "cd" command: cd cs170 (relative path)
Lab exercise • Perform these tasks now: • List the content of your current directory • Change the current working directory of the terminal window to your cs170 directory (do this only if you see cs170 in the listing) • Make sure the directory cs170 is in the listing • If the directory cs170 is not in the listing, tell the Teaching Assistant !!!
Concept: Arguments of a command • In the command below: 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 cd /home/cheung/cs170
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:
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: 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
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.) • ls • ls /home/bob/photos • ls photos • ls /home/sally • ls ../sally
Exercise: Practice "ls" and "cd“ (cont.) • (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 • cd Exercise:
Exercise: Practice "ls" and "cd“ (cont.) • cd /home/sallyls • cd ../bobls • cd ../sally/musicls • cd ../../bob/photosls
Task: Create a directory • The command (= application) that is used to create a new directory: You can use an absolute or a relative directory path. mkdir DIRECTORY-PATH
Task: Create a directory (cont.) • Example: create a sub-directory named hw1 inside /home/cheung/cs170 Browsing the directory with a File Explorer Output of the ls command
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 usuallysingle 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 The options of the command are: "-a -b -c" (3 options) The arguments of the command are: "arg1 arg2 arg3 arg4" (4 arguments) command -a -b -carg1 arg2 arg3 arg4
Some commonly used options of the ls command • Some useful options of the ls command:
Some commonly used options of the ls command Browsing the directory with a File Explorer Output of the ls command • Example: 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.) User Commands ls(1) NAME 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. • You will see:
Finding out the options and arguments of a command • 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 -optionsarguments
Finding out the options and arguments of a command • /usr/bin/ls [-aAbcCdeEfFghHilLmnopqrRstuvVx1@][file]... • meaning: • The absolute path of the ls application is /usr/bin/ls • The list -aAbcCdeEfFghHilLmnopqrRstuvVx1@ are the options • The word file is the argument. Now let's read the synopsis:
Finding out the options and arguments of a command 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 or: ls -l A B
Task: Delete a directory • The command (= application) that is used to delete a directory is: You can use an absolute or a relative directory path. rmdir 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 is the acronym for move mv Old-Name New-Name
Task: Rename a directory (cont.) • Extremely important note: It is advisable to use a relative directory path to rename a directory (you will know why very soon) • The directory New-Name must not existOtherwise, you will perform a entirely different operation !!! • (Explained below)
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: (This has the same effect by using a file browser and do this: drag the item X into the folder D ) mv XD
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