How can I list (ls) the 5 last modified files in a directory?
Try using head or tail. If you want the 5 most-recently modified files:
ls -1t | head -5
The -1 (that's a one) says one file per line and the head says take the first 5 entries.
If you want the last 5 try
ls -1t | tail -5
Get most recent file in a directory on Linux
ls -Art | tail -n 1
This will return the latest modified file or directory. Not very elegant, but it works.
Used flags:
-A
list all files except .
and ..
-r
reverse order while sorting
-t
sort by time, newest first
How to recursively find the latest modified file in a directory?
find . -type f -printf '%T@ %p\n' \
| sort -n | tail -1 | cut -f2- -d" "
For a huge tree, it might be hard for sort
to keep everything in memory.
%T@
gives you the modification time like a unix timestamp, sort -n
sorts numerically, tail -1
takes the last line (highest timestamp), cut -f2 -d" "
cuts away the first field (the timestamp) from the output.
Edit: Just as -printf
is probably GNU-only, ajreals usage of stat -c
is too. Although it is possible to do the same on BSD, the options for formatting is different (-f "%m %N"
it would seem)
And I missed the part of plural; if you want more then the latest file, just bump up the tail argument.
Program to check all last modified files in a folder using python?
In Windows a copy of a file probably has a new creation time. You can look at os.path.getctime()
to check the creation time for the copy of the file.
If that works as expected then you could include os.path.getctime()
as an additional check in the key to max()
.
def latest_change(filename):
return max(os.path.getmtime(filename), os.path.getctime(filename))
latest_file = max(list_of_files, key=latest_change)
The key function just grabs whichever of the modification or creation time is greatest, and then uses that greatest time as the key.
List all files modified in last 5 minutes excluding .svn directories
Try a different order of arguments. Your command:
- If an entry is accessed within five minutes and is a directory and has the name
.svn
, then the entry is ignored - otherwise, for all other cases, if it's a file, print the name
The following command prunes .svn
directories before descending into them:
find . -type d -name .svn -prune -o -mmin -5 -type f -print
If a file is a directory and has the name .svn
, ignore it and do not descend into it either. Otherwise, if it is last modified (-mmin
) within 5 minutes and a file, print the filename.
C#: Get the 5 newest (last modified) files from a directory
Here's a general way to do this with LINQ:
Directory.GetFiles(path)
.Select(x => new FileInfo(x))
.OrderByDescending(x => x.LastWriteTime)
.Take(5)
.ToArray()
I suspect this isn't quite what you want, since your code examples seem to be working at different tasks, but in the general case, this would do what the title of your question requests.
Related Topics
Using Find - Deleting All Files/Directories (In Linux ) Except Any One
In Order to Write Pci Ethernet Driver. How to Implement Mmap in the Pci Ethernet Driver
Redirect All Output to File Using Bash on Linux
How to Calculate an Md5 Checksum of a Directory
When to Use Kernel Threads VS Workqueues in the Linux Kernel
Is There an Equivalent to Com on *Nix Systems ? If Not, What Was the *Nix Approach to Re-Usability
How to Add Date String to Each Line of a Continuously Written Log File
Git Pull: Change Authentication
Thread Utilization Profiling on Linux
How to Exclude Absolute Paths for Tar
The Irq in Kernel Function Asm_Do_Irq() Is Different from the One I Request in Module
What Happens When You Run a Program