Perl list file


 
Thread Tools Search this Thread
Top Forums Programming Perl list file
# 1  
Old 02-27-2013
Perl list file

Hi all

I am looking for a way to list files in a directoy using perl but then exlude some of the results

i've got this to list the files, but how can exclude some of the results

Code:
#!/usr/bin/perl
$d = "/data";
opendir(DIR, $d);
foreach my $file (readdir(DIR))
    {
    print "Version: $file\n";
    }
closedir(DIR);

# 2  
Old 02-27-2013
Explicitly: what exact file names do you want to exclude?

Generally: you can use a regex to filter in the good ones or filter out the bad ones.
(this answer is not much help because there are no details in your question)
# 3  
Old 02-28-2013
So the code will show me Eveything directories as well. I want to exclude the directories called ( see below) from appearing.

Music
Old
OLD
3d
.
..
# 4  
Old 02-28-2013
Quote:
Originally Posted by ab52
Hi all

I am looking for a way to list files in a directoy using perl but then exlude some of the results

i've got this to list the files, but how can exclude some of the results

Code:
#!/usr/bin/perl
$d = "/data";
opendir(DIR, $d);
foreach my $file (readdir(DIR))
    {
      if(-f "$d/$file") {
       print "Version: $file\n";    
      }
    }
closedir(DIR);


You can filter by checking that the input should be file.

Cheers,
RangaSmilie
# 5  
Old 02-28-2013
Sorry i was not clear, i want it to so me the directories as well ( i,e everything in /data) but i want to exclude some of the results
# 6  
Old 02-28-2013
Quote:
Originally Posted by ab52
Sorry i was not clear, i want it to so me the directories as well ( i,e everything in /data) but i want to exclude some of the results
Maybe something like this?

Code:
#!/usr/bin/perl
%exclude = map {$_ => 1} qw(Music Old OLD 3d);
$d = "/data";
opendir(DIR, $d);
foreach my $file (readdir(DIR)) {
  print "Version: $file\n" if not defined $exclude{$file};
}
closedir(DIR);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl list of users

Hi All i need to write something in perl that on a mac list all the users accounts, and then carries out a copy, ( rsync ) on each one it finds. i was going to use glob, but i want to exclude a certain result here is what i have use Getopt::Std; use POSIX qw(tmpnam); use lib... (1 Reply)
Discussion started by: ab52
1 Replies

2. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

3. Shell Programming and Scripting

Using perl to grep a list of patterns from an input file

I have been struggling to grep a file of NGrams (basically clusters of consonants or Consonant and Vowel) acting as a pattern file from an Input file which contains a long list of words, one word per line. The script would do two things: Firstly read a text pattern from a large file of such... (5 Replies)
Discussion started by: gimley
5 Replies

4. Shell Programming and Scripting

Perl Directory List Help

I am currently trying to find all files with extensions .gif .jpg .png .wav and .au in a current directory and count them. I am trying to count them based on there extension only. #!/usr/bin/perl $gif =`ls *.gif | uniq -cf 1`; $jpg =`ls *.jpg | uniq -cf 1`; $png =`ls *.png | uniq -cf 1`;... (2 Replies)
Discussion started by: Temphas
2 Replies

5. Shell Programming and Scripting

Perl: Sorting a hash value that is a list.

Hi Folks I am very much a newbie at perl but picking it up and I'm hoping you can help. I have a file input that details all the /etc/group files in our enterprise in the following format: "<host>:<group>:<gid>:<users>" I want to parse this data display it as the following:... (9 Replies)
Discussion started by: g_string
9 Replies

6. Shell Programming and Scripting

List all file that match user input perl

Hi, I want to list all file that match user input ( specified shell wildcard) but when I compile it dont list me #!/usr/bin/perl -w print "Enter Advance Search Function: "; chomp ($func = <STDIN>); my @files = glob("$func"); foreach my $file (@files) { print "$file\n";... (1 Reply)
Discussion started by: guidely
1 Replies

7. Shell Programming and Scripting

List of directories with a particular name using perl

Hi experts, Sorry if it is repeated query. I have folder that contains many files & folders. But I need to get the list of folders with the following format "1.0.0.0.003X", 1.0.0.0.004X..and so on. How do I get the same using perl? I have this script which will do the listing. $dir... (1 Reply)
Discussion started by: amvarma77
1 Replies

8. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

9. Shell Programming and Scripting

Convert perl qw list to text file list?

Does anyone have a good example? I am having trouble looping.. Thanks (1 Reply)
Discussion started by: mrlayance
1 Replies

10. UNIX for Dummies Questions & Answers

List of all PERL scripts.

Which command should I use to get the list of all the .pl scripts on the unix server... Thanks in advacne, Venky (3 Replies)
Discussion started by: venkyA
3 Replies
Login or Register to Ask a Question