1. Trang chủ
  2. » Công Nghệ Thông Tin

Accessing Files and Directories

19 325 2
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 19
Dung lượng 81,21 KB

Nội dung

Accessing Files and Directories 5 Objectives Upon completion of this module you should be able to: • Contrast absolute and relative pathnames • Access files and directories within the file structure using absolute and relative pathnames • Identify and describe the parts of a command line • Access files and directories within the file structure using pathname abbreviations • List the contents of directories and their file types • Identify various metacharacters • Enter more than one command on a single command line • Demonstrate the use of wildcard characters Discussion – How does the Linux system know how to find files and directories? File System Structure Figure 5-1 shows a typical Linux system file structure; you will refer to this again later in this course. Figure 5-1 Linux File System Structure Pathnames A pathname uniquely identifies a particular file or directory by specifying its location. Pathnames are similar to a road map or a set of directions that tells you how to get from one place in the directory hierarchy to another. Note – Depending on the setup of your system, home directories can be found in either /home or /export/home. For the purposes of this discussion, /home is used. The slashes within the pathnames are delimiters between object names. A slash in the first position represents the / (root) directory. For example: /home/user2/dir1/coffees root directory Delimiters Figure 5-2 Pathname With Delimiters Notes  The concept of pathnames is critical to the student’s understanding of how to access files in the file structure. Pathnames identify every file in the file structure.  Briefly describe the Linux file structure in terms of a hierarchy of directories, subdirectories, and files. The file cabinet analogy works well with the Linux computing environment file structure. If the file cabinet analogy does not work for everyone, you might draw a “tree” structure. Use root (/) and some directories to illustrate the root, branches, limbs, and so on.  At some point it should be noted that this is not what the students’ file structure will look like back at the office. In other words, the file names, and possibly the location of the home directory, will be different in their work environment. The structure shown is an example only.  If the classroom lab is set up in such a way that students’ home directories are under /export/home ,explain that when home directories reside on the local system they are put, by convention, under /export/home .When home directories are provided by a home directory server, by convention, they are found under /home. There are two types of pathnames: absolute and relative. Absolute Pathname An absolute pathname specifies a file or directory in relation to the entire Linux file hierarchy. The hierarchy begins at the / (root) directory. Absolute pathnames always: • Start at the root (/) directory and list each directory along the path to the destination file (or directory) • Use a slash (/) between each directory name in the path to indicate different directories Refer to Figure 5-1 for a visual representation of the following pathnames: • Absolute pathname to user2 /home/user2 • Absolute pathname to dir1 /home/user2/dir1 • Absolute pathname to coffees /home/user2/dir1/coffees Relative Pathname A relative pathname describes the location of a file or directory as it relates to the current directory. If you are in a directory and you want to move down the directory tree, it is not necessary to type the absolute pathname. You can just type the path starting with the name of the next directory in the directory structure. If a pathname does not begin with a slash, it is a relative pathname. Relative pathnames are useful because they are shorter than absolute pathnames. Use Figure 5-1 to support these examples: • The current directory /home • Relative pathname to user2 user2 • Relative pathname to dir1 user2/dir1 • Relative pathname to coffees user2/dir1/coffees Command-Line Syntax When using a terminal window, a shell prompt appears on the screen. For the Bourne and Korn shells, the shell prompt for a user is a dollar sign ($). For a C shell user, the prompt is a percent sign (%). At the shell prompt, you can type commands. Commands are instructions which tell the system to perform an action. The general format for Linux commands is: $ command [option (s)] [argument[s]] command Executable (specifies what you want the system to do) options Modify the executable (specifies how you want the command run) argument File or directory, including the path name, or text. If a pathname is not specified for a file or directory argument, then the operating system will use the current directory. • A space must be used as a delimiter between each part of the command line. • Up to 256 characters can be entered on a single command line. • Many commands do not require all three parts. Some examples are: $ cal (Command only) $ cal 1987 (Command and argument) $ date (Command only) $ date -u (Command and option) $ banner "hi there" (Command and argument) Changing Directories Using the cd Command Files you need to access are often stored in subdirectories below your home directory. Because of the way in which a UNIX file system is set up, it is often desirable to change directory locations. Use the cd command to change to a new current directory. This command, like all UNIX commands, accepts both absolute and relative pathnames. Command Format cd [directory_name] Moving Around the Directory Hierarchy You can use cd with: • An absolute pathname $ cd /home • A relative pathname $ cd user2/dir1 Use the cd command without a directory name to return to your home directory. $ cd Displaying the Current Directory Using the pwd Command Remembering which directory you are currently working in is often difficult. Use the pwd (print working directory) command to display the absolute pathname of your current directory. Command Format pwd Determining Your Current Directory $ pwd /home/user2 $ $ cd /home $ pwd /home $ $ cd $ cd practice $ pwd /home/user2/practice $  Directories always contain a link to their parent directory ( ) and a link to themselves (.). Pathname abbreviations are a form of shorthand when moving between or referring to directories. Table 5-1 Pathname Abbreviations Symbol Meaning . Current (working) directory Parent directory; the directory directly above the current directory ~ User’s home directory (Korn and C shells) ~- The full path to the previous working directory (Korn shell only) ~ logname The home directory of the user specified by logname  The symbol ~+ refers to the current working diretory ($PWD ). It is used mostly when writing scripts. Use pathname abbreviations with the cd command to move around the file structure. For example: $ pwd /home/user2/dir1 $ cd $ pwd /home/user2 $ cd / $ pwd / $ cd ~/practice $ pwd /home/user2/practice $ cd ~- $ pwd / $ cd ~guest $ pwd /home/guest Using the ls Command To determine the contents of a directory, use the ls command. This command will display a listing of all files and directories within specified directories. If no pathname is given as an argument, ls will display the contents of the current directory. Command Format ls [-option(s)] [pathname[s]] Listing the Contents of a Directory $ cd $ pwd /home/user2 $ $ ls dante dir1 dir3 file1 file3 practice dante_1 dir2 dir4 file2 file4 $ $ ls dir1 coffees fruit trees $ $ ls /var/mail user1 user2 user3 user4 user5 $  Have the students try using absolute and relative pathnames. Displaying Hidden Files File names that begin with a dot (.) are called hidden files. Hidden files are frequently used to customize a user’s work environment. They are not shown by default because they are infrequently edited. Use ls -a to list all files in a directory, including hidden (.) files. Using ls to Display Hidden Files $ pwd /home/user2 $ ls -a . .kshrc dante1 dir2 dir4 file2 file4 dante dir1 dir3 file1 file3 practice $ Displaying File Types Use ls -F to display file types. The various file types are displayed using the following symbols: Table 5-2 File Type Symbols File Type Symbol Directory / Executable * Plain text file/ASCII (none) Symbolic link @ Showing File Type $ pwd /home/user2 $ $ ls -F dante dir1/ dir3/ file1 file3 practice/ dante_1 dir2/ dir4/ file2 file4 $ $ ls -F /etc cron@ asppp.cf* shadow uucp/ <output omitted> $  Links will be discussed more fully later in the course. If necessary, introduce symbolic links here by explaining that they are a way to give an alternate name to a file. Further discussion of both hard and symbolic links, along with how to create them, is contained in Module 14, ‘‘File and User Information Utilities." Displaying a Long Listing To see detailed information about the contents of a directory use the ls -l command. Getting Detailed Information Using the ls Command $ ls -l total 8 -rw-r--r-- 1 user2 staff 320Dec 7 11:43 dante -rw-r--r-- 1 user2 staff 368Dec 7 11:45 dante_1 drwxr-xr-x 5 user2 staff 512Dec 4 13:43 dir1 drwxr-xr-x 4 user2 staff 512Dec 4 13:36 dir2 drwxr-xr-x 3 user2 staff 512Dec 4 13:44 dir3 drwxr-xr-x 3 user2 staff 512Dec 4 13:44 dir4 -rw-r--r-- 1 user2 staff 0Dec 2 09:05 file1 $  total 8 indicates the number of 512-byte blocks used at this level of the directory structure. Note – Using ls -t will list files with the most recently modified at the top of the list. Listing Individual Directories Use ls -ld to display detailed information about a directory, but not its contents. This is useful when you want to see the permissions on a directory and not the information about its contents. (Permissions are discussed in detail in Module 8.) Obtaining Detailed Directory Information $ cd $ ls -l dir1 total 6 drwxr-xr-x 2 user2 other 512 Dec 9 11:10 coffees drwxr-xr-x 2 user2 other 512 Dec 9 11:10 fruit drwxr-xr-x 2 user2 other 512 Dec 9 11:10 trees $ ls -ld dir1 drwxr-xr-x 5 user2 other 512 Dec 4 13:43 dir1 $ Use ls -R to display the contents of a directory and all of its subdirectories. Displaying a Recursive Listing $ pwd /home/user2 $ ls -R dir1 coffees fruit trees dir1/coffees: beans nuts dir1/fruit: dir1/trees: Note – The R (or sometimes r) option is commonly used with many UNIX commands to indicate a recursive action. Recursive generally refers to the contents of a directory and all of its subdirectories. Metacharacters Metacharacters are keyboard characters with special meaning to the shell. They are a powerful feature of any shell. A general definition of a metacharacter is any keyboard character that is not alphanumeric; for example: ; $ % ! ~ * ? [] < > | It is very important not to use metacharacters when naming files and directories. The dot (.) and underscore ( _ ) are the only two nonalphanumeric characters that are not metacharacters. This is why you can use the dot (.) and the underscore ( _ ) in file names. A hyphen (-), even though it is a metacharacter (used to delineate options in a command line), can also be used in file names. Asterisk The asterisk (*) represents zero or more characters, except the leading dot on a hidden file. The asterisk is often referred to as a wildcard character. [...]... Contrast absolute and relative pathnames Access files and directories within the file structure using absolute and relative pathnames Identify and describe the parts of a command line Access files and directories within the file structure using pathname abbreviations List the contents of directories and their file types Identify various metacharacters Enter more than one command on a single command line Demonstrate... 18 19 20 21 22 23 24 25 26 27 28 29 30 31 /home/user2 $ File System Structure Figure 5-3 Linux File System Structure Exercise: Accessing Files and Directories Exercise objective – In this lab you will use the commands discussed in this module to change directories and list directories Tasks Use Figure 5-3 to identify the pathnames for the following objects: 1 Specify the absolute pathnames for:  user2... subdirectories in your home directory Is there a directory in the root directory (/) called kernel? _ Is there a directory in /var/spool called cron? 18 Without changing directories, type the ls command that will display all file names that end with the number 1 in your home directory 19 On one command line, change to your home directory and list the contents of the directory 20 Issue an ls command... listing of the files in this directory and see when they were last accessed The default display would be for when each file was last modified How can you get the desired display? 2 User commands generally reside in the /usr/bin directory A few days ago the system administrator showed you how to display a calendar and you remember that the command started with a c You need to use this command again to... that end with the number 1 in your home directory ls ~/*1 19 On one command line, change to your home directory and list the contents of the directory cd;ls 20 Issue an ls command that will display file and directory names of any length beginning with the letters “d” or ”f” ls [df]* 21 Issue an ls command that will display all files starting with file followed by any one character ls file? Check Your... dir2 dir4 file2 file4 fruit2 $ $ ls [af]* file1 file2 file3 file4 fruit fruit2 $ Semicolon The semicolon (;) enables you to enter multiple commands on a single command line The semicolon is also referred to as the command separator Command Format command;command Using the Semicolon $ cd;ls dante dir1 dir3 file1 file3 fruit practice dante_1 dir2 dir4 file2 file4 fruit2 $ $ date;cal;pwd Sun Jul 18 11:05:39... directory Use the ls and cd commands to complete the following steps Refer to Figure 5-2 for the first six steps as needed 12 Return to your home directory 13 Change to the dir1 directory 14 List the contents of the dir1 directory 15 Display a recursive listing of the contents of the dir2 directory 16 Use the ls command to display a detailed listing of your home directory, including hidden files 17 Use the... listing of your home directory, including hidden files ls -la ~ 17 Use the ls option that will recursively display all subdirectories in your home directory ls -R ~ Is there a directory in the root directory (/) called kernel? No; ls –F Is there a directory in /var/spool called cron? Yes; ls -F /var/spool 18 Without changing directories, type the ls command that will display any file names that end with... cd ~/dir4/flowers Use the ls and cd commands to complete the following steps Refer to Figure 5-3 for the first six steps as needed 12 Return to your home directory cd or cd ~ 13 Change to the dir1 directory cd dir1 14 List the contents of the dir1 directory ls 15 Display a recursive listing of the contents of the dir2 directory ls -R /dir2 or ls -R ~/dir2 16 Use the ls command to display a detailed listing... directory 19 On one command line, change to your home directory and list the contents of the directory 20 Issue an ls command that will display file and directory names of any length beginning with the letters “d” or ”f” 21 Issue an ls command that will display all files starting with file followed by any one character Workshop Labs Use what you have learned so far in this course to work through the following: . cal (Command only) $ cal 1987 (Command and argument) $ date (Command only) $ date -u (Command and option) $ banner "hi there" (Command and argument). Exercise: Accessing Files and Directories Exercise objective – In this lab you will use the commands discussed in this module to change directories and list directories.

Ngày đăng: 02/10/2013, 09:20

TỪ KHÓA LIÊN QUAN

w