71

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?

  • no i dont think i try "ls -lT" its not found – WAEL Oct 10 '12 at 7:12
  • 2
    ls -lT is for mac osx – Kris Roofe Jun 7 '17 at 8:38
  • 1
    In general, Unix doesn’t keep track of the creation times of files, and, even when it does, ls generally doesn’t have a way to display it. So, in general, this is impossible. – G-Man Says 'Reinstate Monica' Apr 22 '18 at 2:46
103

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

ls --full-time.

| improve this answer | |
  • 1
    @bob: Completely agree with Turco. If you don't do that, nobody is going to bother about answering your question . – The Dark Knight Oct 10 '12 at 9:33
  • 5
    Example for time style ls -l --time-style=+%F – sobi3ch Sep 19 '17 at 6:56
  • 1
    For those who has already ll set as ls -la or something else, ll --full-time also work. – Meetai.com Feb 24 '19 at 3:05
  • 1
    ls -l --time-style=long-iso shows cleaner output since doesn't print seconds/miliseconds – Daniel Cambría Dec 29 '19 at 21:18
14

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 display complete time information in BSD (MacOS)

| improve this answer | |
  • 1
    ls -lT does not work on Debian (e.g. Raspberry Pi and Ubuntu). – Peter Mortensen Dec 28 '19 at 14:02
  • Thank you. Post edited. For GNU/Linux (as Raspbian in Raspberry Pi and Ubuntu) use ls -l --time-style=long-iso – Daniel Cambría Dec 29 '19 at 21:15
  • Your GNU/Linux variant should work on any GNU system, not only the one with Linux: ls comes from GNU coreutils there. – Ruslan Feb 7 at 6:54
  • I think so, but not tested with another operating systems using GNU utils, only Linux :) – Daniel Cambría Feb 9 at 21:35
6

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. :)

| improve this answer | |
  • 2
    Or ls --fu. GNU-style long options can be abbreviated as long as there's no ambiguity (--f wouldn't work as there's also a --file-type and --format). Note that a future version may introduce a --full-perms so it's dangerous to rely on that. – Stéphane Chazelas May 24 '18 at 15:19
  • 1
    Great to know! Its all about typing less. :) – G_Style May 24 '18 at 21:23
5

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.

| improve this answer | |
  • I like this. A lot less noise in the output and only one letter to remember in the future. – Eric May 8 '17 at 17:18
  • 3
    This seems to change the behavior of ls to date changed rather than the default date modified. – Mateen Ulhaq Apr 22 '18 at 1:41
  • 3
    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. – G-Man Says 'Reinstate Monica' Apr 22 '18 at 2:46
  • 3
    How in the world did this “answer” get seven votes? – G-Man Says 'Reinstate Monica' Apr 22 '18 at 2:46
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).

| improve this answer | |
  • 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. – Stéphane Chazelas Jan 21 '19 at 15:37
  • @StéphaneChazelas didn't know that! Thanks for the edit – Codebling Jan 21 '19 at 17:52
0

may favorite:

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

| improve this answer | |
  • As previously pointed out, the -T option will not work with GNU ls (also not specified in POSIX). Also, when providing code as an answer, it's better to explain what each part of it does. – Anthony Geoghegan Jan 21 '19 at 15:07
  • Thanks, this is the (only) correct answer here for macOS Catalina. – Zsolt Szatmari Aug 6 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.