58

I remember doing someting like "XXX /home/user/dir/child/file" and it returned the owner and/or permission of:

/home
/home/user
/home/user/dir
/home/user/child
/home/user/child/file

But I don't remember what this command was. Anybody any idea?

2
  • Not seeing anything enlightening from apropos mode or apropos permissions on OS X and Debian. Do you know what platform/distribution you were using? Could it have been a site-local command? You could script such a tool using dirname and stat.
    – mrb
    Jul 9, 2013 at 17:22
  • Using Arch Linux, and a pretty vanilla installation so not too many extra tools installed. Note that it was owners AND/OR permissions, I'm not sure at this point. Either would be useful. Jul 10, 2013 at 6:43

6 Answers 6

57

I think you might be thinking of the tree command. For example:

$ tree -pufid apps/glassfish3/ | less
apps/glassfish3
[drwxr-xr-x saml    ]  apps/glassfish3/bin
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/bin
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/config
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/doc-files
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/javax
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/javax/annotation
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/javax/annotation/security
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/javax/annotation/sql
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/javax/decorator
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/javax/ejb
[drwxr-xr-x saml    ]  apps/glassfish3/glassfish/docs/api/javax/ejb/embeddable
...
...

The above switches do the following:

  • -p - permissions
  • -u - username/userid
  • -f - full path
  • -i - don't print indentation lines
  • -d - print directories only

References

5
  • 3
    I think it was asked to display the ancestors of /home/user/dir/child/file not the children. Jul 10, 2013 at 5:12
  • @RaphaelAhrens - yes I realize that but there isn't a command that I'm aware of that does exactly that, there is however the tree command that does what I showed and it's suspiciously similar to what the OP was thinking of.
    – slm
    Jul 10, 2013 at 6:18
  • Is the -u really necessary? It seems it's default here (Tree ver. 1.6.0 under GNU bash 4.2.45). Is there an option to silence the "username/userid"? Dec 13, 2013 at 23:42
  • 1
    @NikosAlexandris - if I drop that switch I do not get the username. My ver: tree v1.6.0. I'm on Fedora 19, GNU bash, version 4.2.45(1)-release (x86_64-redhat-linux-gnu) .
    – slm
    Dec 14, 2013 at 0:17
  • Different "defaults" between the bash versions? Thanks for informing. Dec 16, 2013 at 10:56
54

The command could have been:

namei -m /home/user/dir/child/file
2
  • 12
    namei -mo is great, that it also gives you the owner.
    – earthmeLon
    Jun 16, 2015 at 5:17
  • 2
    BEWARE: namei won't show linux ACL's or SELinux MAC's. I figured out that an ACL was blocking nginx by manually checking with sudo su nginx -s/bin/bash
    – Ray Foss
    Sep 29, 2016 at 0:36
3

After giving it some thougth I came up with this

#!/bin/sh
l_path=$1
while [ "$l_path" != / -a "$l_path" != . ]; do
     ls -ld $l_path
     l_path=$(dirname -- "$l_path")
done

The output looks like this

-rw------- 1 tant tant 181016423 Jun 25 23:49:17 2013 /home/tant/test_file
drwxr-xr-x 85 tant tant 5632 Jul  9 19:40:11 2013 /home/tant
lrwxr-xr-x 1 root wheel 8 Sep  4 23:53:27 2012 /home -> usr/home

I hope it is ok that it is in reverse order.

Based on the comments, here's a way to list from the root downwards:

#!/bin/sh
l_path=$1
while [ "$l_path" != / -a "$l_path" != . ]; do
     ls -ld $l_path
     l_path=$(dirname -- "$l_path")
done | sed '1!G;h;$!d'
8
  • 1
    You can reverse with | tac if you have it, or | sed '1!G;h;$!d' if not.
    – mrb
    Jul 9, 2013 at 17:44
  • 2
    This will break for relative paths. Do [ "$l_path" != / -a "$l_path" != . ] instead.
    – user26112
    Jul 9, 2013 at 17:52
  • @Evan Teitelman dirname should gives the full path Jul 9, 2013 at 17:57
  • 1
    @RaphaelAhrens no, it'll return a dot for relative path in the end.
    – rush
    Jul 9, 2013 at 17:58
  • 1
    Also, stat -l should probably be stat or ls -ld.
    – user26112
    Jul 9, 2013 at 17:59
0

In the directory of which you want to know the ancester's permissions and owners:

for i in $(seq 0 $(pwd | tr -cd / | wc -c)) ; do pwd ; ls -lad ; cd .. ; done

Note that after that, you'll be in / :) if you want to go back to where you were, wrap the command inside

HERE=$(pwd)
...
cd ${HERE}
0

List permissions of every directory in a directory path (up or down)

alias dirls='_dirls() { path="$(realpath $1)";_path="";for i in " " ${path//\// }; do _path="$_path$i/"; ls -ld $_path; done; unset _path; };_dirls'

alias dirls2='_dirls2() { path="$(realpath $1)"; while [[ "$path" != "/" ]]; do ls -ld "$path"; path="$(dirname "$path")"; done; ls -ldh /; };_dirls2'

eg:

# dirls /usr/local/bin
dr-xr-xr-x. 18 root root 236 Nov 11 19:01 /
drwxr-xr-x. 15 root root 181 Dec  7 11:50 /usr/
drwxr-xr-x. 15 root root 205 Nov 22 14:06 /usr/local/
drwxr-xr-x. 2 root root 4096 Dec  5 12:19 /usr/local/bin/

# dirls2 /usr/local/bin
drwxr-xr-x. 2 root root 4096 Dec  5 12:19 /usr/local/bin
drwxr-xr-x. 15 root root 205 Nov 22 14:06 /usr/local
drwxr-xr-x. 15 root root 181 Dec  7 11:50 /usr
dr-xr-xr-x. 18 root root 236 Nov 11 19:01 /
-1

The command you are looking for listing permissions and owners/groups is ls -l.

-l option is used for long listing format.

ls -l /path/to/list

In addition, if you want to list and the hidden files then add the -a(all) option.

ls -al /path/to/list

Also, if you want to list permissions in your subdirectories use -R (recursive) option.

ls -Rl /path/to/list

The first column displays the permissions(read(r), write(w), execute(x)) and some special permissions(directories(d), -(regular file)) and the 3rd and 4th column shows you the file/directory owner and group respectively.

1
  • 2
    I don't think that's what is asked for. Looks like the asker is looking for a list of permissions of directories that lead to a file.
    – Mat
    Jul 9, 2013 at 17:13

You must log in to answer this question.

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