`ls -l` for all parent directories

This question has already been answered in superuser.com (I don't know if I can mark a question from one site as duplicate from another). The solution is as simple as writing (assuming I am in the same directory as the target filename):

$ namei -l $(pwd)/somefile   ## or `namei -l $(realpath -s somefile)`

Because of -l, it lists basic permissions in long format for each parent directory.

I have to use pwd/realpath because namei doesn't resolve relative paths. If I'm not in the target directory, just write the full path.


I made this small script that does this. I use cd "$1"; pwd to get the current directory so that paths are not canonicalized (say, if you try magic-ls . and your current directory is /var/lib/postgres, but that is a symlink to /mnt/postgres, you will get /var, /var/lib and /var/lib/postgres, while using realpath you would get /mnt and /mnt/postgres)

magic-ls() {
    local current=$(cd "$1"; pwd)

    while [[ $current != '/' ]]; do
        ls -ld "$current"
        current=$(dirname "$current")
    done
}

Here's an example output:

[leodag@desk ~]$ magic-ls
drwx------ 1 leodag leodag 2722 jun 21 13:49 /home/leodag
drwxr-xr-x 1 root root 18 mai  2  2019 /home

By the way it will also work with no argument since cd "" does not change your directory.

Edit: removed realpath from the while check, since that could lead to unexpected results if there was a link to / in the path, and was unneeded.


Just using parameter expansion:

#!/usr/bin/env bash
path="$1"
while test -n "$path"; do
    ls -lLd "$path"
    path="${path%/*}"
done

calling method :

bash test.sh /var/lib/program/subfolder/somefile

giving

-rw-r--r-- 1 root root 0 Jun 21 18:49 /var/lib/program/subfolder/somefile
drwxr-xr-x 1 root root 4096 Jun 21 18:49 /var/lib/program/subfolder
drwxr-xr-x 1 root root 4096 Jun 21 18:49 /var/lib/program
drwxr-xr-x 1 root root 4096 Jun 21 18:49 /var/lib
drwxr-xr-x 1 root root 4096 Jun 13 19:24 /var

I wrote a bash script for you. It'll have some bugs, if you have space in names. If it bothers you, I'm happy for changes recommendations in the comments.

#!/bin/bash
if [ ! -z "$1" ] && [ -e "$1" ]
then
 path=`realpath -s "$1"` # read argument as absolute path
else
 path="$PWD" # No valid argument, so we take pwd
fi
paths=""
while [ "$path" != / ];do
 paths+=" $path" 
 path=`dirname "$path"`
done
paths+=" $path" # Adding / to pathlist too 
ls -ld $paths

With realpath -s you can catch the absolute path, but you wont follow the link. If no argument is given, we will use pwd as the file/directory to list.

We append each path to a list. This gives us the advantage of a better layout in the end, so that we get a nice table because we run ls only once.

Output:

bobafit:~$ magic_ls_-l_command /usr/bin/python3
drwxr-xr-x 21 root root   4096 Jun 20 10:07 /
drwxr-xr-x 14 root root   4096 Sep  5  2019 /usr
drwxr-xr-x  2 root root 110592 Jun 20 10:07 /usr/bin
lrwxrwxrwx  1 root root      9 Apr  7 12:43 /usr/bin/python3 -> python3.8

Tags:

Linux

Bash

Ls