0

I have removed "x" execute permission and write permission for others on a directory /u/permtst/test/bits/. This directory details are user: permtst and group: permtst.

permtst@localhost(nyc):~/test/bits> ls -la
total 8
drwxrw-r-- 2 permtst permtst 4096 Aug  3 12:51 .
drwxrwsr-x 8 permtst permtst 4096 Aug  3 11:52 ..
-rw-rw-r-- 1 permtst permtst    0 Aug  3 11:56 test_file
-rw-rw-r-- 1 permtst permtst    0 Aug  3 11:56 test_file2

User motamari is not part of the permtest group.

Since others have read permissions, ls on the directory should give the list of files in the directory. But if you see below it is printing no permission to access and it is also printing the list of the files as expected.

My question is why did it print the error ls: cannot access '/u/permtst/test/bits/test_file': Permission denied

motamari@localhost:~% ls /u/permtst/test/bits/
ls: cannot access '/u/permtst/test/bits/test_file': Permission denied
ls: cannot access '/u/permtst/test/bits/test_file2': Permission denied
test_file  test_file2
5
  • 8
    You might have an alias ls='ls -F' or ls='ls --color where ls tries to find out of the type of the files, and can't as you don't have search access to the directory. What's the output of type ls? Aug 3, 2023 at 17:36
  • 2
    For directories, r means you (or ls) can read the directory, while x means you can search. Try /bin/ls -l /u/permtst/test/bits/test_file /u/permtst/test/bits/test_file2
    – waltinator
    Aug 4, 2023 at 0:38
  • 1
    To add to Stéphane's comment, you can avoid your alias, if it is set, with command ls. The command built-in will cause your shell (in most cases; I haven't tested all shells) to simply run its arguments as commands, avoiding aliases.
    – ghoti
    Aug 4, 2023 at 0:40
  • alias ls gives "ls='ls --color=auto'". So your analysis correct. @StéphaneChazelas Could you elaborate what is meant by having "search" access to the directory? We can only search if we can read the contents first right?
    – Pushparaj
    Aug 7, 2023 at 10:28
  • 2
    @Pushparaj, "search" access means the x permission bit. Calling it "execute" doesn't make sense for directories, and POSIX calls it "execute/search" at least in some places. I would prefer "access", since it really controls accessing the files within, not searching as in browsing the list of names. (Though one could "search" by picking a name and trying to access it. That the x bit would control.)
    – ilkkachu
    Aug 7, 2023 at 11:05

1 Answer 1

1

With r without x on the directory, you can indeed list the filenames within, but you can't get any other information on the files since you can't call stat() on them(*).

The error message and behaviour there is exactly what GNU ls shows if it's started with options that require it to determine the file type even for a short listing, e.g. -F or --color. You probably have ls aliased to something with options, check with alias ls. You can bypass the alias if you run the program with \ls, or /bin/ls. With -l, you also get the really confusing looking row of question marks.

$ mkdir dir; touch dir/file.txt; chmod a-x dir;
$ ls dir
ls: cannot access 'dir/file.txt': Permission denied
file.txt
$ \ls dir
file.txt
$ ls -l dir
ls: cannot access 'dir/file.txt': Permission denied
total 0
-????????? ? ? ? ?            ? file.txt
$ alias ls
alias ls='ls -vF --color=auto'

The behaviour depends on the ls implementation though, e.g. Busybox just croaks on the error and doesn't get around to printing the listing (and I didn't even use -F or --color here):

$ busybox ls dir
ls: dir/file.txt: Permission denied

(* Though some systems give the file type with readdir() too. I'm not sure why that's not enough for ls here.)

You must log in to answer this question.

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