List files by last edited date

BashShell

Bash Problem Overview


I have a directory: /home/user/

How can I list every file in this directory (including those in sub directories) and order them by the date that they were last modified?

Bash Solutions


Solution 1 - Bash

You can use:

ls -Rt

where -R means recursive (include subdirectories) and -t means "sort by last modification date".


To see a list of files sorted by date modified, use:

ls -l -Rt

An alias can also be created to achieve this:

alias lt='ls -lht'
lt

Where -h gives a more readable output.

Solution 2 - Bash

If you'd like a master list in which all the files are sorted together by modification date, showing the directory they're in, but not grouped by directory, you can use this:

find . -type f -printf "%-.22T+ %M %n %-8u %-8g %8s %Tx %.8TX %p\n" | sort | cut -f 2- -d ' '

The result looks a lot like ls -l:

-rw-r--r-- 1 root     root         3892 08/11/2009 11:03:36 /usr/share/man/man1/xmllint.1.gz
-rw-r--r-- 1 root     root        22946 08/13/2009 11:59:20 /usr/share/man/man1/curl.1.gz
-rw-r--r-- 1 root     root          728 08/17/2009 12:06:33 /usr/share/man/man1/thunderbird.1.gz
-rw-r--r-- 1 root     root          873 08/18/2009 10:52:47 /usr/share/man/man1/libgnutls-config.1.gz
-rw-r--r-- 1 root     root         2552 08/19/2009 02:00:34 /usr/share/man/man3/Purple.3pm.gz
-rw-r--r-- 1 root     root         9546 08/19/2009 02:02:00 /usr/share/man/man1/pidgin.1.gz
-rw-r--r-- 1 root     root         2201 08/19/2009 02:02:46 /usr/share/man/man3/Pidgin.3pm.gz
-rw-r--r-- 1 root     root          926 08/19/2009 02:03:05 /usr/share/man/man1/purple-remote.1.gz
-rw-r--r-- 1 root     root        18052 08/19/2009 04:11:47 /usr/share/man/man1/mono.1.gz
-rw-r--r-- 1 root     root         1845 08/19/2009 04:11:47 /usr/share/man/man5/mono-config.5.gz


Mac OS X

For those of you using Mac OS X, option -printf is not available on BSD find (you will get this error: find: -printf: unknown primary or operator). Fortunately you can Install GNU find through Homebrew (there should be an option to Fink and Macports as well):

brew install findutils

After install it the GNU find should be available to you as gfind. So, all you need to do is change the line above to:

gfind . -type f -printf "%-.22T+ %M %n %-8u %-8g %8s %Tx %.8TX %p\n" | sort | cut -f 2- -d ' '

Solution 3 - Bash

MAC OSX 2019

If you don't care about what time it was created but you want your list sorted then use this command

==> ls -t

If you want to order and see date and user info use this command

===> ls -lt

Solution 4 - Bash

For zsh users, you could also use glob qualifiers (also documented on man zshexpn):

echo *(om)

Where o stands for sort order, up and m stands for last modification time.

This can be useful when used in a for loop or another command:

for file in *(^om); do
  [ -e "$file" ] || continue
  # do something with file orderer from least recently modified to last modified
done

Or chained with another glob qualifier:

last_modified_file=(*(om[1]))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartyView Question on Stackoverflow
Solution 1 - BashmipadiView Answer on Stackoverflow
Solution 2 - BashDennis WilliamsonView Answer on Stackoverflow
Solution 3 - BashMussa CharlesView Answer on Stackoverflow
Solution 4 - BashUlysse BNView Answer on Stackoverflow