Thursday, 17 January 2013

Linux Commands For File and Directory Listing



The most basic feature of the shell is the ability to see what files are available on the system. The
list command (ls) is the tool that helps do that. Here we briefly describe ls command, and all
of the options available to format the information it can provide.

Basic listing

The ls command at its most basic form displays the files and directories located in your current
directory:
$ ls
           4rich Desktop Download Music Pictures store store.zip test
           backup Documents Drivers myprog Public store.sql Templates Videos
$
Notice that the ls command produces the listing in alphabetical order (in columns rather than
rows). If you’re using a terminal emulator that supports color, the ls command may also show
different types of entries in different colors. The LS COLORS environment variable controls this
feature. Different Linux distributions set this environment variable depending on the capabilities
of the terminal emulator.
If you don’t have a color terminal emulator, you can use the -F parameter with the ls command
to easily distinguish files from directories. Using the -F parameter produces the following output:
$ ls -F
        4rich/  Documents/  Music/  Public/    store.zip Videos/
        backup.zip Download/  myprog* store/    Templates/
        Desktop/    Drivers/    Pictures/ store.sql test
$
The -F parameter now flags the directories with a forward slash, to help identify them in the
listing. Similarly, it flags executable files  with an asterisk, to help you find the files that can be run on the system easier.
The basic ls command can be somewhat misleading. It shows the files and directories contained
in the current directory, but not necessarily all of them. Linux often uses hidden files to store
configuration information. In Linux, hidden files are files with filenames that start with a period.
These files don’t appear in the default ls listing (thus they are called hidden).
To display hidden files along with normal files and directories, use the -a parameter.
 In a home directory for a user who has logged in to the system
from a graphical desktop, you’ll see lots of hidden configuration files. This particular example
is from a user logged in to a GNOME desktop session. Also notice that there are three files that
begin with .bash. These files are hidden files that are used by the bash shell environment.
The -R parameter is another command ls parameter to use. It shows files that are contained
within directories in the current directory. If you have lots of directories, this can be quite a long
listing.

Modifying the information presented

As you can see in the basic listings, the ls command doesn’t produce a whole lot of information
about each file. For listing additional information, another popular parameter is -l. The
-l parameter produces a long listing format, providing more information about each file in the
directory:
$ ls -l
total 2064
drwxrwxr-x  2      rich rich        4096      2007-08-24   22:04          4rich
-rw-r--r--    1      rich rich      766205    2007-08-24   15:34          backup.zip
drwxr-xr-x   3        rich rich         4096     2007-08-31   22:24        Desktop
drwxr-xr-x   2        rich rich         4096     2001-11-01   04:06        Documents
drwxr-xr-x    2       rich rich         4096     2001-11-01   04:06        Download
drwxrwxr-x   2       rich rich         4096     2007-07-26   18:25        Drivers
drwxr-xr-x    2        rich rich         4096    2001-11-01    04:06       Music
-rwxr--r--     1        rich rich        30         2007-08-23     21:42      myprog
drwxr-xr-x    2        rich rich        4096     2001-11-01     04:06      Pictures
drwxr-xr-x    2        rich rich        4096     2001-11-01    04:06       Public
drwxrwxr-x   5       rich rich         4096    2007-08-24     22:04       store
-rw-rw-r--    1       rich rich       98772     2007-08-24    15:30       store.sql
-rw-r--r--     1       rich rich      107507     2007-08-13   15:45       store.zip
drwxr-xr-x    2       rich rich        4096       2001-11-01   04:06      Templates
drwxr-xr-x    2       rich rich        4096       2001-11-01    04:06     Videos
[rich@testbox ~]$

