114

I am working on a Red Hat server. The commands ls -l or ll giving me the date and time in format +"%b %-d %H:%M".

I want to list the files in a way where the year when each was file created would appear within the date.

How is that possible?

0

7 Answers 7

156

You can use man ls and here you can find the --time-style parameter. Or you can use:

Command Sample Output
ls --full-time 2024-04-09 13:24:44.108466043 -0500
ls -l --time-style=long-iso 2024-04-09 08:58
3
  • 5
    Example for time style ls -l --time-style=+%F
    – sobi3ch
    Commented Sep 19, 2017 at 6:56
  • 4
    For those who has already ll set as ls -la or something else, ll --full-time also work.
    – Meetai.com
    Commented Feb 24, 2019 at 3:05
  • 3
    ls -l --time-style=long-iso shows cleaner output since doesn't print seconds/miliseconds Commented Dec 29, 2019 at 21:18
24

ls -l will display month, day and year - since, according to BSD man page: If the modification time of the file is more than 6 months in the past or future, then the year of the last modification is displayed in place of the hour and minute fields.

So, to make sure that year will always be shown, use:

ls -l --time-style=long-iso (GNU/Linux)

ls -lT will display complete time information in BSD (MacOS)

3
  • Your GNU/Linux variant should work on any GNU system, not only the one with Linux: ls comes from GNU coreutils there.
    – Ruslan
    Commented Feb 7, 2020 at 6:54
  • I think so, but not tested with another operating systems using GNU utils, only Linux :) Commented Feb 9, 2020 at 21:35
  • "If the modification time of the file is more than 6 months in the past or future, then the year of the last modification is displayed". This is what I was looking for!
    – asgs
    Commented Sep 20, 2023 at 16:05
12

In addition to Jan Marek's answer.... I've noticed you can get away with just:

ls --full or ls --fu

which will do the same thing as ls --full-time as he described. Thanks Stéphane Chazelas. Now I type ls --fu everywhere. :)

0
6

Since you asked for the year, ls -lac is an easy one to remember if, like me, you use ls -la all the time. The c gives you ctime which will display a year if it's not the current year or the hour and minute if it is.

4
  • I like this. A lot less noise in the output and only one letter to remember in the future.
    – Eric
    Commented May 8, 2017 at 17:18
  • 4
    This seems to change the behavior of ls to date changed rather than the default date modified. Commented Apr 22, 2018 at 1:41
  • 5
    ls -l displays date and time for dates that are in the past six months, and date and year for other dates.  ctime can be in the past six months just as much as mtime (modification date) can, so ls -lac can display times (instead of years) just as much as ls -la can.  Besides, as Mateen Ulhaq points out, ls -lac does not display the same dates that ls -la does. This answer is wrong. Commented Apr 22, 2018 at 2:46
  • This -c is actually a modifier for -l and -t. It changes the display style for the former, and sorting criteria for the latter. According to manual: −c Use time of last modification of the file status information instead of last modification of the file itself for sorting (−t) or writing (−l).
    – foxesque
    Commented Nov 2, 2022 at 12:56
3

As an alternative to --full-time and its iso-format you can also use the --time-style parameter appending the values you need:
ls . -l --time-style=+%Y/%m/%d
or +%y-%m-%d, maybe listing with -al instead (hidden files too)... That depends on your requirements.

2

If you're using busybox (embedded distros, e.g. OpenWRT, LEDE), the switch you're looking for is -e for versions up to 1.26.2 and --full-time for 1.27.0 and above (see the commit that changed it).

2
  • 1
    -e also works with Solaris ls and ast-open's, so it's actually closer to a standard than GNU's --full-time. A shame that busybox removed it in newer versions. Commented Jan 21, 2019 at 15:37
  • @StéphaneChazelas didn't know that! Thanks for the edit
    – Codebling
    Commented Jan 21, 2019 at 17:52
0

This will work on macOS Catalina and later:

ls -altT /path/to/some/dir
0

You must log in to answer this question.

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