23

How can I use ls in linux to get a listing of filenames date and size only? I don't need to see the other info such as owner, permission.

migrated from stackoverflow.com Oct 7 '11 at 3:45

This question came from our site for professional and enthusiast programmers.

  • which Linux are we talking about? – tolitius Oct 7 '11 at 3:25
  • apache server.. – Pinkie Oct 7 '11 at 3:30
25

ls -l | awk '{print $5, $6, $7, $9}'

This will print the file size in bytes, month, date, and filename.

jin@encrypt /tmp/foo % ls -l
total 0
drwxr-xr-x  2 jin  wheel  68 Oct  4 12:43 bar
drwxr-xr-x  2 jin  wheel  68 Oct  4 12:43 baz
drwxr-xr-x  2 jin  wheel  68 Oct  4 12:43 quux

jin@encrypt /tmp/foo % ls -l | awk '{print $5, $6, $7, $9}'
68 Oct 4 bar
68 Oct 4 baz
68 Oct 4 quux
  • @Sosukudo Yeah, I'm not sure either. But people seem to agree that it's ok to downvote bad questions but not the answers if they're useful. meta.stackexchange.com/questions/98197/… – Jin Oct 7 '11 at 3:35
  • @Sosukodo: feel free to upvote now :) – sehe Oct 7 '11 at 8:54
  • 5
    no support for filenames with multiple spaces – meso_2600 Mar 30 '16 at 13:33
11

Technically, it's not possible with ls, but find can do the same job with its -printf switch:

find -maxdepth 1 -printf '%t %s %p\n'
  • 1
    You're suggestion is my winner, and I'd recommend that if people are interested in more fields for the printf they RTFM. For example, show me the permissions for all files which are not a directory : find usr/lib/ -not -type d -printf '%M %p\n output: -rw-r--r-- usr/lib/x86_64-linux-gnu/apr-util-1/apr_crypto_openssl-1.so ... Teach a man to fish and all that... – Craig Feb 17 '17 at 15:58
  • Question is regarding linux so not unreasonable answer, but for the record this requires GNU find. – Dan Pritts Feb 11 at 17:25
4

you can always do:

$ ls -l
total 0
-rw-r--r--  1 user  staff  0 Oct  6 23:29 file1
-rw-r--r--  1 user  staff  0 Oct  6 23:29 file2
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file3
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file4
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file5
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file6
-rw-r--r--  1 user  staff  0 Oct  6 23:30 file7

cut it to:

$ ls -l | cut -f 8-13 -d ' '

0 Oct  6 23:29 file1
0 Oct  6 23:29 file2
0 Oct  6 23:30 file3
0 Oct  6 23:30 file4
0 Oct  6 23:30 file5
0 Oct  6 23:30 file6
0 Oct  6 23:30 file7

$ 
  • 4
    doesnt work with: variable ownerships, groups, file sizes – meso_2600 Mar 30 '16 at 13:34
  • Cut does not collapse consecutive delimiters, where as ls -l uses whitespace padding. If your columns are not uniform length (which column 5, file size will often not), cut will not select columns correctly. Collapsing consecutive spaces will help (watch out for spaces in file names). ls -l | tr -s ' ' | cut -f 8-13 -d ' ' – Dave May 30 '17 at 16:53
4

Another non-ls way:

> stat --printf='%y\t%12s\t%-16n|\n' tmp.*
2017-06-15 10:42:07.252853000 +0200         10485760    tmp.1           |
2017-06-15 10:41:25.659570000 +0200              666    tmp.TKPzm3BfRw  |

Explanation: %y = human-readable modification date; %s = size in bytes (%12s right-aligned, length 12); %n = file name (%-16n left-aligned, length 16); \t = tab, \n = linefeed. | = literal pipe char, just to show the end of the file name.

Like ls, stat has no options to select which files to show. (That can be done by shell globbing as shown above or some find ... -print0 | xargs -r0 stat ..., for example.)

  • Question was about linux, so your answer is fine, but for anyone who cares, here's an equivalent for FreeBSD stat. stat -f '%N %Sm %z' filename – Dan Pritts Feb 11 at 17:38
2

Slight variation on tolitius

ls -lh | cut -f 6- -d ' '
  • 1
    Care to explain what the variation adds? – Ivo Flipse Jul 9 '12 at 11:37
  • Just removes the need to specify the number of the end field by using the 6- in stead of 6-x – zzapper Jul 9 '12 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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