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? |
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.
|
Hi,
Code:
ls -ltr /tmp | tail -n +2|awk -F: '{$1=""; print gensub(/^..../,"","1",$0) }' |
Displaying Only File and Directory Names Line By Line
Quote:
You have really found out or devised a good method which replaces the following simple command Code:
ls -1 bin Code:
ls -ltr bin | tail -n +2|awk -F: '{$1=""; print gensub(/^..../,"","1",$0) }' 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 |
Quote:
|
Quote:
Well, I have not tried the "stat" function in PHP. But I have done this before: Code:
<?php And here is its output: Code:
[DIRECTORY] . 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? :) |
Quote:
Code:
while ($file) What kind of thing (string or array) is $file in the above context ? How many times Code:
$file = readdir($dh); |
Quote:
I am not a PHP guy; in Perl it is Code:
my $file_prefixed_with_dir = "$dir/file"; |
here's an example of what you want to do in PHP
Code:
<?php |
Quote:
$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.
Code:
readdir — Read entry from directory handle $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. |
Quote:
Code:
while (false !== ($file = readdir($handle))) { |
Quote:
First, thanks for the Perl version. :) The statement below: PHP Code:
I tested it with: PHP Code:
This statement creates a pointer or handler to the specified directory that we are going to work with: PHP Code:
PHP Code:
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. ;) |
Quote:
Code:
if($dh) { |
Quote:
I saw that form of the while loop in the manual of PHP. Yes, you're right. By the way, what is RTFM? :) |
Quote:
|
All times are GMT -5. The time now is 06:24 AM. |