Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interacting with a UNIX computer: Navigating through the directory tree.

Similar presentations


Presentation on theme: "Interacting with a UNIX computer: Navigating through the directory tree."— Presentation transcript:

1 Interacting with a UNIX computer: Navigating through the directory tree

2 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)

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

4 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

5 Task: Changing the current directory / Navigate the directory tree (cont.) Example of the "cd" command: cd /home/cheung/cs170 (absolute path)

6 Task: Changing the current directory / Navigate the directory tree (cont.) Example of the "cd" command: cd cs170 (relative path)

7 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

8 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

9 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)

10 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:

11 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

12 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

13 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 (/)

14 Concept: Special directories (cont.) Examples –Suppose the home directory is /home/cheung and –current (working) directory is /home/cheung/cs170 then

15 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

16 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

17 Exercise: Practice "ls" and "cd“ (cont.) 1.ls 2.ls /home/bob/photos 3.ls photos 4.ls /home/sally 5.ls../sally

18 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

19 Exercise: Practice "ls" and "cd“ (cont.) 1.cd /home/sally ls 2.cd../bob ls 3.cd../sally/music ls 4.cd../../bob/photos ls

20 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

21 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

22 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

23 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

24 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 -c arg1 arg2 arg3 arg4

25 Some commonly used options of the ls command Some useful options of the ls command:

26 Some commonly used options of the ls command Example: You can see that the output format is changed by the various options specified to the ls command Output of the ls command Browsing the directory with a File Explorer

27 The cd command has no options The cd command does not have any options....

28 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

29 Finding out the options and arguments of a command (cont.) You will see: 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.

30 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

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

32 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 or: ls -l A B

33 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

34 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)

35 Task: Delete a directory (cont.) Mistakes are less likely by using a relative path:

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

37 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

38 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 exist Otherwise, you will perform a entirely different operation !!! (Explained below)

39 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)

40 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:

41 Task: Move a directory to somewhere else (cont.) Solution: move the directory handouts into the folder (directory) proj1

42 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 X D

43 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)

44 Task: Move a directory to somewhere else (cont.) Example: move the directory named handouts inside /home/cheung/cs170/proj1

45 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

46 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


Download ppt "Interacting with a UNIX computer: Navigating through the directory tree."

Similar presentations


Ads by Google