0% found this document useful (0 votes)
11 views

Lab5 4 6

This document discusses using the ls command in UNIX/Linux to list directory contents and get file information. It covers basic ls usage as well as options like -a, -F, -l, and -t to view hidden files, file types, long listings, and sort by time modified. The document provides examples of commands and interpretations of the output.

Uploaded by

Byron Floreano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab5 4 6

This document discusses using the ls command in UNIX/Linux to list directory contents and get file information. It covers basic ls usage as well as options like -a, -F, -l, and -t to view hidden files, file types, long listings, and sort by time modified. The document provides examples of commands and interpretations of the output.

Uploaded by

Byron Floreano
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Fundamentals of UNIX

Lab 5.4.6 – Listing Directory Information


(Estimated time: 30 min.)

Objectives:
• Learn to display directory and file information
• Use the ls (list files) command with various options
• Display hidden files
• Display files and file types
• Examine and interpret the results of a long file listing
• List individual directories
• List directories recursively

Background:
In this lab, the student will use the ls command, which is used to display the contents of a directory. This
command will display a listing of all files and directories within the current directory or specified directory
or directories. If no pathname is given as an argument, ls will display the contents of the current
directory. The ls command will list any subdirectories and files that are in the current working directory if
a pathname is specified. The ls command will also default to a wide listing and display only file and
directory names. There are many options that can be used with the ls command, which makes this
command one of the more flexible and useful UNIX commands.

Command Format: ls [-option(s)] [pathname[s]]

Tools / Preparation:
a) Before starting this lab, the student should review Chapter 5, Section 4 – Listing Directory
Contents
b) The student will need the following:
1. A login user ID, for example user2, and a password assigned by their instructor.
2. A computer running the UNIX operating system with CDE.
3. Networked computers in classroom.

Notes:

1-5 Fundamentals UNIX 2.0—-Lab 5.4.6 Copyright  2002, Cisco Systems, Inc.
Use the diagram of the sample Class File system directory tree to assist with this lab.

Step 1. Log in to CDE


The student should login with the user name and password assigned by the instructor in the CDE entry
box.

Step 2. Access the Command Line


Right click on the workspace backdrop and click on Tools. Select Terminal from the menu to open a
terminal window.

Step 3. Use the Basic ls Command


The ls (list files) command, when used by itself, will display a listing of all files and directories in the
current directory. If the student has just logged in, the student’s current directory should be the home
directory.

a. Enter the command to change to the home directory. What command was used?

b. Enter the command to verify the directory the student is currently in. What command was used?

c. Enter the following command: $ ls What is displayed?

d. Can it be determined whether the items listed are directories or files?

Step 4. Use the ls Command with Arguments


Arguments for the ls command can be directory name(s) (relative or absolute) and file name(s).

a. Enter the command to display the contents of the dir2 directory using a relative pathname from
the student’s home directory. What command was used?

b. What was the response?

2-5 Fundamentals UNIX 2.0—-Lab 5.4.6 Copyright  2002, Cisco Systems, Inc.
c. Enter the command to list the files in the /etc directory, which is a standard UNIX directory under
the root, using an absolute pathname. What command was used?

d. Enter the command to list the files in the planets directory using an absolute pathname? What
command was used?

e. Enter the command to list only the dante file in the student’s home directory, to see if it exists and
not see all other files and directories. What command was seen?

Step 5. Use the ls Command to see 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 for example .profile, .dtprofile, .kshrc, .cshrs and so on. Hidden files are not
shown by default because they are infrequently edited. The current directory link (.) and parent directory
link (..) are also hidden and will not be displayed either since these links begin with a dot. Using the ls
command with the –a (all) option will list all files in a directory, including hidden (.) files. Note that the –a
option is lower case. The student should be in the home directory.
a. Enter the basic ls command without the any options. Are any hidden files seen, those files that
begin with a dot?

b. Enter the command that will allow all files in the student’s home directory to be seen?

c. How many hidden files are there?

d. Enter the following to create a new empty file called .hiddenfile using the touch command (be
sure to make the first character a dot). $ touch .hiddenfile

e. Enter the ls –a command again. Is .hiddenfile listed?

Step 6. Use the ls Command to See File Types


When using the ls command by itself, a user can obtain a listing of directory contents but cannot tell
which are files and which are directories. By using the ls command with the -F (File type) a user can
display a listing with a symbol to tell what the type of the file is. The symbol, if visible, is found at the end
of the file or directory name. Note that the –F option is an upper case F. There are four UNIX file types:
Directory, Executable, ASCII text file, and Symbolic link.

