Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I use ls -lah I get lots of information for each file.

How can I get only the name and filesize?

Additionally I would like to keep the listing of symlinks with the arrow like

lrwxrwxrwx  1 rubo77    rubo77       4 Nov 21 01:53 test2 -> test
share|improve this question

4 Answers 4

up vote 1 down vote accepted

Selecting columns to print with awk

One method would be to parse the output of ls.

Example

$ ls -lah | awk '{print $9, $5}' | tail -5
.yEd 4.0K
.youtube-dl 4.0K
.zenmap 4.0K
.zshrc 32
zzzz 3.3K

By the way, you can clean up the output using column.

$ ls -lah | awk '{print $9, $5}' | column -t | tail -5
z                                                   4
.youtube-dl                                         4.0K
.zenmap                                             4.0K
.zshrc                                              32
zzzz                                                3.3K

Selecting columns to remove with awk

If you'd rather remove the other columns, while keeping others you can use this awk method to blank out the undesirable columns.

Example

$ ls -lah | awk '{$1=$2=$3=$4=$6=$7=$8=""}1' | tail -5
    4.0K    .youtube-dl
    4    z -> zzzz
    4.0K    .zenmap
    32    .zshrc
    3.3K    zzzz

Eventual solution

The OP came up with this chain of commands, using a mix of the examples from above.

$ ls -lah | awk '{print $5, $9$10$11}' | column -t | column 
...
4.0K  .gphoto              773   .rdebug_hist     4.0K  .youtube-dl
1.5K  .grip                4.0K  .rdesktop        4     z->zzzz
share|improve this answer
    
cool, if I use ls -lah | awk '{print $5, $9$10$11}'|column I get what I wanted –  rubo77 Nov 21 '13 at 0:59
    
@rubo77 - see updates, for another method to blank out the unwanted columns too. –  slm Nov 21 '13 at 1:00
    
you should trim each line and sent that to column –  rubo77 Nov 21 '13 at 1:01
    
Your awk strategy doesn't preserve alignment. The ones using column inclusion truncate file names containing spaces; the one with column removal doesn't have this defect. Also piping into another command suppresses the colors that many people use. –  Gilles Nov 21 '13 at 1:08
    
If you expect files with leading or trailing spaces you could use ls -lahQ to quote filenames instead –  rubo77 Nov 21 '13 at 1:11

From the man page:

  -s, --size
          print the allocated size of each file, in blocks

So for human-readable sizes:

ls -sh
share|improve this answer
    
I was looking for this to find a solution to How can I list all files and symlinks in a compact view? so this looks promising, but it doesen't show the symlink targets as I was hoping, like ls -lah does. (I added this to the question) –  rubo77 Nov 21 '13 at 0:52
    
if you want to see hidden files too, use ls -ash –  rubo77 Nov 21 '13 at 8:22

One quick and dirty way is to combine the output of ls -lah with a couple of other commands:

ls -lah | tr -s ' ' | cut -d' ' -f5,9-

The tr -s command replaces multiple spaces with single spaces, and the cut -d' ' -f5,9- prints columns 5 and 9 (and beyond). The 9- is required to account for additional space-separated columns produced by symlinks.

share|improve this answer
    
If I try to columnize that symlinks gets stretched at the arrow : ls -lah | tr -s ' ' | cut -d' ' -f5,9- | column -t | column –  rubo77 Nov 21 '13 at 2:09
    
Well your question doesn't say anything about printing in columns. –  Greg Hewgill Nov 21 '13 at 2:33
    
you are right, that was just related to my other question: How can I list all files and symlinks in a compact view? –  rubo77 Nov 21 '13 at 8:17

The closest you can get with ls only is to suppress the user and group columns with ls -log. If you want to go further, you can parse the output. Beware that the second column (the link count) has variable width. The following shell snippet takes care to preserve column alignment, copes with arbitrary file names (except newlines if they're passed literally), and displays the output in color (remove that part if you aren't running GNU coreutils).

if [ -t 1 ]; then color=yes; else color=no; fi
ls -h -log --color="$color" | sed 's/^[^ ][^ ]* *[^ ][^ ]* \( *[^ ][^ ]*\) ............/\1/'
share|improve this answer
    
Is it possible to get this columnized like ls without options does? –  rubo77 Nov 21 '13 at 1:22
    
@rubo77 Columns don't play so well with displaying symlink targets, which tend to get pretty wide. If you really want it, piping through column is the way to go. –  Gilles Nov 21 '13 at 1:29
    
This answer is also fine, you could use: ls -halog --color=no | sed 's/^[^ ][^ ]* *[^ ][^ ]* \( *[^ ][^ ]*\) ............/\1/'|column –  rubo77 Nov 21 '13 at 2:13
    
but slm's answer with awk is easyer to remember –  rubo77 Nov 21 '13 at 2:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.