Information Technology College of HoChiMinh city
Faculty of Information Technology
Course: Fundamentals of Linux OS
Unit 2
Accessing
Files and Directories
Lecturer: Võ Tấn Dũng
/>
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 meta-characters
• Enter more than one command on a single command line
• Demonstrate the use of wildcard characters
VÕ TẤN DŨNG
Pathnames
• A pathname uniquely identifies a particular file or directory
by specifying its location.
VÕ TẤN DŨNG
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
Example:
/home/user2
/home/user2/dir1
/home/user2/dir1/coffees
VÕ TẤN DŨNG
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.
VÕ TẤN DŨNG
Relative Pathname (cont.)
• A relative Pathname starts from a direct child directory of the
current directory to the destination.
• Relative pathnames are useful because they are shorter than
absolute pathnames.
Example:
The current directory
/home
Relative pathname to user2
user2
Relative pathname to dir1
user2/dir1
Relative pathname to coffees
user2/dir1/coffees
VÕ TẤN DŨNG
Changing Directories
using the cd command
• Files you need to access are often stored in a given
subdirectory, it is often desirable to change directory locations.
• Use the cd command to change to a new current directory. The
cd command accepts both absolute and relative pathnames.
Command Format
cd [directory_name]
Example:
With an absolute pathname
$ cd /home
With a relative pathname
$ cd user2/dir1
Use the cd command without a directory name
to return to your home directory.
$ cd
VÕ TẤN DŨNG
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.
Example:
$ pwd
/home/user2
$ cd /home
$ pwd
/home
$ cd
$ cd practice
$ pwd
/home/user2/practice
VÕ TẤN DŨNG
Pathname Abbreviations
Example:
$ pwd
/home/user2/dir1
$ cd ..
$ pwd
/home/user2
$ cd ../..
$ pwd
/
$ cd ~/practice
$ pwd
/home/user2/practice
$ cd ~$ pwd
/
$ cd ~guest
$ pwd
/home/guest
VÕ TẤN DŨNG
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]]
Example:
$ 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
VÕ TẤN DŨNG
Displaying Hidden Files
• File names that begin with a dot (.) are called hidden files.
They are not shown by default because they are infrequently
edited.
• Use ls -a to list all files in a directory, including hidden (.) files.
Example:
$ pwd
/home/user2
$ ls -a
. .kshrc dante1 dir2 dir4 file2 file4
.. dante dir1 dir3 file1 file3 practice
$
VÕ TẤN DŨNG
Displaying File Types
• Use ls -F to display file types. The various file types are
displayed using the following symbols:
Showing File Type:
$ ls -F /etc
cron@ asppp.cf* shadow uucp/
<output omitted>
$
VÕ TẤN DŨNG
Displaying a Long Listing
• To see detailed information about the contents of a
directory use the ls -l command.
VÕ TẤN DŨNG
Displaying a Long Listing (cont.)
VÕ TẤN DŨNG
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.
VÕ TẤN DŨNG
Displaying a Recursive Listing
• Use ls -R to display the contents of a directory and all of its
subdirectories.
VÕ TẤN DŨNG
Meta-characters
• Metacharacters are keyboard characters with special
meaning to the shell. A general definition of a meta
character is any keyboard character that is not
alphanumeric.
• for example:
• It is very important not to use meta characters when naming
files and directories. The dot (.) and underscore ( _ ) are the
only two non-alphanumeric 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.
VÕ TẤN DŨNG
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.
Example:
$ ls
dante dir1 dir3 file1 file3 fruit practice
dante_1 dir2 dir4 file2 file4 fruit2
$ ls d*
dante dante_1
dir1:
coffees fruit trees
dir2:
beans notes recipes
dir3:
planets
dir4:
flowers
VÕ TẤN DŨNG
Question Mark
• The question mark (?) matches any single character, except
for the leading dot on a hidden file.
Example: $ ls
dante dir1 dir3 file1 file3 fruit practice
dante_1 dir2 dir4 file2 file4 fruit2
$ ls dir?
dir1:
coffees fruit trees
dir2:
beans notes recipes
dir3:
planets
dir4:
flowers
VÕ TẤN DŨNG
Question Mark (cont.)
• The following example illustrates the message you will receive
if no file name matches the wildcard character:
Example:
$ ls z?
z?: No such file or directory
$
VÕ TẤN DŨNG
Square Brackets
• Use square brackets ([ ]) to match a set or range of characters
for a single character position. The characters inside the
brackets do not generally need to be in any order; for example,
[abc] is the same as [cab]. However, if you are looking for a
range of characters, they must be in proper order (for example,
[a–z] or [3–9]). If you want to search for all alphabetic
characters, whether lowercase or uppercase, use [A–z] for the
pattern to match. You can use alphabetic or numeric
characters for the search pattern.
Example:
$ ls [b-f]*
dante dir1 dir3 file1 file3 fruit practice
dante_1 dir2 dir4 file2 file4 fruit2
$
$ ls [af]*
file1 file2 file3 file4 fruit fruit2
$
VÕ TẤN DŨNG
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
Example:
$ 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 MDT 1999
July 1999
S M Tu W Th F S
123
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
/home/user2
VÕ TẤN DŨNG
END OF UNIT 2
• remember to do your homework
(see />
VÕ TẤN DŨNG