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

Reading folder content and permissions

P: 4
I want to tweak a small program I'm creating. It will read a directory and if it finds a file inside the directory with executable permissions it should do something.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $dir_to_process = "/tmp";
  4. opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
  5.  
  6. while (my $name = readdir DIR) {
  7.  
  8. next if $name =~ /^\./;
  9.  
  10.  
  11. if (-x $name)
  12. {
  13.  print "Do something";
  14. }
  15.  
  16.  
  17. }
  18.  
  19. closedir DIR;
  20.  
Jul 17 '08 #1
Share this Question
Share on Google+
10 Replies


eWish
Expert 100+
P: 971
Have a look at File::Find or File::Find::Wanted either one will make this task simple.

--Kevin
Jul 17 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
I want to tweak a small program I'm creating. It will read a directory and if it finds a file inside the directory with executable permissions it should do something.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $dir_to_process = "/tmp";
  4. opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
  5.  
  6. while (my $name = readdir DIR) {
  7.  
  8. next if $name =~ /^\./;
  9.  
  10.  
  11. if (-x $name)
  12. {
  13.  print "Do something";
  14. }
  15.  
  16.  
  17. }
  18.  
  19. closedir DIR;
  20.  

OK, is there a problem with the code you have? Does it not work? Did you look up what the -x file test operator does? You also have this question on multiple forums, check them too.
Jul 18 '08 #3

nithinpes
Expert 100+
P: 410
Absolute filename is required for a file test like -x to pass. That means you should include the complete path of the file for the test to pass, not just the filename.
Change the code to:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $dir_to_process = "/tmp";
  4. opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
  5.  
  6. while (my $name = readdir DIR) {
  7. next if $name =~ /^\./;
  8. $name = $dir_to_process.'/'.$name;  ##append the directory path 
  9. if (-x $name)
  10. {
  11.  print "Do something";
  12. }
  13.  
  14. }
  15.  
  16. closedir DIR;
  17.  
- Nithin
Jul 18 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
Absolute filename is required for a file test like -x to pass
I suggested that on another forum where he posted this same question. The file test operator will work if the script is in the directory where the files are. If not he has to use the full path or chdir() into the directory where the files are.
Jul 18 '08 #5

P: 4
I uncluded the line $filename = "/tmp/".$name."\n"; to make the path absolute and is not detecting that is executable.

The -x should find out if the file is executable then it will perform the action inside the the braces?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $dir_to_process = "/tmp";
  4. opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
  5.  
  6. while (my $name = readdir DIR) {
  7. next if $name =~ /^\./;
  8.  
  9. $filename =  "/tmp/".$name."\n";
  10.  
  11. if (-x $filename)
  12.  
  13. {
  14.  print "Do something";
  15. }
  16.  
  17. }
  18.  
Jul 18 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
I uncluded the line $filename = "/tmp/".$name."\n"; to make the path absolute and is not detecting that is executable.

The -x should find out if the file is executable then it will perform the action inside the the braces?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $dir_to_process = "/tmp";
  4. opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
  5.  
  6. while (my $name = readdir DIR) {
  7. next if $name =~ /^\./;
  8.  
  9. $filename =  "/tmp/".$name."\n";
  10.  
  11. if (-x $filename)
  12.  
  13. {
  14.  print "Do something";
  15. }
  16.  
  17. }
  18.  
Why are you putting the "\n" newline in the path? In this context I believe it literally means "\n" and not a newline. You should remove it as you are setting the value of the variable $filename, not printing its value.

Regards,

Jeff
Jul 18 '08 #7

P: 4
The line print "Do something"; now prints 3 times but of the 3 files inside /tmp onle 1 of them is executable.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $dir_to_process = "/tmp";
  4. opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
  5.  
  6. while (my $name = readdir DIR) {
  7. next if $name =~ /^\./;
  8.  
  9. $filename =  "/tmp/".$name;
  10.  
  11. if (-x $filename) {
  12.  
  13. print "Do something";
  14.  
  15. }
  16.  
  17. }
  18.  
  19. closedir DIR;
  20.  
Jul 18 '08 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
The line print "Do something"; now prints 3 times but of the 3 files inside /tmp onle 1 of them is executable.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $dir_to_process = "/tmp";
  4. opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
  5.  
  6. while (my $name = readdir DIR) {
  7. next if $name =~ /^\./;
  8.  
  9. $filename =  "/tmp/".$name;
  10.  
  11. if (-x $filename) {
  12.  
  13. print "Do something";
  14.  
  15. }
  16.  
  17. }
  18.  
  19. closedir DIR;
  20.  
Ah, interesting I haven't had anything to do the the -x test operator so I am guessing it was needed. Sorry about that.

Regards,

Jeff
Jul 18 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
I posted several questions for you to answer but you either ignored them or felt they were not worth answering for some reason. No sense in me trying to help if the questions I post are going to be ignored.
Jul 18 '08 #10

P: 4
I had to do it this because that test condition using the -x was not working for me.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my $dir_to_process = "/tmp";
  3. my @files = split("\n", `find $dir_to_process -type f -perm -o+rx`);
  4. foreach my $filename (@files) {
  5. print "Do something on ($filename)\n";
  6. }
Jul 18 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.