LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2010, 02:36 PM   #1
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
Extracting File Names from Long Listing of Directory in UNIX


Hi,


I am calling a Shell Script, namely "scanTmp.sh" from within a PHP script using exec().


The Shell Script, in fact, has only one task to perform:

[CODE=php]contents=`ls -ltr /tmp | tail -n +2`[/CODE]

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().

Any ideas?
 
Old 08-31-2010, 02:58 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 08-31-2010, 03:02 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

Code:
ls -ltr /tmp | tail -n +2|awk -F: '{$1=""; print gensub(/^..../,"","1",$0) }'
This assumes that your first colon is the one in the time field.
 
Old 08-31-2010, 08:38 PM   #4
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Talking Displaying Only File and Directory Names Line By Line

Quote:
Originally Posted by crts View Post
Hi,

Code:
ls -ltr /tmp | tail -n +2|awk -F: '{$1=""; print gensub(/^..../,"","1",$0) }'
This assumes that your first colon is the one in the time field.
Hi Carts!

You have really found out or devised a good method which replaces the following simple command

Code:
ls -1 bin
cleanup.sh
exit.sh
host.sh
Note: That is digit 1 and not l after ls -


Code:
ls -ltr bin | tail -n +2|awk -F: '{$1=""; print gensub(/^..../,"","1",$0) }'
cleanup.sh
exit.sh
host.sh
Well, the awk... part is really good to understand how we can come up with different ways of doing the same thing and different things as well.

But my question is different from what you understood. The result of

ls -l is to be passed to the PHP Script which displays the information keeping it intact.

But I want to extract the file name part from every line in the PHP Script.

Let's say this is being passed to the PHP Script:

Code:
-rw-r--r-- 1 root root 14236 Aug  4 11:49 msg.txt
and it is being displayed by the PHP Script. But now I want to extract the file name part so that I can fetch its contents on demand.
 
Old 08-31-2010, 08:43 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hi_This_is_Dev View Post
...
Let's say this is being passed to the PHP Script:

Code:
-rw-r--r-- 1 root root 14236 Aug  4 11:49 msg.txt
...
Is there anything wrong in my proposal in http://www.linuxquestions.org/questi...6/#post4083928 ?
 
Old 08-31-2010, 08:50 PM   #6
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Lightbulb

Quote:
Originally Posted by Sergei Steshenko View Post
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:

Code:
<?php

	$dir = "test";

	$dh = opendir($dir);

	echo "<ul>";

	if($dh) {

		$file = readdir($dh);

       		while ($file) {

			echo "<li>";
        
			if(filetype($file) == "dir") {

				echo "<font color=RED>[DIRECTORY]</font> ";
			}	

			else {

				echo "<font color=GREEN>[FILE]</font> ";

			}
 
			echo "<a href='" . $file . "'>" . $file . "</a><br />";

			echo "</li>";

			$file = readdir($dh);
       		}

        	closedir($dh);
	}

	echo "</ul>";

?>

And here is its output:

Code:
[DIRECTORY] .

[DIRECTORY] ..

[FILE] var1.php
Nice?



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?
 
Old 08-31-2010, 09:05 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hi_This_is_Dev View Post
Well, I have not tried the "stat" function in PHP. But I have done this before:

Code:
<?php

	$dir = "test";

	$dh = opendir($dir);

	echo "<ul>";

	if($dh) {

		$file = readdir($dh);

       		while ($file) {

			echo "<li>";
        
			if(filetype($file) == "dir") {

				echo "<font color=RED>[DIRECTORY]</font> ";
			}	

			else {

				echo "<font color=GREEN>[FILE]</font> ";

			}
 
			echo "<a href='" . $file . "'>" . $file . "</a><br />";

			echo "</li>";

			$file = readdir($dh);
       		}

        	closedir($dh);
	}

	echo "</ul>";

?>

And here is its output:

Code:
[DIRECTORY] .

[DIRECTORY] ..

[FILE] var1.php
Nice?



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 ?

How many times

Code:
$file = readdir($dh);
is supposed to be executed ?
 
Old 08-31-2010, 09:16 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hi_This_is_Dev View Post
...
if(filetype($file) == "dir") {

...
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.
 
Old 08-31-2010, 09:46 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
here's an example of what you want to do in PHP
Code:
<?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.
 
Old 08-31-2010, 09:56 PM   #10
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by Sergei Steshenko View Post
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
  1. 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.
  1. 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.
 
Old 08-31-2010, 10:10 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
What did you mean writing

Code:
while ($file)
?

What kind of thing (string or array) is $file in the above context ?
that while loop should be written this way (as documented)
Code:
while (false !== ($file = readdir($handle))) {
        echo "$file\n";
}

Last edited by ghostdog74; 08-31-2010 at 10:19 PM.
 
Old 08-31-2010, 10:11 PM   #12
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 08-31-2010, 10:13 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hi_This_is_Dev View Post
$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
  1. 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.
  1. 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:

Code:
	if($dh) {

		$file = readdir($dh);

       		while ($file) {
 
Old 08-31-2010, 10:28 PM   #14
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by ghostdog74 View Post
you always advise people to RTFM, but you don't do it yourself?
that while loop can be written this way (as documented)
Code:
while (false !== ($file = readdir($handle))) {
        echo "$file\n";
}
Hi,


I saw that form of the while loop in the manual of PHP. Yes, you're right.

By the way, what is RTFM?
 
Old 08-31-2010, 10:30 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hi_This_is_Dev View Post
...
By the way, what is RTFM?
The politically correct explanation is: "Read The Fine manual".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Long file names on Linux file server jumbled when opening on 16-bit PC program. brandonhughesj General 3 03-04-2009 07:53 AM
Bind DNS for Active Directory long names don't resolve humbletech99 Linux - Networking 2 01-18-2007 05:22 AM
long listing using ls -l command display year or time of the file mnd_world Linux - Newbie 1 05-20-2004 11:39 AM
Long File Names Pwcca Linux - General 1 03-05-2003 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration