Home > Articles

  • Print
  • + Share This

Listing Directories

Listing the contents of directories, like navigating through your system, is a basic skill you should quickly master. The following sections describe several directory listing programs that are usually included with most Linux distributions.

Listing Directories and Files with the ls Command

Use the ls (list directory) command to list the contents of one or several directories. This command has more than 40 different command-line options that can be combined to format listings. Wildcards can be used to specify certain files or directories. To list the contents of the /usr/local/ directory, type the following:

$ ls /usr/local/ 
bin      etc      info     lib      qt       src 
doc      games    lesstif  man      sbin 

By default, the ls command lists the contents of directories in columns, sorted vertically. You can use different command-line options and wildcards to view directory contents in different formats. For example, the -F (classify) option identifies directories and executable files by appending a forward slash (/) and asterisk (*) to file or directory names:

$ ls  -F /usr/local/lib/* 
/usr/local/lib/cddb: 
eb104910 
/usr/local/lib/saytime: 
saytime.sh*  sounds/ 

Table 3.1 lists some common command-line options that you can use to view directory contents in different formats.

Table 3.1  Common ls Command-Line Options

Flag

Description

-d

List directory names, not directory contents

-l

Long format listing (includes file size, dates, permissions, and so on)

-m

List filenames separated by commas

-x

Sort filenames in columns horizontally

-a

List all files (including those beginning with .)

-A

List all files, but not . and ..

-C

Sort files in columns vertically

-F

Identify directories, links, and executables

-R

List directory contents recursively

-S

Sort files by size

--color

Use color to identify files


Changing file and directory listing colors

You can use ls --color to easily distinguish file types in a directory listing by color. For instance, by default all the subdirectories will be blue, executables will be green, text files will be white. If you plan to make an alias for this, it's preferable to use ls --color=auto which will only colorize output sent to your screen (and not confuse programs you are piping output to). To learn how to customize which colors are used for certain files, see the info or man page on dircolors.

The ls command is documented in its manual page. You should also check out the manual pages for related directory listing commands, such as dir, vdir, lsattr, tree, and dircolors.

Echoing Directory Contents with the echo Command

The echo command can also be used to list the contents of directories. This command, built in to each shell, is also a program found under the /bin directory. In order to list directories and files, specify a wildcard on the command line like so:

$ echo /* 
/bin /boot /dev /etc /home /lib /lost+found 
/mnt /opt /proc /root /sbin /tmp /usr /var 

Here's another example:

$ echo /mnt/z* 
/mnt/zip /mnt/zipln 

The echo command prints all matching filenames in alphabetical order, but does not format the listing in columns. Use the ls command for formatted listings.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.