Find the oldest file (recursively) in a directory
Hm. Nadia's answer is closer to what you meant to ask; however, for finding the (single) oldest file in a tree, try this:
import os
def oldest_file_in_tree(rootfolder, extension=".avi"):
return min(
(os.path.join(dirname, filename)
for dirname, dirnames, filenames in os.walk(rootfolder)
for filename in filenames
if filename.endswith(extension)),
key=lambda fn: os.stat(fn).st_mtime)
With a little modification, you can get the n
oldest files (similar to Nadia's answer):
import os, heapq
def oldest_files_in_tree(rootfolder, count=1, extension=".avi"):
return heapq.nsmallest(count,
(os.path.join(dirname, filename)
for dirname, dirnames, filenames in os.walk(rootfolder)
for filename in filenames
if filename.endswith(extension)),
key=lambda fn: os.stat(fn).st_mtime)
Note that using the .endswith
method allows calls as:
oldest_files_in_tree("/home/user", 20, (".avi", ".mov"))
to select more than one extension.
Finally, should you want the complete list of files, ordered by modification time, in order to delete as many as required to free space, here's some code:
import os
def files_to_delete(rootfolder, extension=".avi"):
return sorted(
(os.path.join(dirname, filename)
for dirname, dirnames, filenames in os.walk(rootfolder)
for filename in filenames
if filename.endswith(extension)),
key=lambda fn: os.stat(fn).st_mtime),
reverse=True)
and note that the reverse=True
brings the oldest files at the end of the list, so that for the next file to delete, you just do a file_list.pop()
.
By the way, for a complete solution to your issue, since you are running on Linux, where the os.statvfs
is available, you can do:
import os
def free_space_up_to(free_bytes_required, rootfolder, extension=".avi"):
file_list= files_to_delete(rootfolder, extension)
while file_list:
statv= os.statvfs(rootfolder)
if statv.f_bfree*statv.f_bsize >= free_bytes_required:
break
os.remove(file_list.pop())
statvfs.f_bfree
are the device free blocks and statvfs.f_bsize
is the block size. We take the rootfolder
statvfs, so mind any symbolic links pointing to other devices, where we could delete many files without actually freeing up space in this device.
UPDATE (copying a comment by Juan):
Depending on the OS and filesystem implementation, you may want to multiply f_bfree by f_frsize rather than f_bsize. In some implementations, the latter is the preferred I/O request size. For example, on a FreeBSD 9 system I just tested, f_frsize was 4096 and f_bsize was 16384. POSIX says the block count fields are "in units of f_frsize" ( see http://pubs.opengroup.org/onlinepubs/9699919799//basedefs/sys_statvfs.h.html )
Finding the oldest folder in a directory in linux even when files inside are modified
How To Get File Creation Date Time In Bash-Debian
You'll want to read the link above for that, files and directories would save the same modification time types, which means directories do not save their creation date. Methods like the ls -i
one mentioned earlier may work sometimes, but when I ran it just now it got really old files mixed up with really new files, so I don't think it works exactly how you think it might.
Instead try touching a file immediately after creating a directory, save it as something like .DIRBIRTH
and make it hidden. Then when trying to find the order the directories were made, just grep for which .DIRBIRTH
has the oldest modification date.
How to get oldest file in a directory using Java
Based off of @Yoda comment, i figured i would answer my own question.
public static void main(String[] args) throws IOException {
File directory = new File("/logFiles");
purgeLogFiles(directory);
}
public void purgeLogFiles(File logDir){
File[] logFiles = logDir.listFiles();
long oldestDate = Long.MAX_VALUE;
File oldestFile = null;
if( logFiles != null && logFiles.length >499){
//delete oldest files after theres more than 500 log files
for(File f: logFiles){
if(f.lastModified() < oldestDate){
oldestDate = f.lastModified();
oldestFile = f;
}
}
if(oldestFile != null){
oldestFile.delete();
}
}
}
How to find oldest and newest file in a directory?
Try using pathlib, also getmtime
gives the last modified time, you want the time file was created so use getctime
if you strictly want only files:
import os
import pathlib
mypath = 'your path'
taggedrootdir = pathlib.Path(mypath)
print(min([f for f in taggedrootdir.resolve().glob('**/*') if f.is_file()], key=os.path.getctime))
print(max([f for f in taggedrootdir.resolve().glob('**/*') if f.is_file()], key=os.path.getctime))
if results may include folders:
import os
import pathlib
mypath = 'your path'
taggedrootdir = pathlib.Path(mypath)
print(min(taggedrootdir.resolve().glob('**/*'), key=os.path.getctime))
print(max(taggedrootdir.resolve().glob('**/*'), key=os.path.getctime))
Find and delete oldest AVI file recursively
Use File.mtime(filename)
to get the last modified time of the file.
movie_file_path.sort_by {|f| File.mtime(f)}
will return a sorted array by mtime
. You can then delete the file using File.delete(filename)
.
Edit: Last accessed time atime
might be a better option than mtime
.
how to get the oldest file in a directory fast using .NET?
The short answer is no. Windows file systems don't index files by date so there is no native way to do this, let alone a .net way without enumerating all of them.
Related Topics
Total Memory Used by Python Process
Oserror 38 [Errno 38] with Multiprocessing
Auto.Arima() Equivalent for Python
How to Handle Os.System Sigkill Signal Inside Python
Writing to Shared Memory in Python Is Very Slow
How to Default Python3.8 on My MAC Using Homebrew
Python Error - "Importerror: Cannot Import Name 'Dist'"
Pandas Column of Lists, Create a Row For Each List Element
What Is a Clean "Pythonic" Way to Implement Multiple Constructors
Cron Job: How to Run a Script That Requires to Open Display
Decrypt Chrome Linux Blob Encrypted Cookies in Python
What Are Python Pandas Equivalents for R Functions Like Str(), Summary(), and Head()
Cannot Get Environment Variables in Django Settings File
How to Get Each Dependent Command Execution Output Using Paramiko Exec_Command