![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX and Linux Applications Discuss UNIX and Linux software applications. This includes SQL, Databases, Middleware, MOM, SOA, EDA, CEP, BI, BPM and similar topics. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Finding the oldest file in a particular directory | pavan_movva | Shell Programming and Scripting | 4 | 04-08-2009 07:24 AM |
| Finding the oldest file | janet | Shell Programming and Scripting | 5 | 03-23-2009 10:31 AM |
| Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names | nikosey | Shell Programming and Scripting | 6 | 07-30-2008 09:46 PM |
| Removing the oldest file in a directory | pavan_movva | Shell Programming and Scripting | 2 | 10-10-2006 11:38 AM |
| Oldest File In A Directory | bergerj3 | UNIX for Dummies Questions & Answers | 2 | 02-27-2002 03:31 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Finding the oldest file in a directory without ls
I am trying to determine the oldest and most recent files in a huge directory. I am using an ls -tr statement outside my find statement. The directory is too big and I am getting an "arg list too long" error. Is there something I can put in my find statement that doesn't create a list to determine the earliest and most recent files so I do not have to use ls -tr and head -1 or tail -1. Below is one of the statements that I am trying to use.
firstFilePath=`ls -tr \`find "$fileDir" -name "$fileMask"\*"$fileType" -exec /bin/ls '{}' \;\` | head -1` Thanks! |
|
||||
|
This is a good reason to consider a "filing system method" for large numbers of files - create a lot of strategic sub-directories - otherwise directory commands take forever.
There is no cure for the time these commands take except to to get your directory under control. This is one way that does not require tail, which is slower than head. Code:
ls -t | head -1 | read oldest ls -rt | head -1 | read youngest echo " oldest = $oldest, youngest = $youngest" |
|
||||
|
Code:
sub oldestFile {
my $oldValue = 9999999999;
my $oldestFile;
my $file;
my $statHandle;
opendir(DIR, $DIR) || die "Unable to open Dir : $DIR <$!> \n";
@files_arr = readdir(DIR);
closedir(DIR);
foreach(@files_arr) {
next if ( /^\./ || -d $DIR.$_ );
$file = $DIR . $_;
$statHandle = stat($file);
if ( $statHandle->mtime < $oldValue ) {
$oldValue = $statHandle->mtime;
$oldestFile = $file;
}
}
return $oldestFile;
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|