简体   繁体   中英

Replicate the right justification that ls does when using ls twice

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


-rw-r--r--  1  myuser  myuser  0       Oct  18  11:24  tmp.small
-rw-r--r--  1  myuser  myuser  113616  Oct  18  11:42  tmp.big

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 to column -t -l 9 to limit the total number of columns to 9. Another workaround is to use stat to simulate ls output


-rw-r--r--  1  myuser  myuser  1307    2020-10-18 12:08:45.360000000 +0700  tmp with space.txt
-rw-r--r--  1  myuser  myuser  0       2020-10-18 11:24:21.650000000 +0700  tmp.small
-rw-r--r--  1  myuser  myuser  113616  2020-10-18 11:42:04.150000000 +0700  tmp.big

Files 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 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 my column doesn't recognize \\0 as the delimiter. Not sure about other columns versions, just try it on your PC and see

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM