2

I found a way to show the date in iso-format: How to make ls produce ISO 8601 format

Now I want to show only the time, not the columns before:

ls --time-style=long-iso  -l
drwxr-xr-x 11 foo foo  4096 2018-03-06 14:17 mydir

How can I make the output look like this?

2018-03-06 14:17 mydir

A solution without cut/grep/sed/awk/tr is preferred.

4 Answers 4

4

You can't with ls alone. But find has a -printf action that allows for custom output. See the manual, especially the section about -printf format.

me@ubuntu:~> find . -maxdepth 1 -printf '%TY-%Tm-%Td %TH:%TM %f\n'
2019-02-07 11:25 .
2019-02-05 18:58 foo.txt
2019-02-05 17:40 bar.txt
2019-01-22 16:47 other.file
2019-02-05 16:42 README.txt

Here %TY denotes the year of the file's modification time, %Tm the month, and so on. %f is the filename. The -maxdepth 1 avoids recursion.

2
  • 2
    ! -path . excludes the cwd (source) and the -printf argument can be shortened to '%TF %TR %f\n'. I don’t know why that’s not in find’s manpage, but well, it’s all just strftime… :)
    – dessert
    Feb 8, 2019 at 12:18
  • That TF/TR is a good finding. Thanks, @dessert.
    – PerlDuck
    Feb 8, 2019 at 12:21
4

You can use cut to do this job for you pretty easily

hostname$ ls --time-style=long-iso  -l | cut -f 6- -d " "
2019-02-06 02:57 Desktop/
2019-02-07 02:08 Documents/
2019-02-08 04:02 Downloads/
2019-01-29 04:04 Pictures/
2018-12-15 13:35 VirtualBox VMs/
2019-02-08 03:34 custom-repo/

As was stated in the comments, this only works under certain circumstances. However I think this is something you can do with awk as well (I in no way claim to be an awkspert :-) nor am I even close to one) but I came up with this awk solution

hostname$ ls --time-style=long-iso  -la | awk '{print $6, $7, $8, $9}'
Jan 29 08:34 .xinitrc
Dec 17 17:55 .xinitrc.bkp
Feb 8 04:21 .zcompdump
Feb 8 04:58 .zdirs
Oct 11 16:51 .zprofile
Dec 10 10:17 .zsh_history
Feb 7 01:48 .zshrc.local
Feb 6 02:57 Desktop/
Feb 7 02:08 Documents/
Feb 8 04:02 Downloads/
Jan 29 04:04 Pictures/
Dec 15 13:35 VirtualBox
Feb 8 03:34 custom-repo/

which seems to work with conditions presented in comments

2
  • 2
    Basically correct but fails if you have one- and two-digit numbers (e.g. 5 and 10) in the second column (of ls -l). Then the one-digit numbers are padded with blanks in front and cut -f6- -d" " sees an empty column.
    – PerlDuck
    Feb 8, 2019 at 11:37
  • Nice! I'm not an awkspert either ;-) but found this one: awk '{$1=$2=$3=$4=$5=""; print}'. It sets the first 5 fields to empty before printing.
    – PerlDuck
    Feb 8, 2019 at 12:20
2

You can use stat -c"%y %n" and its output comes quite close to what you want, however without further processing it’s not exactly the same, e.g.:

$ stat -c"%y %n" *
2019-02-08 00:36:58.025402641 +0100 file.docx
2019-02-08 00:36:55.845416494 +0100 file.txt
2019-02-02 16:50:59.572351725 +0100 test

To delete the seconds and time zone part you can use sed as follows:

$ stat -c"%y %n" * | sed 's/:[^:]*+[0-9]*//'
2019-02-08 00:36 file.docx
2019-02-08 00:36 file.txt
2019-02-02 16:50 test

If you want to do that reasonably, i.e. without relying on text processing, you should use date. Note however that this calls both stat and date once for every single file/directory processed, which is not efficient.

$ for i in *; do printf '%s %s\n' "$(date -d@$(stat -c%Y "$i") +'%F %R')" "$i"; done
2019-02-08 00:36 file.docx
2019-02-08 00:36 file.txt
2019-02-02 16:50 test
1

ls --time-style=+%Y-%m-%d

should work nicely you can also use different date modifiers

this is according to man ls

show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT;

You must log in to answer this question.

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