11

I have a problem with listing of some directory with ls -l:

$ ls -l ./directory
-????????? ? ? ? ?            ? file001.txt
-????????? ? ? ? ?            ? file002.txt

and just ls works good:

$ ls ./directory
file001.txt file002.txt

What is wrong?

1 Answer 1

16

Check the permissions of ./directory : if you have read permissions but not execute permissions on this directory, then you have enough rights to read the list of files in that directory, but you can't actually use these files or get information about them.

example session:

$ cd /tmp/
$ mkdir /tmp/test
$ touch /tmp/test/file
$ ls -la test/
total 44
drwxr-xr-x  2 myself myself  4096 janv.  5 11:01 .
drwxrwxrwt 42 root   root   54242 janv.  5 11:01 ..
-rw-r--r--  1 myself myself     0 janv.  5 11:01 file
$ chmod a-x /tmp/test # remove execute permission for all
$ ls -la test/
total 0
d????????? ? ? ? ?            ? .
d????????? ? ? ? ?            ? ..
-????????? ? ? ? ?            ? file
$ ls -ld test/
drw-r--r-- 2 myself myself 4096 Jan  5 11:01 test/
$ cat test/file 
cat: test/file: Permission denied
$ chmod a+x /tmp/test # readd execute permission for all
$ ls -la test/
total 44
drwxr-xr-x  2 myself myself  4096 janv.  5 11:01 .
drwxrwxrwt 42 root   root   54242 janv.  5 11:01 ..
-rw-r--r--  1 myself myself     0 janv.  5 11:01 file
$ ls -ld test/
drwxr-xr-x 2 myself myself 4096 Jan  5 11:01 test/
$ cat test/file
$

Some ls versions show error messages when they can't display information about files.

2
  • But how ls can know, is the file inside test directory or not (check d char for '.' and '..')?
    – osgx
    Jan 5, 2013 at 10:18
  • 1
    @osgx: This is part of the file list, with the file name and the inode number. man readdir for more low-level detail. Note that this behaviour is not specified by POSIX.
    – BatchyX
    Jan 5, 2013 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .