Martin Drozdik
Martin Drozdik

Reputation: 13343

How to format the output of ls?

I would like to output all headers in a directory. There is one entry per line and each line should begin with four whitespaces and should end with a whitespace and a '\' character.

____header1.h_\
____header2.h_\
____header3.h_\

I already figured out how to make the output one entry per line.

ls -1 *.h

But I do not know how to do the formatting. Where should I look to learn more complicated formatting?

EDIT:

All the scripts in all the answers produce the desired output. I wish I could accept all answers.

Upvotes: 9

Views: 20798

Answers (5)

Ricardo Padua Soares
Ricardo Padua Soares

Reputation: 447

see

find . -maxdepth 3 -type f -iname "*" -printf "%h,%f,%CY-%Cm-%Cd %CT,%s,%u,%M\n"

Upvotes: 11

Harka Győző
Harka Győző

Reputation: 1

ls -l *.h|rev|ts "\\"|rev|ts "   "

( if ls output is not an interactive terminal, it will display filenames line by line)

Upvotes: 0

devnull
devnull

Reputation: 123708

ls -1 *.h | sed 's/^/    /' | sed 's/$/ \\/'

Alternatively, you could say:

ls -1 *.h | sed 's/.*/    & \\/'

Upvotes: 6

Vijay
Vijay

Reputation: 67319

ls -l *.h|awk '/\.h/{print "    "$0" \\"}'

Or in a more simple way in awk:

> ls -1 *.h | awk '$0="    "$0" \\"'

Tested :

> ls -1 *.hh 
Algorithms.hh
Timer.hh
a.hh
> ls -1 *.hh | awk '/\.hh/{print "    "$0" \\"}'
    Algorithms.hh \
    Timer.hh \
    a.hh \
> 

Or you can use perl:

ls -1 *.h | perl -plne '$_="    ".$_." \\";'

Upvotes: 3

Mat
Mat

Reputation: 206929

You can use printf and shell globbing rather than attempt to format ls output.

Try something like:

$ printf '    %s \\\n' *.h
    a.h \
    b.h \
    c.h \

ls is really meant as an "interactive" tool for humans, avoid using it for anything else.

Upvotes: 13

Related Questions