Connecting Tech Pros Worldwide Forums | Help | Site Map

formatting output of `ls -l` shell command

lecichy
Guest
 
Posts: n/a
#1: Jul 17 '05
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





Zurab Davitiani
Guest
 
Posts: n/a
#2: Jul 17 '05

re: formatting output of `ls -l` shell command


lecichy wrote on Wednesday 15 October 2003 14:02:
[color=blue]
> 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.[/color]

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/
Pedro
Guest
 
Posts: n/a
#3: Jul 17 '05

re: formatting output of `ls -l` shell command


lecichy wrote:[color=blue]
> '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;[/color]

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.
lecichy
Guest
 
Posts: n/a
#4: Jul 17 '05

re: formatting output of `ls -l` shell command


[color=blue]
> 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[/color]

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 ?



Pedro
Guest
 
Posts: n/a
#5: Jul 17 '05

re: formatting output of `ls -l` shell command


lecichy wrote:[color=blue]
> 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 :)[/color]

you really, really, *really* should follow Zurab's suggestion of using
the filesystem functions.

[color=blue]
> But since we are here to lern something more so could you Pedro explain this
> " ls -l | awk '{ print($9 " size: " $5) }' " you suggested?.[/color]

from a shell prompt execute

lecichy@host.com$ man awk

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

[color=blue]
> Its possible to add PHP variables inside the shell command
> ( this ` ` backticks or whatever its called ) in the script ?[/color]

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:

lecichy@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.
Tyler Eaves
Guest
 
Posts: n/a
#6: Jul 17 '05

re: formatting output of `ls -l` shell command


On Wed, 15 Oct 2003 21:20:29 +0000, Zurab Davitiani wrote:
[color=blue]
> 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.[/color]

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)

Closed Thread