14

It may be a generic Linux question but I'll give it a try here.

I need a list of recently modified files very often and currently for it I am piping ls output to grep with the param on today or a certain hour, but I need to change the grep param frequently to get an updated list, being a bit unproductive.

Is there an easy command to list the last modified files on a certain path ?

2
  • An existing command not as far as I know. A script would be easy to make. Saved in $PATH would make it a command. Is that what you sre looking for? Commented Oct 11, 2015 at 16:28
  • yes it could work for me as a script too.
    – Tiberiu C.
    Commented Oct 11, 2015 at 16:29

5 Answers 5

24

To find the last 5 modified files from a certain directory recursively, from that directory run:

find . -type f -printf '%T@ %p\n' | sort -k1,1nr | head -5
  • %T@ with -printf predicate of find will get modification time since epoch for the files, %p will print the file names

  • sort -k1,1nr will reverse numerically sort the result according to the epoch time

  • head -5 will get us the last modified five file names

If you just want to search only in the current directory (not recursively), use stat:

stat -c '%Y %n' * | sort -k1,1nr | head -5

Or find:

find . -maxdepth 1 -type f -printf '%T@ %p\n' | sort -k1,1nr | head -5

Check man find and man stat to get more idea.

2
  • find . -type f -printf '%T@ %p\n' | sort -k1,1nr | head -5 gives me error in Azure: sort: fflush failed: standard output and sort: write error
    – Saibamen
    Commented Aug 6, 2021 at 7:46
  • 1
    Using %TY-%Tm-%Td-%TH:%TM:%TS in place of %T@ produces a more readable output. In that case you also want to omit -n.
    – x-yuri
    Commented Dec 19, 2021 at 4:38
8

It is so simple actually.To get the Most recent file in the currant directory:

ls -Art | tail -n 2

To get the Most recent file in another directory , for example in /opt/spark/ :

ls /opt/spark/ -Art | tail -n 1

This is when i use it on my cluster:

aa23916@picard03:/opt/spark/applicationHistory$ ls -Art | tail -n 2
app-20180124162809-0004
app-20180124163207-0005.inprogress

Simple and easy : )

2
  • 1
    If you have to explicitly state a folder it hardly seems recursive
    – sbergeron
    Commented Jan 24, 2018 at 17:32
  • -t already lists newest first, so reversing it gives you the least recently modified file. Commented Apr 16, 2020 at 17:58
6

The script below lists all files recursively inside a directory, edited shorter ago than an arbitrary time. Additionally, it displays the time span since the last edit.

It can be used with the command:

findrecent <directory> <time(in hours)>

as shown below:

enter image description here

In this example, all files on my Desktop, edited less than 36 hours ago are listed.

The script

#!/usr/bin/env python3
import os
import time
import sys

directory = sys.argv[1]

try:
    t = int(sys.argv[2])
except IndexError:
    t = 1

currtime = time.time(); t = t*3600

def calc_time(minutes):
    # create neatly displayed time
    if minutes < 60:
        return "[edited "+str(minutes)+" minutes ago]"
    else:
        hrs = int(minutes/60); minutes = int(minutes - hrs*60)
        return "[edited "+str(hrs)+" hours and "+str(minutes)+" minutes ago]"

for root, dirs, files in os.walk(directory):
    for file in files:
        file = os.path.join(root, file); ftime = os.path.getctime(file)
        edited = currtime - ftime
        if edited < t:
            print(file, calc_time(int(edited/60)))

How to use

  1. Create, if it doesn't exist yet, the directory ~/bin
  2. Copy the script above into an empty file, save it as findrecent in ~/bin
  3. Make the script executable
  4. Log out and back in and you're done

Note

  • if no time is given, the script lists all files, edited in the last hour
  • if a directory includes spaces, use quotes, like in the example
2
  • 2
    That really seems like overkill (reinventing the wheel) when so many tools already exist that can easily be piped together to do this. (As heemayl described.)
    – Wildcard
    Commented Oct 11, 2015 at 18:15
  • @Wildcard Tools, of which python is one, or: tools, used by python to control them in an easy way. The script was written in a minute into a flexible and easy to use option to list recently used files recursively. No doubt a matter of taste, an ongoing discussion I am afraid. Commented Oct 11, 2015 at 19:10
2

To get last 5 modified files execute the below command in the directory you looking for.

ls -lrt | tail -5
1

Using HseJar's and A Alnafessah's answer

find /myfolder/ -type f | xargs ls -lt | head

shows the latest, with a full path (including dates)

You must log in to answer this question.

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