ls command options | Linux

Shell Scripting

ls is probably the most used command and for good reason. With it, we can see directory contents and determine a variety of important file and directory attributes. As we have seen, we can simply enter ls to see a list of files and subdirectories contained in the current working directory:

[me@linuxbox ~]$ ls

Desktop Documents Music Pictures Public Templates Videos

Besides the current working directory, we can specify the directory to list, like so:

 

me@linuxbox ~]$ ls /usr

bin games kerberos libexec sbin src etc include lib local share tmp

 

or even specify multiple directories. In this example we will list both the user’s home directory (symbolized by the ~ character) and the /usr directory:

 

[me@linuxbox ~]$ ls ~ /usr

/home/me:

Desktop Documents Music Pictures Public Templates Videos

/usr:

bin games kerberos libexec sbin src etc include lib local share tmp

We can also change the format of the output to reveal more detail:

 

[me@linuxbox ~]$ ls -l

total 56

drwxrwxr-x 2 me me 4096 2012-10-26 17:20 Desktop

drwxrwxr-x 2 me me 4096 2012-10-26 17:20 Documents

drwxrwxr-x 2 me me 4096 2012-10-26 17:20 Music

drwxrwxr-x 2 me me 4096 2012-10-26 17:20 Pictures

drwxrwxr-x 2 me me 4096 2012-10-26 17:20 Public

drwxrwxr-x 2 me me 4096 2012-10-26 17:20 Templates

drwxrwxr-x 2 me me 4096 2012-10-26 17:20 Videos

 

By adding -l to the command, we changed the output to the long format.

 

Options and Arguments

 

This brings us to a very important point about how most commands work. Commands are often followed by one or more options that modify their behavior and, further, by one or more arguments, the items upon which the command acts. So most commands look something like this:

 

command -options arguments

 

Most commands use options consisting of a single character preceded by a dash, such as -l. But many commands, including those from the GNU Project, also support long options, consisting of a word preceded by two dashes.

 

Also, many commands allow multiple short options to be strung together. In this example, the ls command is given two options, the l option to produce long format output, and the t option to sort the result by the file’s modification time:

 

[me@linuxbox ~]$ ls -lt

 

We’ll add the long option –reverse to reverse the order of the sort:

 

[me@linuxbox ~]$ ls -lt –reverse

The ls command has a large number of possible options and most commonly used commands are listed below:

Option Long Option Description
-a –all List all files, even those with names that begin with a period, which are normally not listed (i.e., hidden).
-d –directory Ordinarily, if a directory is specified, ls will list the contents of the directory, not the directory itself. Use this option in conjunction with the -l option to see details about the directory rather than its contents.
-F –classify This option will append an indicator character to the end of each listed name (for example, a forward slash if the name is a directory).
-h –human-readable In long format listings, display file sizes in human-readable format rather than in bytes.
-l Display results in long format.
-r –reverse Display the results in reverse order. Normally,
ls Displays its results in ascending alphabetical order.
-S
-t Sort by modification time.

 

A Longer Look at Long Format

As we saw before, the -l option causes ls to display its results in long format.

This format contains a great deal of useful information. Here is the Examples

directory from an Ubuntu system:

-rw-r–r– 1 root root 3576296 2012-04-03 11:05 Experience ubuntu.ogg

-rw-r–r– 1 root root 1186219 2012-04-03 11:05 kubuntu-leaflet.png

-rw-r–r– 1 root root 47584 2012-04-03 11:05 logo-Edubuntu.png

-rw-r–r– 1 root root 44355 2012-04-03 11:05 logo-Kubuntu.png

-rw-r–r– 1 root root 34391 2012-04-03 11:05 logo-Ubuntu.png

-rw-r–r– 1 root root 32059 2012-04-03 11:05 oo-cd-cover.odf

-rw-r–r– 1 root root 159744 2012-04-03 11:05 oo-derivatives.doc

-rw-r–r– 1 root root 27837 2012-04-03 11:05 oo-maxwell.odt

-rw-r–r– 1 root root 98816 2012-04-03 11:05 oo-cd-cover.odf

ls Long Listing Fields Field Meaning

 

  • -rw-r—r– Access rights to the file. The first character indicates the type of file. Among the different types, a leading dash means a regular file, while a d indicates a directory. The next three characters are the access rights for the file’s owner, the next three are for members of the file’s group, and the final three are for everyone else.
  • root The user name of the file’s owner.
  • root The name of the group that owns the file.
  • 32059 Size of the file in bytes.
  • 2012-04-03 11:05 Date and time of the file’s last modification.
  • oo-cd-cover.odf Name of the file.


In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.