If I have two files, tmp.txt
and tmp.pdf
, one large and one small, when I type ls -al tmp.*
everything is nicely right justified and I get this output
-rw-r--r-- 1 simon simon 615316 Oct 17 20:55 tmp.pdf
-rw-r--r-- 1 simon simon 0 Oct 17 20:55 tmp.txt
For a reason that doesn't matter, I want to be able to write the output of two separate ls -al
commands to a file, then cat
the file and obtain the same output. But of course, if I do this:
ls -al tmp.txt > foo
ls -al tmp.pdf >> foo
and then cat foo
, I get this
-rw-r--r-- 1 simon simon 0 Oct 17 20:55 tmp.txt
-rw-r--r-- 1 simon simon 615316 Oct 17 20:55 tmp.pdf
Is there a way of mimicking the justified output that ls -al
produces? Obviously, I can use wc -c tmp.pdf
etc to figure out which output is largest, but how would I translate that information into code that would put the requisite number of spaces before the 0 in the first line? Thanks very much for any suggestions.
Yes. Just use
column
However if your file names contain spaces then those names won't be printed correctly. If your
column
supports the-l
option then the simplest fix is to change tocolumn -t -l 9
to limit the total number of columns to 9. Another workaround is to usestat
to simulatels
outputFiles with tabs or newlines in their names still don't work though. You may try to change the delimiter to null with
stat --printf="%A\0%h\0%U\0%G\0%s\0%Y\0%n\n" tmp* | column -t -s $'\0' -l 9
but somehow mycolumn
doesn't recognize\0
as the delimiter. Not sure about other columns versions, just try it on your PC and see