How can I use the ls command to find out a folder's owner and group, and what rights they each have?

8,771

Solution 1

I wonder why this is a "problem". The directory /root is the home directory of the root user, and you usually don't need to do anything in it. Accordingly, it has no execute permission for normal users, meaning that you can't enter it or list its contents. These are not things you should change, for security reasons.

Anyway, you can often find some guidance on usage of a command by running

man command

In man ls you'll find

-d, --directory
       list directories themselves, not their contents

Compare the effects:

$ ls /root
ls: cannot open directory '/root': Permission denied
$ sudo !!
sudo ls /root
[sudo] password for zanna: 
snap
$ ls -d /root
/root

I need root permission (gained just for one command by running sudo command) to list out the contents of this directory, but not to list the directory itself. That's because I have execute permission on the parent directory of /root, / (often confusingly called the root directory as it is the "root" of the filesystem from which the rest branches off).

The vast majority of directories on your system have read and execute permission for you by default. The /root directory is rather exceptional in its restrictive permissions.

Also in man ls is the tool to show the permissions, but it's not at all obvious which one it is there, so you might want to try running info ls, which shows, in the section explaining what information each option causes to be shown, the documentation for the -l option:

‘-l’
‘--format=long’
‘--format=verbose’
     In addition to the name of each file, print the file type, file
     mode bits, number of hard links, owner name, group name, size, and
     timestamp (*note Formatting file timestamps::), normally the
     modification time.  Print question marks for information that
     cannot be determined.

This still isn't very clear! Did you know that "file mode bits" means the permissions? The command chmod is an abbreviation of change mode. Now you know. So let's try the options together. I have annotated the output:

$ ls -dl /root       #note that the order of options does not matter here
drwx------ 8 root root 4096 Dec 17 14:06 /root
^ ^  ^  ^  ^  ^     ^    ^   ^   ^    ^     ^--file name
| |  |  |  |  |     |    |   |   |    |--last modified time
| |  |  |  |  |     |    |   |   |--day of month
| |  |  |  |  |     |    |   |--name of month *
| |  |  |  |  |     |    |--size
| |  |  |  |  |     |--group
| |  |  |  |  |--owner
| |  |  |  |--number of hard links to this file
| |  |  |--permission bits for "others" (any user/program)
| |  |--permission bits for group
| |--permission bits for owner
|--file type (d=directory)

*Depends on locale settings. If you use a non-English locale you can get English output for any command by running LC_ALL=C ls -dl /root

See the tag wiki for permissions for information on how to interpret the "mode bits". ls -l displays symbolic notation for the permissions, where r=read access, w=write access, x=executable, and - indicates no permission. These have different effects for directories and regular files. For directories, read access allows the contents of the directory to be listed if the x permission bit it also set, the x bit allows the directory to be entered, and w allows files within the directory to be deleted, created and renamed if the x bit is also set. It helps to remember that a directory is a file that stores a list of files - that's why you need write permission on the directory to create, rename or delete its contents - those actions change the list. This permission also allows you to force-write other changes to files inside the directory though.

If we are only interested in owners and permissions, ls -l gives us too much information. There is a more flexible command to show file metadata; it's stat. You can use it to list owner (%U) (u for user - most command line utilities call the owner of a file the user. In chmod, for example, u=owner and o=others - make sure you don't give permissions to o when you only want to give them to u!), group (%G), and list permissions in octal or symbolic notation:

$ stat -c "%U %G %a" /root
root root 700
$ stat -c "%U %G %A" /root
root root drwx------

See man stat for many other things stat can tell you about files.

Solution 2

To check the permissions type into the command line:

ls -dl

This will list you all folders and files of the current path you are in.

at the beginning of each line you will see some letters. e.g.: example

First letter = type of file (here d = directory / Folder) then 3x3 letter for the permissions

  • first 3 letters = permissions of the owner
  • second 3 letters = permissions of the group
  • third 3 letters = permission of all others

the letters are rwx

  • r = read permission
  • w = write permission
  • x = execute permission

if there is an - the permission is not set/given

in my example the owner has all permissions, the group and the others are not allowed to write (but to read and execute)

Solution 3

ls -dl /root

The -d option says that you want to see info on the directory itself rather than the files in it.

The -l option says to give a long list i.e. permissions, owner and group. The first three permissions are for owner, the second three for group and the third three for everyone else.

Share:
8,771

Related videos on Youtube

Frank
Author by

Frank

Updated on September 18, 2022

Comments

  • Frank
    Frank over 1 year

    Just switched completely from Windows to Ubuntu and am happy.

    I have a problem; there is a folder called /root. I want to know how I could use the ls command to find out the owner and group associated with this folder, and what rights they each have.

  • Frank
    Frank over 6 years
    I appreciate your detailed explanation. You just smoothened my experience with Ubuntu so far!
  • Martin Loeffler
    Martin Loeffler over 6 years
    Zanna, you are right, and your answer is way better than mine (I just started to code 4 weeks ago) and I tried to answer in my words. Now Changend it to ls -dl.Didn't expect to get downvotes. Maybe better to not answer questions.
  • Zanna
    Zanna over 6 years
    now your answer is very useful because it clearly explains the symbolic notation layout :)
  • Zanna
    Zanna over 6 years
    @Frank great to hear it was useful to you :)
  • Martin Loeffler
    Martin Loeffler over 6 years
    thx. I will try first to learn how to code and than come back to answer