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 or permission. Is this possible?
9 Answers
Try stat
instead of ls
:
stat -c "%y %s %n" *
To output in columnar format:
stat -c "%n,%s" * | column -t -s,
-
3This is nice, but it does have the "environment too large" /"argument list too long" problem potentially.– MatOct 7, 2011 at 7:02
-
6:-) Just a proof of concept. In Real Life[tm] this will be a
find . -type f -print0 | xargs -0 stat -c "%y %s %n"
– f4m8Oct 13, 2011 at 7:27 -
5To format the output of
stat
, you can add width information to the format string like C printf function, e.g."%y %8s %n"
, it's not documented, but seems works (coreutils 8.17, Fedora 18) Apr 7, 2013 at 7:47 -
Because with
ls
I can output it with a thousand separator char. How does it work withstat
? Mar 17, 2018 at 15:43 -
4That command seems to be for GNU
stat
. For MacOS/BSD, the command isstat -f "%m %z %N" [files...]
. Jan 8, 2019 at 14:50
You could always use another utility like awk
to format the output of ls
1:
/bin/ls -ls | awk '{print $7,$8,$9}'
1.Yes, generally, you shouldn't parse the output of ls but in this case the question specifically calls for it...
-
That doesn't print the file size though. And it only prints the first part of filenames with whitespace in them. And it can fail if
ls
is aliased (sayalias ls='ls -i'
). You really should take a lot of care if you want to go about parsing ls output.– MatOct 7, 2011 at 6:16 -
I had the file size in there and then edited it out (vague moment) - I'll restore it. And I agree about all the caveats re parsing
ls
, but that is what the OP wanted... Oct 7, 2011 at 6:31 -
I disagree, the OP wants the filenames, not the first part of filenames if the filename happens to have whitespace. (Using
/bin/ls
would avoid the alias problem.)– MatOct 7, 2011 at 6:34 -
That is understood implicitly: what is stated explicitly is that OP wants a solution with
ls
which we both agree is not going to satisfy the whitespace requirement. The/bin/ls
suggestion is a good one. Oct 7, 2011 at 6:44 -
1love this solution; in regards to previous comments about not printing size, you can get size with ls, and can use
-h
to make it human-readable. Specifically I used:ls -lah | awk '{print $5,$6,$7,$8}'
which yields:4.0K Jan 24 18:17
. Granted, the original solution doesn't say anything about awk, but in linux we should assume the output of a process will always be the input to another process, right? ;)– abgordonJan 24, 2019 at 20:34
You can get a lot of control about how you list files with the find
utility. ls
doesn't really let you specify the columns you want.
For example:
$ find . -maxdepth 1 -printf '%CY%Cm%Cd.%CH%CM\t%s\t%f\n'
20111007.0601 4096 .
20111007.0601 2 b
20111001.1322 4096 a
The argument to the printf
action is a detailed in the manpage. You can choose different time information, what size you want (file size or disk blocks used), etc. You can also make this safe for unusual file names if further processing is needed.
-
1Above input to find ('%CY%Cm%Cd.%C...') is long. At least GNU find has
%C+
(output "2016-08-29+10:57:56.9201257840") and%Cc
(output "Mo 29 Aug 2016 10:57:56 CEST")– guettliDec 1, 2016 at 9:47 -
1
-
1lovely, and if you want to print just the full path and size, this should work "find /path/to/ -printf '%h/%f %s\n'" Sep 28, 2018 at 8:06
-
You can also use the 'date' command. It is very easy to use:
date -r [file name]
-
Nice, this was new to me. Unfortunately it does not work for several files via globbing:
date -r foo*.txt
-->date: extra operand "foo2.txt"
– guettliDec 1, 2016 at 9:38
If you wish to use ls, but preserve proper size, you can use:
ls -Ss1pq --block-size=1
ls -s1 returns file size and name only on AIX, not sure about Linux
-
4Can you explain how/why you believe this answers the question? Jun 13, 2017 at 20:04
-
Best answer here. This works very well cross-platform. MacOS, Alpine Linux... @Scott
man ls
and click the Edit button here, Chief. May 6, 2022 at 18:08 -
where space is defined as the separator and f6 means field 6
ls -lt | cut -d" " -f6-
-
4it failed because ls uses spaces for indentation and sometimes it is -f6- other times it is -f5- Apr 16, 2016 at 6:37
Since ls
sometimes uses multiple spaces for formatting, use tr -s' '
to squeeze down multiple spaces into a single space, so that your cut
command always refers to the same column:
ls -l | tr -s' ' | cut -d' ' -f5-
Explanation
ls -l
:l
flag uses "long" formatting to show permissions, ownership, size, date, etc.tr -s' '
: squeeze multiple spaces into onecut -d' ' -f5-
: get from the 5th column to the end, which in this case is size, date, filename
Caveats
- This solution assumes there are no whitespace characters in your filenames.
- For flexibility in output column ordering, consider one of the solutions that uses
awk
.
-
This is useful. It's the only answer that I found working in a Busybox environment, where a lot of other commands are not available or have less options. I needed to adapt it slightly, I used
ls -lR --full-time | tr -s ' ' | cut -d" " -f5-
– Kar.maJan 20 at 8:45
You can pipeline two commands
ls -l|cut -d" " -f5
-
2it failed because ls uses spaces for indentation and sometimes it is -f6- other times it is -f5- Apr 16, 2016 at 6:37
ls
is great because it has very fast sorting by datetime, but the formatting is hard to deal with. I suggest using a token at --time-style like--time-style='+&%Y%m%d+%H%M%S.%N'
where the token is '&', using that as reference you can further parse the output withsed
so you can also backtrack as just before the token is the size! If someone want to post that as a complete answer, feel free to, I am too asleep right now :)-l
but retain the date info.