1

ls -l

-rw-r--r-- 1 angus angus    0 2013-08-16 01:33 copy.pl
-rw-r--r-- 1 angus angus 1931 2013-08-16 08:27 copy.txt
-rw-r--r-- 1 angus angus  492 2013-08-16 03:15 ex.txt
-rw-r--r-- 1 angus angus   25 2013-08-16 09:07 hello.txt
-rw-r--r-- 1 angus angus   98 2013-08-16 09:05 hi.txt

I need only the read, write , access data as well as the file name.

#! /usr/bin/perl -w
@list = `ls -l`;
$index = 0;
#print "@list\n";
for(@list){
 ($access) = split(/[\s+]/,$_);
 print "$access\n";
 ($data) = split(/pl+/,$_);
 print "$data";
 @array1 = ($data,$access);
}
print "@array1\n"

I have written this code to extract the read,write,access permission details and the file name corresponding to it. I couldn't extract the filename which is the last column.

2 Answers 2

7

Check perl stat http://perldoc.perl.org/functions/stat.html It's more robust and efficient than calling external ls command,

use File::stat;
$sb = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
       $filename, $sb->size, $sb->mode & 07777,
       scalar localtime $sb->mtime;
Sign up to request clarification or add additional context in comments.

Comments

2

I think you have an error in line number 8 of your script. You are trying to split the line using the string "pl" as a delimiter which will only match the first line of your input and will not give you what I think you want.

I believe you should just split the whole line on white space and assign just the columns you want (number 1 and 8 in this case).

change your loop for this:

for my $filename (@list){
    chomp($filename);
    my ($access, $data) = (split(/\s+/, $filename))[0, 7]; #use a slice to get only the columns you want.
    print "$access $data\n";
}

Note: mpapec suggestion to use Stat would be better. I just wanted to let you know why your code is not working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.