1 / 47

Interacting with a UNIX computer: Navigating through the directory tree

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

yori
Download Presentation

Interacting with a UNIX computer: Navigating through the directory tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 (working) directory / Navigate the directory tree (cont.) • Example of the "cd" command:    cd /home/cheung/cs170   (absolute path)

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

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

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

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

  10. 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)

  11. 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:

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

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

  14. 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 (/)

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

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

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

  18. Exercise: Practice "ls" and "cd“ (cont.) • ls • ls /home/bob/photos • ls photos • ls /home/sally • ls ../sally

  19. 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:

  20. Exercise: Practice "ls" and "cd“ (cont.) • cd /home/sallyls • cd ../bobls • cd ../sally/musicls • cd ../../bob/photosls

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

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

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

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

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

  26. Some commonly used options of the ls command • Some useful options of the ls command:

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

  28. The cd command has no options • The cd command does not have any options....

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

  30. 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:

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

  32. 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:

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

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

  35. 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)

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

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

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

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

  40. 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)

  41. 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:

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

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

  44. 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)

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

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

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

More Related