Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a list of all files with a certain extension from a specific directory?

Tags:

directory

perl

I'm using this code to get a list of all the files in a specific directory:

opendir DIR, $dir or die "cannot open dir $dir: $!";
my @files= readdir DIR;
closedir DIR;

How can I modify this code or append something to it so that it only looks for text files and only loads the array with the prefix of the filename?

Example directory contents:

.
..
923847.txt
98398523.txt
198.txt
deisi.jpg
oisoifs.gif
lksdjl.exe

Example array contents:

files[0]=923847 
files[1]=98398523
files[2]=198
like image 312
CheeseConQueso Avatar asked Oct 02 '09 15:10

CheeseConQueso


People also ask

How do I search for all files with specific extensions?

For finding a specific file type, simply use the 'type:' command, followed by the file extension. For example, you can find . docx files by searching 'type: . docx'.


1 Answers

my @files = glob "$dir/*.txt";
for (0..$#files){
  $files[$_] =~ s/\.txt$//;
}
like image 131
Wooble Avatar answered Oct 05 '22 22:10

Wooble