Re: [PHP] Find largest integer filename
Hi, Wednesday, July 6, 2005, 2:43:38 PM, you wrote: RL Suppose I have a directory with a HUGE number of filenames, all of which RL happen to look like integers: RL ~/nntp/1 RL ~/nntp/2 RL ~/nntp/3 RL . RL . RL . RL ~/nntp/59874 RL ~/nntp/59875 RL ~/nntp/59876 RL Now, in a PHP script, what's the most efficient way to find the largest RL filename, where largest means in the sense of an integer, not a string? RL I could probably force the filenames to be 0-padded if that would make a RL significant difference. RL Is there some nifty shell command I should just exec?... RL I don't really care which shell, but I'm a bash user generally. RL Obviously I could loop through all the filenames, so I'm looking for RL something faster than that. :-) RL -- RL Like Music? RL http://l-i-e.com/artists.htm You could try: $last = exec('ls -D /fullpath/nntp | sort -n'); echo Largest: $lastbr; -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Find largest integer filename
Suppose I have a directory with a HUGE number of filenames, all of which happen to look like integers: ~/nntp/1 ~/nntp/2 ~/nntp/3 . . . ~/nntp/59874 ~/nntp/59875 ~/nntp/59876 Now, in a PHP script, what's the most efficient way to find the largest filename, where largest means in the sense of an integer, not a string? I could probably force the filenames to be 0-padded if that would make a significant difference. Is there some nifty shell command I should just exec?... I don't really care which shell, but I'm a bash user generally. Obviously I could loop through all the filenames, so I'm looking for something faster than that. :-) I can't think of anything you could do in the shell that would be faster... Anything you do in the shell is going to involve 'ls' which is going to have to loop through all the filenames anyway... so you might as well do it within PHP and save yourself the exec and other processes to sort/trim the list. I suppose you could try some sort of hunt and peck search... say you had some idea how many files where in there (under say 10,000). Then you could maybe do something like checking if 5000 exists. If it does, check 7500. If it doesn't, check 6250. If it does, check 6875 and so on until you're down to a reasonable gap. Then just check incrementally from there. Whether or not that's going to be faster than just looping through them all I don't know... good luck! -philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Find largest integer filename
I can't believe I'm posting this :-p I'm nowhere near being a unix/linux guru, and one would probably have a dozen complaints with this if they saw it, but ls -l | grep -v total | cut -f 11 -d ' ' | sort -n | tail -1 Your milage may vary if ls -l displays something slightly different than mine does, then try adjusting which field cut takes. HTH, Matt Philip Hallstrom wrote: Suppose I have a directory with a HUGE number of filenames, all of which happen to look like integers: ~/nntp/1 ~/nntp/2 ~/nntp/3 . . . ~/nntp/59874 ~/nntp/59875 ~/nntp/59876 Now, in a PHP script, what's the most efficient way to find the largest filename, where largest means in the sense of an integer, not a string? I could probably force the filenames to be 0-padded if that would make a significant difference. Is there some nifty shell command I should just exec?... I don't really care which shell, but I'm a bash user generally. Obviously I could loop through all the filenames, so I'm looking for something faster than that. :-) I can't think of anything you could do in the shell that would be faster... Anything you do in the shell is going to involve 'ls' which is going to have to loop through all the filenames anyway... so you might as well do it within PHP and save yourself the exec and other processes to sort/trim the list. I suppose you could try some sort of hunt and peck search... say you had some idea how many files where in there (under say 10,000). Then you could maybe do something like checking if 5000 exists. If it does, check 7500. If it doesn't, check 6250. If it does, check 6875 and so on until you're down to a reasonable gap. Then just check incrementally from there. Whether or not that's going to be faster than just looping through them all I don't know... good luck! -philip -- Matt Blasinski (mbv) Information Systems Technology Services Professional Internet Infrastructure Applications Technology Division of Information Technology 3228 Computer Science and Statistics 1210 West Dayton Street Madison WI 53706 Work (608) 263-4865 Personal Cell (608) 347-6940 ?php echo You can have it fast, cheap, or working. Choose two.; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Find largest integer filename
Suppose I have a directory with a HUGE number of filenames, all of which happen to look like integers: ~/nntp/1 ~/nntp/2 ~/nntp/3 . . . ~/nntp/59874 ~/nntp/59875 ~/nntp/59876 Now, in a PHP script, what's the most efficient way to find the largest filename, where largest means in the sense of an integer, not a string? I could probably force the filenames to be 0-padded if that would make a significant difference. Is there some nifty shell command I should just exec?... I don't really care which shell, but I'm a bash user generally. Obviously I could loop through all the filenames, so I'm looking for something faster than that. :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Find largest integer filename
Richard Lynch wrote: Suppose I have a directory with a HUGE number of filenames, all of which happen to look like integers: [...] Now, in a PHP script, what's the most efficient way to find the largest filename, where largest means in the sense of an integer, not a string? [...] Is there some nifty shell command I should just exec?... How about this: $output = exec(cd $myDirectory; ls -C -1 | sort -n -r | head -n1); That's assuming your version of ls supports the -C (list entries by column) and -1 (list one file per line) options. Mine does (Debian stable) but if you're using a different *nix it may not. HTH... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php