Colors of ls

  • Use of colors for indicating different types of files and directories can be enabled for the ls command. This is useful to discern the files you want from the output quickly.

  • To enable color output use the --color option. To be more clever and show colors only when the output is not being piped to another program, use the --color=auto option.

  • By default, these colors are used: blue for directories, cyan for symbolic links, green for executables, yellow for device files, red for archived files and pink for image files.

  • You can see the color settings for the various file types and extensions by running the dircolors command or echo $LS_COLORS. Both of them should show the same output.

  • The explanation of the various foreground and background color values as set in $LS_COLORS are explained here.

  • To set your own colors or add new filetypes with colors, change the $LS_COLORS environment variable in your shell startup file.

  • Once you have colors, you may find the character indicators that ls uses by default as unnecessary. You can turn them off by using the --indicator-style=file-type option.


How to view octal permission of file

The ls command shows the access permissions of a file or directory in the symbolic representation using rwx. Sometimes you may want the view the permissions in octal representation. Sadly ls has no option to show this.

However, the stat command shows both the octal and symbolic representations by default amongst the other wealth of details it lists:

$ stat foobar.txt
  File: β€˜foobar.txt’
  Size: 1692            Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 57285082    Links: 1
Access: (0400/-r--------)  Uid: ( 1001/  joe)   Gid: ( 1001/  joe)
Access: 2016-05-11 15:16:32.884351709 +0800
Modify: 2016-05-11 13:42:04.135990274 +0800
Change: 2016-05-11 15:16:31.872327471 +0800
 Birth: -

To get just the permissions in octal representation:

$ stat --format="%a" foobar.txt

Tried with: Ubuntu 14.04

Inodes and hard links

A hard link is a reference that links a file or directory name to an inode in the filesystem. Hard links are created by default when a file or directory is created. Hard links can also be created in Linux (or any Unix) using the ln command.

The inode number of any file or directory can be viewed using ls -i:

$ mkdir foo
$ ls -id foo
1842306 foo/

Here, 1842306 is the inode number of the foo directory we created.

The number of hard links can be viewed in the second column of ls -l:

$ ls -ld foo
drwxrwxr-x 2 joe joe 4096 Mar 16 21:59 foo/

We can see that an empty directory starts off with 2 hard links. These are from foo and foo/.. respectively.

In contrast, a file starts off with a single hard link.

The number of hard links associated with the inode of a directory is usually the number of its subdirectories, plus two. This is because the .. entry inside each subdirectory points back to the inode of its parent directory.

Here is an illustration:

$ mkdir -p foo/bar
$ mkdir -p foo/buzz
$ mkdir -p foo/pizza
$ ls -ld foo/{,.,*/..} 
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/.
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/bar/..
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/buzz/..
drwxrwxr-x 5 joe joe 4096 Mar 16 22:04 foo/pizza/..

How to list files or directories only

There are many instances where I have wanted to list only the files or only the directories. The find tool seems to be the most convenient way to do this.

To list only the files in current directory:

$ find . -maxdepth 1 -type f | sort

To list only the directories in current directory:

$ find . -maxdepth 1 -type d | sort

The -maxdepth says it should only look in the directory and not recurse into subdirectories. The -type indicates that only files (or directories) should be found. The results of find are not in sorted order, so we pipe them to sort.

I recommend making an aliases out of this, so that they can be called easily like say lsf or lsd.

Tried with: FindUtils 4.4.2-7 and Ubuntu 14.04

How to list files in natural order

One of the little-known options of ls is -v. It sorts the files or directories in a natural order. That is, if the file or directory name has numbers, then those files and directories are sorted in the natural order of those numbers. Like 2 comes before 101.

$ ls -v

This is one of those ls options that you might want to add to your favorite ls alias.

Tried with: CoreUtils 8.21-1 and Ubuntu 14.04

How to change time style used by ls

The date and time displayed by ls -l can be changed to any format you want.

The default is:

$ ls -l
total 88
-rw-rw-r-- 1 joe joe     6 Jan 27 12:23 a.txt
-rw-rw-r-- 1 joe joe   522 Jan 27 12:22 haha.txt
-rw-rw-r-- 1 joe joe 80360 Jan 27 12:23 jump.pdf

To change the format of the date and time use the --time-style option. Values that can be passed to it are long-iso, full-iso and iso. You can also specify your own custom format as a string.

Here is how the different time styles look:

$ ls -l --time-style=long-iso
total 88
-rw-rw-r-- 1 joe joe     6 2015-01-27 12:23 a.txt
-rw-rw-r-- 1 joe joe   522 2015-01-27 12:22 haha.txt
-rw-rw-r-- 1 joe joe 80360 2015-01-27 12:23 jump.pdf

$ ls -l --time-style=full-iso
total 88
-rw-rw-r-- 1 joe joe     6 2015-01-27 12:23:08.309375574 +0800 a.txt
-rw-rw-r-- 1 joe joe   522 2015-01-27 12:22:59.773375414 +0800 haha.txt
-rw-rw-r-- 1 joe joe 80360 2015-01-27 12:23:32.693376032 +0800 jump.pdf

$ ls -l --time-style=iso
total 88
-rw-rw-r-- 1 joe joe     6 01-27 12:23 a.txt
-rw-rw-r-- 1 joe joe   522 01-27 12:22 haha.txt
-rw-rw-r-- 1 joe joe 80360 01-27 12:23 jump.pdf

Tried with: Ubuntu 14.04

HgList extension for Mercurial

When I am at the shell inside a Mercurial repository, I sometimes want to list only those files that are under version control and some details about them. Something like the ls command, but for Mercurial. That is exactly what the HgList extension for Mercurial sets out to do.

It can be installed from PyPI easily:

$ sudo pip install hglist

To enable it, add this line to your global .hgrc:

hglist =

It can be used at any directory inside any Mercurial repository:

$ hg list
$ hg ls
$ hg ls -l -H

Tried with: HgList 0.3, Mercurial 2.8.2 and Ubuntu 14.04

How to count files at commandline in Ubuntu

Is there a way to easily count how many files and directories match a given pattern? Thankfully, this is easy to do at the commandline with the ls and wc programs. ls can be used to list files that match the pattern you want and wc can be used to count them.

Assume we want to count how many files or directories have prefix Foo.

First, let us see what does not work:

$ ls -l Foo* | wc -l

This is ls with its -l (EL, not ONE) parameter. It lists the files that match and you pipe it to wc to count the number of lines it receives.

This almost works, except that ls -l puts an extra line at the beginning that shows the number of blocks used by that directory.

Instead, use ls -1 (that is ONE, not EL) parameter which gives exactly what you want:

$ ls -1 Foo* | wc -l

Tried with: Ubuntu 12.04.1 LTS