• Directory: A forward slash (/) after the name indicates this is a directory or subdirectory. A
directory is considered a type of file with UNIX.

• ASCII Text File: If there is no symbol after the name this indicates a plain ASCII text file with no
formatting characters in it. ASCII is the American Standard Code for Information Interchange. An
ASCII text file is similar to a DOS text file.

• Executable: An asterisk (*) after the name indicates that this is a command, an application, or a
script file, which can be run or executed.

• Symbolic Link: An at sign (@) after the name indicates a symbolic link which is a way of giving a
file an alternate name. Symbolic links are covered later in this course.

a. From the student’s home directory, enter the basic ls command without any options. Could a
user tell whether they are looking at files or directories if it were not for the fact that most of the
directories have “dir” in their name?

3-5 Fundamentals UNIX 2.0—-Lab 5.4.6 Copyright  2002, Cisco Systems, Inc.
b. Enter the command that will allow the student to see the file names in the home directory and
their type. List the names of the directories:

c. What types of files are present?

d. The /usr/bin directory contains many UNIX executable commands, some of which the student has
already used. Enter the command to see the files and types in the /usr/bin directory. What
command was used?

e. What types of files are present?

f. The /etc directory contains many different type of UNIX system files. Enter the command to see
the files and types in the /etc directory. What command was used?

g. Which different kinds of file types are seen?

Step 7. Use the ls Command to Displaying a Long Listing


The previous versions of the ls command displayed the names of directories and files in a wide format
(across the screen). The ls command can be used with the –l (long) option to see more detailed
information on each file or directory. The ls –l option will also distinguish between files and directories.
Note that the –l option is a lower case letter L.

Shown below is an example of a long listing for a file (dante) and a directory (dir1). The listing is
interpreted as follows:
• The first position of the display indicates whether this is a file or a directory.
• The lower case letter d indicates a directory.
• The dash (-) indicates a file.
• The group of characters shown as r, w, x and dashes are the permissions for the file or directory.
• The numbers shown as 1 and 5 are the number of links.
• User2 is the file owner.
• Staff is the file or directory access group.
• The 320 and 512 indicate the file size.
• The date and times shown indicate the date and time the file was created or modified.
• Finally the name of the file or directory is shown.
Links, owner, and group are covered later in the course.

-rw-r—-r-- 1 user2 staff 320 Dec 7 11:43 dante


drwxr-xr-x 5 user2 staff 512 Dec 4 13:43 dir1

a. From the student’s home directory, enter the basic ls command without any options. What
information was displayed on each file or directory listed?

b. Enter the command that will allow a user to see a long listing for the file names in their home
directory. What command was entered?

c. How many files are over 300 bytes in size?

d. Who is the owner of the files?

e. Using -t (time) option will list files with the most recently modified at the top of the list. To get a
detailed or long listing of files sorted by time use the ls –lt version of the command. This will
show the most recent at the top. Enter the ls –lt command. What is the most recently created
or modified file?

4-5 Fundamentals UNIX 2.0—-Lab 5.4.6 Copyright  2002, Cisco Systems, Inc.
Step 8. Use the ls Command to List Individual Directories
Use ls -ld to display detailed information about a directory, but not its contents. This is useful when a
user wants to see the properties of a directory and not the information about its contents.

a. From the student’s home directory, enter the command that will provide a long listing of just the
information for the dir2 directory.

b. From the student’s home directory, enter the command that will provide a long listing for just the
information on the fruit directory using a relative pathname.

Step 9. Use the ls Command to List Directories Recursively


Use ls -R (recursive) to display the contents of a directory and all of its subdirectories. Recursive
means to do again and again. This option is useful if a user wants to see all directories, subdirectories
and their contents for a particular part of the directory tree. If this is done at a high level in the directory
structure the output can be substantial. Notice that the –R option is an upper case R.

a. From the student’s home directory, enter the command that will provide a recursive listing for the
dir2 directory.

b. What was the result of the command?

Step 10. Close the Terminal Window and Logout


Double click on the dash button in the upper left corner of the screen, then click the EXIT icon on the front
panel.

5-5 Fundamentals UNIX 2.0—-Lab 5.4.6 Copyright  2002, Cisco Systems, Inc.

You might also like