Command to list permission of file and all folders from root to the file's path

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?


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)

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.