3

This is my setup in /tmp/test/

if I use ls -l

-rw-r--r--  1 rubo77 rubo77    0 Okt 21 04:15 a
-rw-r--r--  1 rubo77 rubo77    2 Okt 21 04:16 b
drwxr-xr-x  2 rubo77 rubo77 4,0K Okt 21 03:58 c
lrwxrwxrwx  1 rubo77 rubo77    1 Okt 21 03:57 d -> c
lrwxrwxrwx  1 rubo77 rubo77    1 Okt 21 03:58 e -> a
lrwxrwxrwx  1 rubo77 rubo77    2 Okt 21 03:59 f -> nofile

If I use just ls I see only the files without details:

a b c d e f

ls -F appends an indicator (one of */=>@|) to entries

a  b  c/  d@  e@  f@

How can I achieve this display?

a  b  c/  d->c/  e->a  f->nofile
rubo77
  • 28,966
  • lame answer that doesn't quite do what you want it to: ls -l | grep "->". there's probably a way to get rid of the additional information, but I don't really feel like poking through the manpage right now. – strugee Oct 21 '13 at 02:41
  • I "poked" through the manpage and there seems to be no such option for ls. But maybe with another command? – rubo77 Oct 21 '13 at 02:44
  • Not with ls. You'll have to do your own stat and readlink then your own formatting in columns (you could call ls to leverage the coloring). – Gilles 'SO- stop being evil' Oct 21 '13 at 15:47
  • I don't think this can be done in a compact way. Especially since since sometimes a link can point to a really long path to another file, and this would look odd: a b c/ d->~/dev/kernel/net/wireless/mac80211/blah.c e->../../../dir/file f->e. Furthermore, what if the link is pointing to another link; how is that handled? It may be worth scripting to you, but probably not a default put into ls or any other listing programs. – Jacob Minshall Oct 21 '13 at 04:13

3 Answers3

1
#!/bin/bash

    ls -l | while read response
        do
            words=`echo $response | wc -w`      #count how many words are

            case "$words" in
                9) echo $response | cut -d " " -f9 # when file is not a symlink then the ouput prints only 9 fields
                    ;;
               11) echo $response | cut -d " " -f9-11 # when file is symlink its prints 11 fields indicating the target and symbol "->"
                   ;;
            esac
        done
andr3w
  • 86
0

If you buffer the output you can send it to column :

#!/bin/bash
TMP=/tmp/output-buffer
echo "">$TMP
ls -l | while read response
    do
        words=`echo $response | wc -w`

        case "$words" in
            9) echo $response | cut -d " " -f9 >>$TMP
                ;;
           11) echo $response | cut -d " " -f9-11 >>$TMP
               ;;
        esac
    done
cat $TMP | column
rm $TMP
rubo77
  • 28,966
0

a simple solution with some more information:

ls -hago | column

also interesting (but without the links shown):
This will show all files with human-readable sizes in columns:

ls -sh

These commands will do the job:

ls -lah | awk '{print $5, $9$10$11}' | column -t | column

or

ls -hago --color=no| sed 's/^[^ ][^ ]* *[^ ][^ ]* \( *[^ ][^ ]*\) ............/\1/' | column

with coloring it works too, but doesen't look so ordered:

if [ -t 1 ]; then color=yes; else color=no; fi
ls -hago --color="$color"| sed 's/^[^ ][^ ]* *[^ ][^ ]* \( *[^ ][^ ]*\) ............/\1/' | column
rubo77
  • 28,966