how can I set permissions to a folder so all users not in my group, can only do ls on it? I don't get the difference between - and r.

  • You want people to be able to list the contents of a directory, but not to e.g. cd into it, or access subdirectories? Could you add a behaviour that you want to deny and one that you want to allow? – Kusalananda Oct 20 '19 at 8:22

The important flag is the "execute" flag on the directory, which on directories is actually a "traversable" flag. If it is not set, the directory cannot be used in a path. So: 1) you cannot CD to it, and 2) you cannot use the files in the directory.

Let's set up test ritg with a directory and a file

>>mkdir listonly
>>touch listonly/uselessfile

So far so good:

>>ls listonly/

Change the executable flag of the directory:

>>chmod -x listonly

You can no longer CD to it:

>>cd listonly
bash: cd: listonly: Permission denied

When you use ls you can see the files:

>>ls listonly/

However, in many cases ls is aliased to add some options that require looking at the metadata for each file (flags, timestamp, size...), which it cannot do since that requires using the directory in a path (for me that was ls --color=auto') so you have to use plain ls otherwise you get a of the files but with unsightly errors:

>>ls listonly/
ls: cannot access 'listonly/uselessfile': Permission denied

Note that file explorers typically change their working directory to the target directory to list its contents, so this may not play well with GUI interfaces.



#change current directory to /tmp/
vodka@vodka-PC:~$ cd /tmp/

#create my_folder directory in tmp
vodka@vodka-PC:/tmp$ mkdir my_folder

#chnage directory permissions to 775 
(all people can show dir content and make cd, 
but can not remove or create new files)
vodka@vodka-PC:/tmp$ chmod 775 my_folder/

#check permissions
vodka@vodka-PC:/tmp$ ls -ld my_folder
drwxrwxr-x 2 vodka vodka 4096 окт 20 11:26 my_folder

#create new test file
nano ./my_folder/test_file.txt

#chage file permission to 770 (only owner and member of groups can show file content and modify it)
vodka@vodka-PC:/tmp$ chmod 770 ./my_folder/test_file.txt

#test it. Switch to postgres user and show dir content. We see files in directory
vodka@vodka-PC:/tmp$ sudo su - postgres
postgres@vodka-PC:~$ ls -l /tmp/my_folder/
-rwxrwx--- 1 vodka vodka 4 окт 20 11:27 test_file.txt
#try to show file content, remove or create new files in dir
cat: /tmp/my_folder/test_file.txt: Permission denied
postgres@vodka-PC:~$ >/tmp/my_folder/test_file_2.txt
-bash: /tmp/my_folder/test_file_2.txt: Permission denied
postgres@vodka-PC:~$ rm /tmp/my_folder/test_file.txt
rm: remove write-protected regular file '/tmp/my_folder/test_file.txt'? y
rm: cannot remove '/tmp/my_folder/test_file.txt': Permission denied

So, other users can only show list of files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.