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.
5 Answers
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/…– JinOct 7, 2011 at 3:35
-
-
6
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'
-
1You'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...– CraigFeb 17, 2017 at 15:58 -
Question is regarding linux so not unreasonable answer, but for the record this requires GNU find. Feb 11, 2019 at 17:25
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
-
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 ' '
– DaveMay 30, 2017 at 16:53
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
Feb 11, 2019 at 17:38
Slight variation on tolitius
ls -lh | cut -f 6- -d ' '
-
1
-
Just removes the need to specify the number of the end field by using the 6- in stead of 6-x– zzapperJul 9, 2012 at 13:32