Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format the output of ls?

Tags:

linux

bash

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.

like image 791
Martin Drozdik Avatar asked Aug 05 '13 07:08

Martin Drozdik


People also ask

What is the format of ls?

Long Listing Format. The default output of the ls command shows only the names of the files and directories, which is not very informative. The -l ( lowercase L) option tells ls to print files in a long listing format.

What is the output of the ls?

The output of the ls command displays only the size of a file and does not include indirect blocks used by the file. Any empty blocks of the file also get included in the output of the command.

How do you write ls output to a file?

List files and output the result to a file. Type the ls > output. txt command to print the output of the preceding command into an output. txt file.

How do I size an ls file?

Using the ls Command–l – displays a list of files and directories in long format and shows the sizes in bytes. –h – scales file sizes and directory sizes into KB, MB, GB, or TB when the file or directory size is larger than 1024 bytes. –s – displays a list of the files and directories and shows the sizes in blocks.


2 Answers

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.

like image 83
Mat Avatar answered Oct 04 '22 22:10

Mat


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

Alternatively, you could say:

ls -1 *.h | sed 's/.*/    & \\/'
like image 35
devnull Avatar answered Oct 04 '22 21:10

devnull