Jan 23 2011 10:42 SOMETHING 2007.12.20.avi
Jun 26 2009 SOMETHING 2009.06.25.avi
Feb 12 2010 SOMETHING 2010.02.11.avi
Jan 29 2011 09:17 SOMETHING 2011.01.27.avi
Feb 11 2011 20:06 SOMETHING 2011.02.10.avi
Feb 27 2011 23:05 SOMETHING 2011.02.24.avi


Feb 27 2011 23:05 SOMETHING 2011.02.24.avi
Feb 11 2011 20:06 SOMETHING 2011.02.10.avi
Jan 29 2011 09:17 SOMETHING 2011.01.27.avi
Jan 23 2011 10:42 SOMETHING 2007.12.20.avi
Feb 12 2010 SOMETHING 2010.02.11.avi
Jun 26 2009 SOMETHING 2009.06.25.avi

I generated the above list (Original) with:

ls -A -lR | grep "^-" | sed "s/.\{43\}/&\n/" | grep -v "^-" | while read ONELINE; do if echo $ONELINE | cut -d " " -f3 | grep -o '[0-9][0-9]:[0-9][0-9]' > /dev/null 2>&1; then echo $ONELINE | sed "s/.\{7\}/&$(date +%Y) /"; else echo $ONELINE; fi; done

How could I get the output where the newest file is at the top?

  • 2
    @user4724: how did yuo generate the list ? – user591 Feb 28 '11 at 20:30
  • @user4724: That doesn't generate the same type of outpout for me that you have - no matter as Gilles has the answer – user591 Feb 28 '11 at 21:46

On most unices, ls has a -t option. From the man page of my Debian box:

-t sort by modification time

Try the following command:

$ ls -lt

  • i need it recursively, sorry – LanceBaynes Feb 28 '11 at 21:21
  • +ls -lt will list a full directory, if 1 file changes in it. – LanceBaynes Feb 28 '11 at 21:22

Is that a text file you're trying to sort, or are you trying to view a directory listing by date? For the latter, use the -t flag to ls. For the former, see in particular the -M flag to GNU sort, which sorts by three-letter month name abbreviations. The following command sorts by the 3rd, 1st, 2nd and 4th columns, treating the second sort key as a month name.

sort -k 3 -k 1M -k2 -k4 in.txt

Do consider always using Japanese/ISO style dates, always in the order YYYY-MM-DD-HH-MM-SS (i.e. most significant first, constant column width). This way sorting by date is identical to a lexicographic sort.

  • Don't forget the leading zeroes on that date format suggestion. This is the format I use whenever I format the string myself. – stolenmoment May 26 '18 at 9:39
  • @stolenmoment You can use spaces instead of zeros if you don't mind spaces, the important point is a constant column with. 2018  5 26 works just as well as 2018 05 26 (as long as you don't go through markup that merges consecutive spaces…), what doesn't work well is 2018 5 26. – Gilles 'SO- stop being evil' May 26 '18 at 10:39
  • Yeah, I reflexively avoid spaces in filenames, I'm sure you can guess why. – stolenmoment May 26 '18 at 11:34

I’m not sure if the functionality was in the ls command when the original question was asked, but now you can use the option --full-time.


ls -t --full-time 

will sort with oldest time at the bottom; or if you use:

ls -tr --full-time

then it will reverse the order.

  • The question is about how to sort file by modification date/time. The -t option answers that question completely; --full-time isn’t really relevant. – G-Man Says 'Reinstate Monica' Feb 17 '17 at 21:05
  • The only thing -t option doesn't give you the year for files modified within six months where as --full-time gives you exactly that. You can also use time-style to format a date format to you liking. e.g ls -ltr --time-style+%Y-%m-%d\ %H:%M:%S – Bevan Thomas Feb 20 '17 at 8:35
  • I’m not sure what your point is.  ls -t doesn’t give you mode and owner of the files, the host name and operating system version, or the price of tea in China, either.  ls -t doesn’t even display the modification date at all.  But the question isn’t about displaying file information, it’s about sorting files by modification date.  You are not contributing to the answer of the question; you are adding nice-to-know supplementary information. When you get 50 reputation points, you can post that sort of information as a comment. – G-Man Says 'Reinstate Monica' Feb 21 '17 at 0:52

recursive (R) and pause (|more)

ls -ltR |more

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.