Extracting File Names from Long Listing of Directory in UNIX
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The value of the variable "contents" is then reutrned to the PHP script. I am displaying the contents using the array which is used in the exec() function to hold the output of the shell script.
The problem that I am now facing is: How to extract only the name part of the directory / file from the output such as:
[CODE=php]-rw-r--r-- 1 root root 0 Aug 30 16:50 2_b
-rw-r--r-- 1 root root 0 Aug 30 16:50 2_a
-rw-r--r-- 1 root root 0 Aug 30 16:50 1_d
-rw-r--r-- 1 root root 0 Aug 30 16:50 1_c
-rw-r--r-- 1 root root 0 Aug 30 16:50 1_b
-rw-r--r-- 1 root root 0 Aug 30 16:50 1_a
-rw-r--r-- 1 root root 133 Aug 31 19:52 August.txt
-rw-r--r-- 1 root root 1963 Aug 31 20:30 2010 Programme[/CODE]
It is also important that file / directory name may contain blank spaces:
[CODE=php]-rw-r--r-- 1 root root 1963 Aug 31 20:30 2010 Programme[/CODE]
Here, the file name is: "2010 Programme".
Well, the purpose of extracting only file names is to enable the functionality of displaying the contents of that file after passing the file name as the parameter to another PHP script. We can create hyper links while listing the directory contents.
Please note that the solution I want is in PHP and not in Shell Script as the functionality is to be provided in the PHP Script while displaying the contents of the array that holds the output of the result returned by the function exec().
PHP has both 'opendir' and 'readdir', as well as 'stat'. so why do you need the shell script in the first place ? I.e. use these functions and create whatever output you like in the first place, rather than adjusting output you do not like.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Quote:
Originally Posted by Sergei Steshenko
PHP has both 'opendir' and 'readdir', as well as 'stat'. so why do you need the shell script in the first place ? I.e. use these functions and create whatever output you like in the first place, rather than adjusting output you do not like.
Well, I have not tried the "stat" function in PHP. But I have done this before:
Okay, my primary purpose is to learn and explore. That is why I tried to see if I could easily interact with a Shell Script from within a PHP Script. It's working fine. But I want to handle the output returned by the Shell Script program according to my requirement.
In other words, sometimes or often we may need to handle the output returned by a command or program found in UNIX / Linux when we are doing certain tasks through a Web Interface.
I got the idea of displaying file names along with the associated information from the web interface I am using in my project (within the company). When we click a log file name, its contents extracted as per the requirement and are then displayed in a columnar fashion. Isn't it great?
Okay, my primary purpose is to learn and explore. That is why I tried to see if I could easily interact with a Shell Script from within a PHP Script. It's working fine. But I want to handle the output returned by the Shell Script program according to my requirement.
In other words, sometimes or often we may need to handle the output returned by a command or program found in UNIX / Linux when we are doing certain tasks through a Web Interface.
I got the idea of displaying file names along with the associated information from the web interface I am using in my project (within the company). When we click a log file name, its contents extracted as per the requirement and are then displayed in a columnar fashion. Isn't it great?
What did you mean writing
Code:
while ($file)
?
What kind of thing (string or array) is $file in the above context ?
The item in red is apparently wrong, and it's a typical error with 'readir' - you are not in $dir directory, so you are checking $file in your CWD.
I am not a PHP guy; in Perl it is
Code:
my $file_prefixed_with_dir = "$dir/file";
if(-d $file_prefixed_with_dir)
{
print "$file_prefixed_with_dir is a directory\n";
}
else
{
print "$file_prefixed_with_dir is NOT a directory\n";
}
, i.e. you need to construct the correct path to item to be stat'ed.
<?php
$dir = "/path";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$ftype=filetype($dir . $file); # or is_file(), is_dir()
$mtime = filemtime($file); #you can use stat , but this is much easier
# now you can collect the mtime and file name and put into array.
# this array can be sorted and you can get the last 10 elements ( ie your tail -10 )
if ( $ftype == "file" ) {
echo "file: $file\n";
} elseif( $ftype == "dir" ) {
echo "Directory: $file\n";
}
}
closedir($dh);
}
}
?>
If in doubt, always go to the PHP documentation site.
Last edited by ghostdog74; 09-01-2010 at 12:06 AM.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Quote:
Originally Posted by Sergei Steshenko
What did you mean writing
Code:
while ($file)
?
What kind of thing (string or array) is $file in the above context ?
How many times
Code:
$file = readdir($dh);
is supposed to be executed ?
$file = readdir($dh);
Reads the next content / file from the specified directory that the Directory Handler $dh points to.
Inside the loop, the last statement, as given above, fetches the next file name. On failure, readdir() returns false. When it is false, the while loop while... is exited.
From the PHP Manual:
Code:
readdir — Read entry from directory handle
Description
string readdir ([ resource $dir_handle ] )
Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.
Return Values
Returns the filename on success, or FALSE on failure.
$file is a variable that is assigned the value of the statement readdir($dh). On success, it is the next file name, or else boolean false.
So, the while loop keeps executing as long as the $file doesn't contain a boolean false value.
I have written and run that PHP script and tested on Windows XP (IIS). I will check it on Linux as well.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Quote:
Originally Posted by Sergei Steshenko
The item in red is apparently wrong, and it's a typical error with 'readir' - you are not in $dir directory, so you are checking $file in your CWD.
I am not a PHP guy; in Perl it is
Code:
my $file_prefixed_with_dir = "$dir/file";
if(-d $file_prefixed_with_dir)
{
print "$file_prefixed_with_dir is a directory\n";
}
else
{
print "$file_prefixed_with_dir is NOT a directory\n";
}
, i.e. you need to construct the correct path to item to be stat'ed.
First, thanks for the Perl version.
The statement below:
PHP Code:
$dir = "test";
assigns the literal name test to the variable $dir. test is the directory found in the CWD, yes, you're right. So, the contents of the test directory would be displayed, and not of the CWD.
I tested it with:
PHP Code:
$dir = "../wwwroot";
and the script then displayed the contents / files of the current directory incuding, of cuorse, the directory "test" which is in the "../wwwroot/test" and of the other directories found in the current directory- provided that we click those directory names to see what they contain.
This statement creates a pointer or handler to the specified directory that we are going to work with:
PHP Code:
$dh = opendir($dir);
The statement below:
PHP Code:
if(filetype($file) == "dir") {...}
checks for the type of the file or content found in the specified directory.
filetype() returns file and dir values as string. So, if the return value is "dir" then branch accordingly.
Okay, we have is_dir() and is_file() functions also to know whether a given (file) name is a regular directory or a regular file.
Yes, you're right, we can give the absolute or relative path as well.
Well, you know Perl so I may bug you sometimes when I begin to learn Perl.
Reads the next content / file from the specified directory that the Directory Handler $dh points to.
Inside the loop, the last statement, as given above, fetches the next file name. On failure, readdir() returns false. When it is false, the while loop while... is exited.
From the PHP Manual:
Code:
readdir — Read entry from directory handle
Description
string readdir ([ resource $dir_handle ] )
Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.
Return Values
Returns the filename on success, or FALSE on failure.
$file is a variable that is assigned the value of the statement readdir($dh). On success, it is the next file name, or else boolean false.
So, the while loop keeps executing as long as the $file doesn't contain a boolean false value.
I have written and run that PHP script and tested on Windows XP (IIS). I will check it on Linux as well.
But your code contradicts the item in bold - you do not call readdir in a loop:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.