By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

getting last file from linux directory

P: n/a
I have been presented with the task of getting the last file from
a linux directory when the files are of the form:
nnnnnn-xxxxxxx-.ext

where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.

I could read the entire directory in an sort, but I hope there is
an easier way.

suggestions ?

bill
Feb 18 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
bill wrote:
I could read the entire directory in an sort, but I hope there is
an easier way.
$dir = '/some/dir/';
$filename = `ls -1r $dir | head -1`;
# Note that $filename probably contains a line break
# at the end. You might want to strip it out.

is a quick and dirty way. Note this works in both PHP and Perl with no
modifications! :-)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 18 '07 #2

P: n/a
>I have been presented with the task of getting the last file from
>a linux directory when the files are of the form:
nnnnnn-xxxxxxx-.ext

where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.
Define "last". Files do not have an intrinsic ordering.
>I could read the entire directory in an sort, but I hope there is
an easier way.
You may have to read the entire directory in, get the timestamp
(which one? There are several.) for each file, and sort based on
most recent timestamp.

You also might want to avoid files that don't match the given filename
pattern. Files with names matching *.core tend to appear even when
not intended.

Feb 18 '07 #3

P: n/a
Gordon Burditt wrote:
>I have been presented with the task of getting the last file from
a linux directory when the files are of the form:
nnnnnn-xxxxxxx-.ext

where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.

Define "last". Files do not have an intrinsic ordering.
>I could read the entire directory in an sort, but I hope there is
an easier way.

You may have to read the entire directory in, get the timestamp
(which one? There are several.) for each file, and sort based on
most recent timestamp.

You also might want to avoid files that don't match the given filename
pattern. Files with names matching *.core tend to appear even when
not intended.
"last" is numerically highest nnnnnn

bill
Feb 19 '07 #4

P: n/a
Toby A Inkster wrote:
bill wrote:
>I could read the entire directory in an sort, but I hope there is
an easier way.

$dir = '/some/dir/';
$filename = `ls -1r $dir | head -1`;
# Note that $filename probably contains a line break
# at the end. You might want to strip it out.

is a quick and dirty way. Note this works in both PHP and Perl with no
modifications! :-)
thanks

bill
Feb 19 '07 #5

P: n/a
bill <no****@spamcop.netwrites:
Gordon Burditt wrote:
>>I have been presented with the task of getting the last file from a
linux directory when the files are of the form:
nnnnnn-xxxxxxx-.ext

where nnnnnn are 6 numeric digits and xxxxxx is variable a/n data.

Define "last". Files do not have an intrinsic ordering.
>>I could read the entire directory in an sort, but I hope there is
an easier way.

You may have to read the entire directory in, get the timestamp
(which one? There are several.) for each file, and sort based on
most recent timestamp.

You also might want to avoid files that don't match the given filename
pattern. Files with names matching *.core tend to appear even when
not intended.
"last" is numerically highest nnnnnn

bill
So you have answered your own question. Read all files, filter ones with
numeric prefixes and sort (ksort) based on the numeric part of the file
name.
Feb 19 '07 #6

P: n/a
On Mon, 19 Feb 2007 06:54:01 -0500, bill wrote:
Toby A Inkster wrote:
>bill wrote:
>>I could read the entire directory in an sort, but I hope there is
an easier way.

$dir = '/some/dir/';
$filename = `ls -1r $dir | head -1`;
# Note that $filename probably contains a line break
# at the end. You might want to strip it out.

is a quick and dirty way. Note this works in both PHP and Perl with no
modifications! :-)

thanks
Do a sanity check on the results. If ever an alpha-named file gets in
that directory, it will appear first (`head -1`).

Jonesy
--
Marvin L Jones | jonz | W3DHJ | linux
38.24N 104.55W | @ config.com | Jonesy | OS/2
*** Killfiling google posts: <http://jonz.net/ng.htm>
Feb 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.