Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a file, say /var/some_dir_1/some_dir_2/some_dir_3/some_file, is there a single command that will list the permissions of some_file and all the directories in its path from root, i.e. the permissions for var, some_dir_1, some_dir_2, some_dir_3?

share|improve this question

You can use namei with -l (long) on an absolute path:

namei -l /absolute/path/to/file

It will give you something like:

dr-xr-xr-x root  root   /
drwxr-xr-x root  root   absolute
drwx------ user1 group1 path
drwxr-xr-x user1 group1 to
-rw-r--r-- user1 group1 file

You must provide full path. If you don't want to type it you can use realpath or readlink.

namei -l $(readlink -m relative_path_to/file)
share|improve this answer
    
Welcome to SuperUser, and thanks for updating an old question with an even better answer! – glallen Nov 29 '14 at 21:54

The following bash script prints all permissions of the directory entry passed as argument and all its parents up to /:

#!/usr/bin/env bash

[[ $# -eq 1 ]] || exit 1

FILEPATH="$1"

while true ; do
    ls -ld "$FILEPATH"
    [[ "$FILEPATH" != "/" ]] || exit
    FILEPATH="$( dirname "$FILEPATH" )"
done

Save as e.g. parent_permissions.sh and run using /path/to/parent_permissions.sh /path/to/file.

It only works with absolute paths unless combined with readlink -f or the abspath script from this answer, in which case you need to change the initial assignment of FILEPATH to:

FILEPATH="$( abspath "$1" )"

On Linux, this might work (untested):

FILEPATH="$( readlink -f "$1" )"

Example output:

drwxr-xr-x  66 danielbeck  staff  2244  2 Feb 12:38 /Users/danielbeck
drwxr-xr-x  11 root  admin  374  1 Feb 15:21 /Users
drwxrwxr-t  35 root  admin  1258 22 Jan 23:09 /

Add arguments to the ls call, or replace it e.g. with getfacl, as appropriate on your system to print ACLs and extended attributes if you're interested in them.

share|improve this answer

You must log in to answer this question.

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