Jump to content

Optimize Directory listing regex


jay3ld

Recommended Posts

Hello,

 

I am creating a regular expression to match the output from the command "ls -go". I only really want the permissions and the file name.

-rw-r--rw-  1      323 Nov  3 07:28 index.php

 

I came up with this that works:

~([-d][-r][-w][-x][-r][-w][-x][-r][-w][-x])+\s+\d+\s+\d+\s+\d+\s+\d+\s+[A-Za-z]+\s+\d+\s+[\d:\d]+\s([A-Za-z0-9_\-]+.[A-Za-z]{3})+~mis

 

I optimized it down to this:

~([-drwx]{10})+[\s\d]+[A-Za-z]+[\s\d:]+([A-Za-z0-9_\-.]+.[A-Za-z]{3})+~mis

 

Any ideas on how i can optimize it any more than this?

Did I miss something that my testing didn't see that could cause this to miss a true result?

Link to comment
Share on other sites

A quick and dirty way could include:

 

$str = '-rw-r--rw-  1      323 Nov  3 07:28 index.php';
preg_match('#^([^ ]+).+?([^ ]+\..+)$#', $str, $match);
echo $match[1] . ' ' . $match[2]; // outputs: -rw-r--rw- index.php

 

But I suppose this largely depends on the nature of the string. Is the string in question by itself?

Link to comment
Share on other sites

nrg_alpha,

No the string itself isn't in question. The output should always be similar to that.

 

ghostdog74,

The reason why I can't use those is that this information comes from a FTP connection (via a socket). I didn't see any information in FTP functions on getting the file permissions.

 

Daniel0,

Would those work with a remote socket connection to a FTP server? I have done a fsockopen to a FTP server, been able to authenticate, list files, change their permissions, etc. But I haven't found an easy way to just obtain the information I want (Their permissions)

Link to comment
Share on other sites

ghostdog74,

The reason why I can't use those is that this information comes from a FTP connection (via a socket). I didn't see any information in FTP functions on getting the file permissions.

ok , so you are listing files from inside an FTP session? PHP has almost any kind of libraries you need to make life easy for you. check out the FTP module. you can use ftp_nlist() function to list files from remote server, then get the results and manipulate them as you wish.

Link to comment
Share on other sites

ghostdog74,

The reason why I can't use those is that this information comes from a FTP connection (via a socket). I didn't see any information in FTP functions on getting the file permissions.

ok , so you are listing files from inside an FTP session? PHP has almost any kind of libraries you need to make life easy for you. check out the FTP module. you can use ftp_nlist() function to list files from remote server, then get the results and manipulate them as you wish.

 

Correct, but it doesn't provide you with any other information that is useful such as permissions, ownership, modified time, etc. That is why I am doing it this way.

 

This script isn't something I intend to release. It is being used on remote servers I have. The script has to be via FTP, as apache on that server doesn't run with enough permissions to the files/folders to to chmod.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.