Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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