Bash: lsup zur Anzeige der Dateiberechtigungen

Donnerstag, 25. Mai, 2023

Wie oft ich das schon im Linux Filesystem gebraucht habe: in einem aktuellen oder angegebenen Verzeichnis prüfen, ob der Linux-Benutzer XY bis dorthin “durchkommt” und Berechtigungen in einem Verzeichnis oder auf eine Datei hat.

Also schrieb ich ein Shellskript. Man ruft es mit einem Parameter für ein zu prüfendes Verzeichnisses auf. Oder ohne Parameter für das aktuelle Verzeichnis.

Ich fange mal mit dessen Ausgabe an:

$ lsup
drwxr-xr-x 1 root root       244 Apr  2 23:34 /
drwxr-xr-x 1 root root       100 Jun  4  2022 /home
drwx------ 1 axel autologin 1.7K May 25 18:17 /home/axel
drwxr-xr-x 1 axel autologin  232 May 25 19:50 /home/axel/tmp

Ab dem Root-Verzeichnis werden mit ls -ld alle Verzeichnisebenen bis zum angegebenen Punkt angezeigt.
Syntax:

$ lsup -h

===== LSUP v1.3 :: make an ls upwards ... =====

Show directory permissions above by walking from / to the given file.
You can add no or one or multiple files as params.

SYNTAX:
  lsup                 Walk up from current directory
  lsup FILE [FILE N]   Walk up from given file/ directory
                       If the target is relative it will walk up
                       to the current directory
  lsup -h              Show this help and exit

EXAMPLES:
  lsup
  lsup .
  lsup /var/log/
  lsup my/relative/file.txt
  lsup /home /tmp /var

Zur Installation: als User root;: cd /usr/bin/ und nachfolgenden Code in eine Datei namens “lsup” werfen und ein chmod 0755 lsup hinterher.

#!/usr/bin/env bash
# ======================================================================
#
# Make an ls -d from root (/) to given dir to see directory  
# permissions above
#
# ----------------------------------------------------------------------
# 2020-12-01  v1.0  Axel Hahn
# 2023-02-06  v1.1  Axel Hahn  handle dirs with spaces
# 2023-02-10  v1.2  Axel Hahn  handle unlimited spaces
# 2023-03-25  v1.3  Axel Hahn  fix relative files; support multiple files
# ======================================================================

_version=1.3

# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
function help(){
 local self=$( basename $0 )
 cat <<EOH

===== LSUP v$_version :: make an ls upwards ... =====

Show directory permissions above by walking from / to the given file.
You can add no or one or multiple files as params.

SYNTAX:
  $self                 Walk up from current directory
  $self FILE [FILE N]   Walk up from given file/ directory
                       If the target is relative it will walk up
                       to the current directory
  $self -h              Show this help and exit

EXAMPLES:
  $self
  $self .
  $self /var/log/
  $self my/relative/file.txt
  $self /home /tmp /var

EOH
}

# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

if [ "$1" = "-h" ]; then
    help
    exit 0
fi

test -z "$1" && "$0" "$( pwd )"

# loop over all given params
while [ $# -gt 0 ]; do
    # param 1 with trailing slash
    mydir="${1%/}"
    if ! echo "$mydir" | grep "^/" >/dev/null; then
        mydir="$( pwd )/$mydir"
    fi

    ls -ld "$mydir" >/dev/null 2>&1        || echo "ERROR: File or directory does not exist: $mydir" 
    ls -ld "$mydir" >/dev/null 2>/dev/null || exit 1

    mypath=
    arraylist=()
    arraylist+=('/')

    IFS="/" read -ra aFields <<< "$mydir"
    typeset -i iDepth=${#aFields[@]}-1

    for iCounter in $( seq 1 ${iDepth})
    do
        mypath+="/${aFields[$iCounter]}"
        arraylist+=( "${mypath}" )
    done

    # echo ">>>>> $mypath"
    eval "ls -lhd ${arraylist[*]}"
    shift 1
    test $# -gt 0 && echo
done

# ----------------------------------------------------------------------

Viel Spass damit!

Kommentar hinzufügen

Die Felder Name und Kommentar sind Pflichtfelder.