473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getting last file from linux directory

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
6 2043
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
>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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
3
by: Kali K E | last post by:
Hi, I could not understand how I can do the following things in Python. Please help me. 1. First I have to find the current directory from where the script is invoked. 2. Next I have to form a...
6
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
6
by: Affan Syed | last post by:
Hi, I am getting this weird problem. I know what i am doing is strange.. i am using C++ vectors and fopen, but for some reason if i used ofstream in the similar scenario it would give me errors....
10
by: ken | last post by:
hello, i'm writing a c program on a linux system. i'm debugging a segmentation fault but i don't want it to dump a core file because the memory footprint of the program is over 300Mb and i don't...
13
by: ThaDoctor | last post by:
I would like to get started with programming in C, but I cant get it to work, I do not need any compiler or such. Only a basic explanation of the first program Hello World that print "Hello World"...
1
by: Reb | last post by:
Hi all, I have not successfully found a Javascript sample for getting next and previous links from a file... I figured that someone must have solved this problem already! ... here is what I'm...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.