Color ls output and permission denied

In my post from earlier this week on removing the color output from the ls command I mentioned that in some circumstances the color-coded output can cause permissions errors. Cloned Milkmen asked that I explain why this would be.

I was hoping someone would ask that. In Linux and UNIX if the execute flag is not set on a directory, as in the example below, the ls --color=tty command will return Permission denied.

$ ls -ld ls_test/
dr-------- 4 jonemmons pg222211 4096 Nov 12 20:07 ls_test/
$ ls ----color=tty ls_test/
ls: ls_test/test1: Permission denied
ls: ls_test/test2: Permission denied
ls: ls_test/test3: Permission denied
dir1 dir2
$ ls ls_test/
dir1 dir2 test1 test2 test3

The execute flag is funny on directories. The main effect is that it will inhibit a user’s ability to cd into a directory, but it will also keep you from being able to get information out of the file’s inode (the inode contains information about when a file was created, modified, accessed etc.) Since ls requires information from the inode to properly color-code files in a directory it will fail if the execute bit is not set. An ls -l will fail for the same reason.

So thanks to Cloned Milkmen for asking the question. If you’re looking for some eclectic and distracting reading, check out his blog The Synthetic Librarian.

unix, linux, directory, permission, security, sysadmin, system administration

3 thoughts on “Color ls output and permission denied”

  1. I don’t think the follwoing you wrote works:
    ls –color=tty ls_test/
    I believe the correct construct is:
    ls -–color=tty ls_test/
    Furthermore, if you wanted to make an alias I’d suggest the following:
    ls=’-hF-–color=tty’

  2. Erich,

    Thanks for pointing that out! I’m not sure why (and don’t have the time to figure it out right now) but WordPress interpreted my double-dash as a single-dash. The temporary fix was to add two more dashes and it displays sort-of right.

    I would consider the -F option redundant to the color output, but actually prefer it if you’re looking for an indication of the file types.

    Thanks for the comment.

  3. Sorry to comment on such an old article, but I was looking for information on color in ls and stumbled across it.

    Regardless, this is patently untrue. How can you compare using ls with and without the -d (print directory entries) switch?

    In brief:

    [root@bacon test]# ls -la
    total 24
    drw——- 2 notmike notmike 4096 2009-08-14 08:49 .
    drwx–x–x 66 notmike notmike 4096 2009-08-14 08:47 ..
    -rw-r–r– 1 root root 0 2009-08-14 08:49 temp1
    -rw-r–r– 1 root root 0 2009-08-14 08:49 temp2

    [notmike@bacon ~]$ ls –color=none test
    temp1 temp2
    [notmike@bacon ~]$ ls -l –color=none test
    ls: cannot access test/temp2: Permission denied
    ls: cannot access test/temp1: Permission denied
    total 0
    -????????? ? ? ? ? ? temp1
    -????????? ? ? ? ? ? temp2
    [notmike@bacon ~]$ ls -ld –color=none test
    drw——- 2 notmike notmike 4096 2009-08-14 08:49 test
    [notmike@bacon ~]$ ls –color=tty test
    ls: cannot access test/temp2: Permission denied
    ls: cannot access test/temp1: Permission denied
    temp1 temp2
    [notmike@bacon ~]$ ls -l –color=tty test
    ls: cannot access test/temp2: Permission denied
    ls: cannot access test/temp1: Permission denied
    total 0
    -????????? ? ? ? ? ? temp1
    -????????? ? ? ? ? ? temp2
    [notmike@bacon ~]$ ls -ld –color=tty test
    drw——- 2 notmike notmike 4096 2009-08-14 08:49 test

    Take note: Works as advertised.

Leave a Reply

Your email address will not be published. Required fields are marked *