If you mean that you want to list symlinks along with the target paths they point to, with zsh
:
$ zmodload zsh/stat
$ stat -n +link /dev/*(@)
/dev/cdrom sr0
/dev/core /proc/kcore
/dev/fd /proc/self/fd
/dev/initctl /run/initctl
/dev/log /run/systemd/journal/dev-log
/dev/rtc rtc0
/dev/stderr /proc/self/fd/2
/dev/stdin /proc/self/fd/0
/dev/stdout /proc/self/fd/1
With the ast-open implementation of ls
, you can also do:
$ ls -d --format='%(name)s %(linkpath)s' /dev/*(@)
/dev/cdrom sr0
/dev/core /proc/kcore
/dev/fd
/dev/initctl /run/initctl
/dev/log /run/systemd/journal/dev-log
/dev/rtc rtc0
/dev/stderr /proc/self/fd/2
/dev/stdin /proc/self/fd/0
/dev/stdout /proc/self/fd/1
With GNU find
:
$ find /dev/ -maxdepth 1 -type l -printf '%p %l\n'
/dev/cdrom sr0
/dev/rtc rtc0
/dev/log /run/systemd/journal/dev-log
/dev/initctl /run/initctl
/dev/core /proc/kcore
/dev/stderr /proc/self/fd/2
/dev/stdout /proc/self/fd/1
/dev/stdin /proc/self/fd/0
/dev/fd /proc/self/fd
(unsorted and includes hidden files if any).
With GNU stat
:
$ command stat -c %N /dev/*(@)
'/dev/cdrom' -> 'sr0'
'/dev/core' -> '/proc/kcore'
'/dev/fd' -> '/proc/self/fd'
'/dev/initctl' -> '/run/initctl'
'/dev/log' -> '/run/systemd/journal/dev-log'
'/dev/rtc' -> 'rtc0'
'/dev/stderr' -> '/proc/self/fd/2'
'/dev/stdin' -> '/proc/self/fd/0'
'/dev/stdout' -> '/proc/self/fd/1'
path/to/*(@)
expands to the non-hidden files of type symlink in the path/to
directory, using the @
glob qualifier. That feature is zsh
-specific. In other shells you generally need to resort to find
to select files based on their type.
For instance, in bash 4.4+, and on GNU systems, the equivalent of expanding /dev/*(@)
would be something like:
readarray -td '' files < <(
LC_ALL=C find /dev/ -mindepth 1 -maxdepth 1 ! -name '.*' -type l -print0 |
sort -z
)
if (( ${#files[@]} )); then
stat -c %N -- "${files[@]}"
else
echo>&2 No match.
fi
ls
output (and what to do instead) – Marcus Müller Apr 05 '23 at 07:05