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.
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.
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.
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.
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.
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.
ls -1 *.h | sed 's/^/ /' | sed 's/$/ \\/'
Alternatively, you could say:
ls -1 *.h | sed 's/.*/ & \\/'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With