By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

formatting output of `ls -l` shell command

P: n/a
Hello.
As in the topic, I use www to execute shell command, in this case 'ls -l'
and i get output like:

total 8
drwx------ 11 lecichy staff 4096 Oct 15 18:18 Maildir
drwx---r-x 3 lecichy staff 4096 Oct 13 19:28 public_html

etc.
but what If i would want to print to www only some columns. e.g. only
filename and size like;

dir contains
Maildir size: 4096
public_html size 4096

or any other combinations, the point is if anyone has
any ideas how to split the output of this command to variables on witch i
can easily operate?

And maybe its possible to achieve it in the other way. What I mean is
selecting filenames and so on as a regular strings from a specific line of
text
?

Thanks


Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
lecichy wrote on Wednesday 15 October 2003 14:02:
Hello.
As in the topic, I use www to execute shell command, in this case 'ls -l'
and i get output like:

total 8
drwx------ 11 lecichy staff 4096 Oct 15 18:18 Maildir
drwx---r-x 3 lecichy staff 4096 Oct 13 19:28 public_html

etc.
but what If i would want to print to www only some columns.


ls -l is a system specific command. If you wanted to parse the output, you
could probably use the space as a separator between data and also taking
into account formatting options on the system.

What you should be using instead are the directory and filesystem functions
in PHP:
http://www.php.net/manual/en/ref.dir.php
http://www.php.net/manual/en/ref.filesystem.php
i.e., iterate through the directory and display data for each entry.

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 17 '05 #2

P: n/a
lecichy wrote:
'ls -l' and i get output like:

total 8
drwx------ 11 lecichy staff 4096 Oct 15 18:18 Maildir
drwx---r-x 3 lecichy staff 4096 Oct 13 19:28 public_html

etc.
but what If i would want to print to www only some columns. e.g. only
filename and size like;


instead of ls -l try

ls -l | awk '{ print($9 " size: " $5) }'
or, if you don't want that, grab the result and preg_split it twice:

the first time with "\n" (newline) for each line

and the second time (once for every line found the first time)
with "\s" (whitespace) for each column

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #3

P: n/a
instead of ls -l try

ls -l | awk '{ print($9 " size: " $5) }'
or, if you don't want that, grab the result and preg_split it twice:

the first time with "\n" (newline) for each line

and the second time (once for every line found the first time)
with "\s" (whitespace) for each column


heh! Thanks! Now it seems so obvious and so "doable by me". So many ideas!
Guess i have to lern thinking creatively, not only technics :)
But since we are here to lern something more so could you Pedro explain this
" ls -l | awk '{ print($9 " size: " $5) }' " you suggested?. Its possible
to add PHP variables inside the shell command ( this ` ` backticks or
whatever its called ) in the script ?

Jul 17 '05 #4

P: n/a
lecichy wrote:
heh! Thanks! Now it seems so obvious and so "doable by me". So many ideas!
Guess i have to lern thinking creatively, not only technics :)
you really, really, *really* should follow Zurab's suggestion of using
the filesystem functions.

But since we are here to lern something more so could you Pedro explain this
" ls -l | awk '{ print($9 " size: " $5) }' " you suggested?.
from a shell prompt execute

le*****@host.com$ man awk

"awk" is simply a system command, just like "ls"

Its possible to add PHP variables inside the shell command
( this ` ` backticks or whatever its called ) in the script ?


Yes, but that is not what I meant. I typed the command just like the
system would see it. And I didn't use backticks.

Try it at the shell prompt:

le*****@host.com$ ls -l | awk '{ print($9 " size: " $5) }'

Check the execution chapter of the PHP Manual

http://www.php.net/manual/en/ref.exec.php


But I repeat, it's better to follow Zurab's suggestion!
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #5

P: n/a
On Wed, 15 Oct 2003 21:20:29 +0000, Zurab Davitiani wrote:
ls -l is a system specific command. If you wanted to parse the output, you
could probably use the space as a separator between data and also taking
into account formatting options on the system.


NOT safe. With many file systems (including all the commong Linux
filesystems), spaces are valid in filenames. Some other systems (but NOT
linux) may even allow spaces in user/group names. (Not at all in
disagreement re: using built-in functions vs ls, but want to point out
thhe flaw in your suggested scheme)

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.