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 
4
  • 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. Commented Oct 21, 2013 at 2:41
  • I "poked" through the manpage and there seems to be no such option for ls. But maybe with another command? Commented Oct 21, 2013 at 2:44
  • 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. Commented Oct 21, 2013 at 4:13
  • 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). Commented Oct 21, 2013 at 15:47

3 Answers 3

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 
3
  • this will show it like test2 -> test but only one line per file. I would like to see it compact as many files as fit in one line already Commented Nov 20, 2013 at 11:53
  • maybe we find a solution here.: How do I echo a line with linebreak at the end at window border? Commented Nov 20, 2013 at 12:01
  • How can I use column to format the complete output in columns? Commented Nov 20, 2013 at 13:49
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 
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 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.