Page 1 of 1

Extracting File Names from Long Listing of Directory in UNIX

Posted: Tue Aug 31, 2010 2:26 pm
by devarishi
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: Select all

contents=`ls -ltr /tmp | tail -n +2`
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: Select all

-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
It is also important that file / directory name may contain blank spaces:

Code: Select all

-rw-r--r--  1 root root 1963 Aug 31 20:30 2010 Programme
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?

Re: Extracting File Names from Long Listing of Directory in

Posted: Tue Aug 31, 2010 2:55 pm
by AbraCadaver
If all you need is the filenames in PHP, why not use the PHP glob() function?

Or to use ls for the sorting, use -1 instead of -l (number one not lowercase L).

Re: Extracting File Names from Long Listing of Directory in

Posted: Tue Aug 31, 2010 9:17 pm
by devarishi
AbraCadaver wrote:If all you need is the filenames in PHP, why not use the PHP glob() function?

Or to use ls for the sorting, use -1 instead of -l (number one not lowercase L).
Well, I know them and have used.

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?


Check out this script:

Code: Select all

<?php


	echo "<table border=1 cellpadding=10>";

	echo "<tr><th>File Name</th><th>File Type</th><th>File Size</th></tr>";

	foreach (glob("*.*") as $filename) {

		echo "<tr>";

			echo "<td><a href='" . $filename . "'>" . $filename . "</td>";
			echo "<td>" . filetype($filename) . "</td>";
			echo "<td>" . filesize($filename) . "</td>";

		echo "</tr>";
	}


	echo "</table>";
?> 

Here's the output:


Code: Select all

File Name	File Type	File Size
CV.pdf	file	46981
array.php	file	184
const.php	file	371
db.php	file	404
dir.php	file	963

Clicking any file name will display its contents.


Well, that is fine to a great degree. But I want to experiment more. :D

The kind of output which is produced by:

Code: Select all

ls -l

-rwxr-xr-x 1 root root 128 Aug 25 09:19 cleanup.sh
-rwxr-xr-x 1 root root   7 Aug 29 04:51 exit.sh
-rwxr-xr-x 1 root root 107 Sep  1 07:28 glob.php
is far more appealing as we know what permissions are set for a particular file. I am not sure of what function in PHP can return that information. But I would still want to handle the one-line-of-output and extract the-desired-bit-of-information :mrgreen: in PHP.

Re: Extracting File Names from Long Listing of Directory in

Posted: Thu Sep 02, 2010 3:16 pm
by devarishi
Somebody has helped me solve the problem. The solution can be found here:

viewtopic.php?f=1&t=120655&p=622511#p622511

Thanks to all of you, by the way!