The long listing format lists each file and directory contained in the directory on a single line.
Besides the filename, it shows additional useful information. The first line in the output shows
the total number of blocks contained within the directory. Following that, each line contains the
following information about each file (or directory):
■ The file type (such as directory (d), file (-), character device (c), or block device (b)
■ The permissions for the file
■ The number of hard links to the file
■ The username of the owner of the file
■ The group name of the group the file belongs to
■ The size of the file in bytes
■ The time the file was modified last
■ The file or directory name
The -l parameter is a powerful tool to have. Armed with this information you can see just about
any information you need to for any file or directory on the system.

The complete parameter list

There are lots of parameters for the ls command that can come in handy as you do file management.
If you use the man command for ls, you’ll see several pages of available parameters for you
to use to modify the output of the ls command.
The ls command uses two types of command line parameters:
■ Single-letter parameters
■ Full-word (long) parameters
The single-letter parameters are always preceded by a single dash. Full-word parameters are more
descriptive and are preceded by a double dash. Many parameters have both a single-letter and
full-word version, while some have only one type.
You can use more than one parameter at a time if you want to. The double dash parameters must
be listed separately, but the single dash parameters can be combined together into a string behind
the dash. A common combination to use is the -a parameter to list all files, the -i parameter to
list the inode for each file, the -l parameter to produce a long listing, and the -s parameter to
list the block size of the files. The inode of a file or directory is a unique identification number
the kernel assigns to each object in the filesystem. Combining all of these parameters creates the
easy-to-remember -sail parameter:
$ ls -sail
total 2360
301860           8   drwx------ 36      rich rich     4096    2007-09-03          15:12 .
65473             8   drwxr-xr-x    6       root root    4096   2007-07-29           14:20 ..
360621           8   drwxrwxr-x   2       rich rich     4096    2007-08-24           22:04     4rich
301862           8   -rw-r--r--      1       rich rich      124    2007-02-12           10:18 .bashrc
361443           8     drwxrwxr-x  4       rich rich    4096     2007-07-26          20:31 .ccache
301879           8      drwxr-xr-x  3        rich rich   4096     2007-07-26          18:25 .config
301871           8  drwxr-xr-x       3        rich rich    4096      2007-08-31         22:24 Desktop
301870           8  -rw-------        1       rich rich                   26 2001-11-01    04:06 .dmrc
301872            8 drwxr-xr-x      2      rich rich     4096   2001-11-01            04:06  Download
360207        8 d   rwxrwxr-x      2      rich rich     4096   2007-07-26            18:25 Drivers
301882        8     drwx------      5      rich rich      4096   2007-09-02            23:40 .gconf
301883        8 drwx------         2       rich rich      4096    2007-09-02           23:43 .gconfd
360338        8 drwx------         3       rich rich      4096    2007-08-06           23:06 .gftp

SingleLetter             Full Word                              Description
-a                             all                                         Don’t ignore entries starting with a period.
-A                          --almost-all                             Don’t list the . and .. files.
                               --author                                 Print the author of each file.
-b                           --escape                                Print octal values for nonprintable characters.
                              --block-size=size                    Calculate the block sizes using size-byte blocks.
-B                          --ignore-backups                    Don’t list entries with the tilde (∼) symbol (used to
                                                                            denote backup copies).
-c                                                                        Sort by time of last modification.
-C                                                                       List entries by columns.
                               --color=when                       When to use colors (always, never, or auto).
-d                           --directory                            List directory entries instead of contents, and don’t
                                                                           dereference symbolic links.
-F                             --classify                           Append file-type indicator to entries.
                              --file-type                            Only append file-type indicators to some filetypes (not
                                                                         executable files).
                              --format=word                    Format output as either across, commas, horizontal, long,
                                                                         single-column, verbose, or vertical.
-g                                                                      List full file information except for the file’s owner.
                             --group-directoriesfirst         List all directories before files.
-G                         --no-group                          In long listing don’t display group names.
-h                          --human-readable               Print sizes using K for kilobytes, M for megabytes, and G
                                                                        for gigabytes.
                           --si                                       Same as -h, but use powers of 1000 instead of 1024.
-i                             --inode                            Display the index number (inode) of each file.
-l                                                                    Display the long listing format.
-L                      --dereference                         Show information for the original file for a linked file.
-n                        --numeric-uid-gid                Show numeric userid and groupid instead of names.

-o                                                                  In long listing don’t display owner names.
-r                      --reverse                               Reverse the sorting order when displaying files and
                                                                      directories.
-R                     --recursive                            List subdirectory contents recursively.
-s                      --size                                    Print the block size of each file.
-S                     --sort=size                            Sort the output by file size.
-t                      --sort=time                           Sort the output by file modification time.
-u                                                                 Display file last access time instead of last modification time.
-U                      --sort=none                          Don’t sort the output listing.
-v                    --sort=version                         Sort the output by file version.
-x                                                                 List entries by line instead of columns.
-X                   --sort=extension                   Sort the output by file extension.

Besides the normal -l parameter output information, you’ll see two additional numbers added
to each line. The first number in the listing is the file or directory inode number. The second
number is the block size of the file. The third entry is a diagram of the type of file, along with the
file’s permissions.
Following that, the next number is the number of hard links to the file  the owner of the file, the group the file belongs to, the size of the file (in bytes), a timestamp showing the last modification time by default, and finally, the actual filename.

Filtering listing output

As you’ve seen in the examples, by default the ls command lists all of the files in a directory.
Sometimes this can be overkill, especially when you’re just looking for information on a single file.
Fortunately, the ls command also provide a way for us to define a filter on the command line. It
uses the filter to determine which files or directories it should display in the output.
The filter works as a simple text-matching string. Include the filter after any command line parameters
you want to use:
$ ls -l myprog
-rwxr--r-- 1 rich rich 30 2007-08-23 21:42 myprog
$

When you specify the name of specific file as the filter, the ls command only shows the information
for that one file. Sometimes you might not know the exact name of the file you’re looking for.
The ls command also recognizes standard wildcard characters and uses them to match patterns
within the filter:
■ A question mark to represent one character
■ An asterisk to represent zero or more characters
The question mark can be used to replace exactly one character anywhere in the filter string. For
example:
$ ls -l mypro?
-rw-rw-r-- 1 rich rich 0 2007-09-03 16:38 myprob
-rwxr--r-- 1 rich rich 30 2007-08-23 21:42 myprog
$
The filter mypro? matched two files in the directory. Similarly, the asterisk can be used to match
zero or more characters:
$ ls -l myprob*
-rw-rw-r-- 1 rich rich 0 2007-09-03 16:38 myprob
-rw-rw-r-- 1 rich rich 0 2007-09-03 16:40 myproblem
$
The asterisk matches zero characters in the myprob file, but it matches three characters in the
myproblem file.
This is a powerful feature to use when searching for files when you’re not quite sure of the filenames.







No comments:

Post a Comment