Linux Secrets: How to Customize the ls Output

Use ls Options Change the Information that it Displays

Use ls from the Linux Command Line - Mark Alexander Bain
Use ls from the Linux Command Line - Mark Alexander Bain
The Linux command ls is used to list directory contents, but what it displays can be customized just by using a couple of options.

One of the most underrated Linux commands is ls; it is, of course, used for listing the contents of a directory. However ls has a number of options and each of these options can change the output that ls produces - making it a very versatile tool.

The Standard ls Usage

The most usual usage of ls is just to list the files in a directory:

$ ls
process_article writing_status writing_status~

This output can be changed from horizontal to vertical by using the -1 (that's the number one, not lowercase L) option:

$ ls -1
process_article
writing_status
writing_status~

Using ls to Show Hidden Files

Any file with the prefix (a full-stop or period) is normally hidden from the display; the -a option is used to reveal the hidden files:

$ ls -1a
.netrc
process_article
writing_status
writing_status~

The Long Listing Format of ls

More than just a simple list of files can be displayed; the long listing format shows:

  • the file permissions
  • number of hard links to the file
  • file owner
  • file group
  • size
  • date and time of file creation
  • file name

The long listing format is initiated by using the -l (that's lowercase L, not the number one) option:

$ ls -l
-rwxrw-rw- 1 bainm bainm 312 2008-10-10 21:20 process_article
-rwxrwxrwx 1 bainm users 4690 2008-07-14 14:58 writing_status
-rwxrwxrwx 1 bainm users 4318 2008-07-13 23:01 writing_status~

And the long listing format can be used with the -a option to show the details of all of the hidden files as well:

$ ls -la
total 32
drwxr-xr-x 2 bainm users 4096 2008-12-04 13:53 .
drwxr-xr-x 33 bainm bainm 4096 2008-11-30 14:57 ..
-rw------- 1 bainm bainm 248 2008-12-04 13:53 .netrc
-rwxrw-rw- 1 bainm bainm 312 2008-10-10 21:20 process_article
-rwxrwxrwx 1 bainm users 4690 2008-07-14 14:58 writing_status
-rwxrwxrwx 1 bainm users 4318 2008-07-13 23:01 writing_status~

Another useful form of the long format is the file classification (-F) - this appends a symbol onto the file name according to the file:

  • /: directory
  • *: executable
  • @: symbolic link
  • =: socket
  • %: whiteout
  • |: FIFO

For example:

$ ls -laF
total 32
drwxr-xr-x 2 bainm users 4096 2008-12-04 13:53 ./
drwxr-xr-x 33 bainm bainm 4096 2008-11-30 14:57 ../
-rw------- 1 bainm bainm 248 2008-12-04 13:53 .netrc
-rwxrw-rw- 1 bainm bainm 312 2008-10-10 21:20 process_article*
-rwxrwxrwx 1 bainm users 4690 2008-07-14 14:58 writing_status*
-rwxrwxrwx 1 bainm users 4318 2008-07-13 23:01 writing_status~*

Learning more about ls

All of the other options can be read about by using the Linux man command:

$ man ls

Summary

At it's simplest level ls is very useful for listing the contents of a directory, but has a number of options, for example:

  • -1: display vertically rather than horizontally
  • -a: show hidden files
  • -l: long listing format
  • -F: show file classification

The options can be used on their own or in conjunction completely change the way that the ls output looks.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement