127

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?

2
  • 2
    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 with sed 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 :) Apr 16, 2016 at 6:39
  • Damn, I was hoping that I wouldn't have to use -l but retain the date info. Nov 9, 2022 at 21:20

9 Answers 9

149

Try stat instead of ls:

stat -c "%y %s %n" *

To output in columnar format:

stat -c "%n,%s" * | column -t -s,
6
  • 3
    This is nice, but it does have the "environment too large" /"argument list too long" problem potentially.
    – Mat
    Oct 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"
    – f4m8
    Oct 13, 2011 at 7:27
  • 5
    To 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 with stat? Mar 17, 2018 at 15:43
  • 4
    That command seems to be for GNU stat. For MacOS/BSD, the command is stat -f "%m %z %N" [files...]. Jan 8, 2019 at 14:50
48

You could always use another utility like awk to format the output of ls1:

/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...

7
  • 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 (say alias ls='ls -i'). You really should take a lot of care if you want to go about parsing ls output.
    – Mat
    Oct 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...
    – jasonwryan
    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.)
    – Mat
    Oct 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.
    – jasonwryan
    Oct 7, 2011 at 6:44
  • 1
    love 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? ;)
    – abgordon
    Jan 24, 2019 at 20:34
37

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.

4
  • 1
    Above 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")
    – guettli
    Dec 1, 2016 at 9:47
  • 1
    Wish this worked on macs.
    – Jerinaw
    Jul 17, 2018 at 17:31
  • 1
    lovely, 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
  • @Jerinaw It works for me on MacOS Catalina, bash terminal. Sep 29, 2020 at 3:41
9

You can also use the 'date' command. It is very easy to use:

date -r [file name]

1
  • 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"
    – guettli
    Dec 1, 2016 at 9:38
8

If you wish to use ls, but preserve proper size, you can use:

ls -Ss1pq --block-size=1
1
  • Not sure why you added Spq options, can you elaborate?
    – karfau
    Mar 3, 2021 at 14:27
3

ls -s1 returns file size and name only on AIX, not sure about Linux

3
  • 4
    Can 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
  • This works on Solaris 10 as well - elegant, simple solution.
    – SAFX
    Jul 8 at 19:17
1

where space is defined as the separator and f6 means field 6

ls -lt | cut -d" " -f6-
1
  • 4
    it failed because ls uses spaces for indentation and sometimes it is -f6- other times it is -f5- Apr 16, 2016 at 6:37
1

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 one
  • cut -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.
1
  • 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.ma
    Jan 20 at 8:45
-1

You can pipeline two commands

ls -l|cut -d" " -f5
1
  • 2
    it failed because ls uses spaces for indentation and sometimes it is -f6- other times it is -f5- Apr 16, 2016 at 6:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .