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`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 :) – Aquarius Power Apr 16 '16 at 06:39
-
Damn, I was hoping that I wouldn't have to use `-l` but retain the date info. – Sridhar Sarnobat Nov 09 '22 at 21:20
9 Answers
Try stat instead of ls:
stat -c "%y %s %n" *
To output in columnar format:
stat -c "%n,%s" * | column -t -s,
- 209
- 2
- 9
- 1,919
- 1
- 11
- 5
-
3This is nice, but it does have the "environment too large" /"argument list too long" problem potentially. – Mat Oct 07 '11 at 07: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 '11 at 07: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) – LiuYan 刘研 Apr 07 '13 at 07:47
-
Because with `ls` I can output it with a thousand separator char. How does it work with `stat`? – Peter VARGA Mar 17 '18 at 15:43
-
I don’t see how this answers the question?! It’s a nice outside the box solution, but you’d lose all benefits from `ls`. What about colours?? – MS Berends Aug 08 '18 at 20:15
-
4That command seems to be for GNU `stat`. For MacOS/BSD, the command is `stat -f "%m %z %N" [files...]`. – Christopher Schultz Jan 08 '19 at 14:50
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...
- 71,734
- 34
- 193
- 226
-
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 07 '11 at 06: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 07 '11 at 06: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 07 '11 at 06: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 07 '11 at 06: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? ;) – abgordon Jan 24 '19 at 20:34
-
Since `ls` was explicitly asked, and `awk` proposed as a solution in this response, the space in filenames problem may be solved with a similarly unadvised approach: `/bin/ls -lt | awk '{printf $5};{$1=$2=$3=$4=$5=$6=$7=$8=""};{print $0}'` (macos /bin/ls, /usr/bin/awk, zsh) – Rich Andrews Jul 08 '22 at 13:54
-
This *still* doesn't show the size **or** the filename, not even a chunk of the latter. And if `--time-style=long-iso` isn't given all it prints is `month day year` for old files and `month day time` for recent ones. – tink Aug 29 '22 at 22:12
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.
- 51,578
- 10
- 158
- 140
-
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") – guettli Dec 01 '16 at 09: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'" – Pierluigi Vernetto Sep 28 '18 at 08:06
-
You can also use the 'date' command. It is very easy to use:
date -r [file name]
- 78,313
- 42
- 165
- 222
- 191
- 1
- 2
-
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 01 '16 at 09:38
If you wish to use ls, but preserve proper size, you can use:
ls -Ss1pq --block-size=1
- 89
- 1
- 1
ls -s1 returns file size and name only on AIX, not sure about Linux
- 69
- 1
-
4Can you explain how/why you believe this answers the question? – Scott - Слава Україні Jun 13 '17 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. – Joel Mellon May 06 '22 at 18:08
-
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:lflag 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.
- 603
- 6
- 14
-
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 '23 at 08:45
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- – Aquarius Power Apr 16 '16 at 06:37
-
2it failed because ls uses spaces for indentation and sometimes it is -f6- other times it is -f5- – Aquarius Power Apr 16 '16 at 06:37