glob

Perl - How to process every file in a directory that matches a pattern

Perl FAQ: "How can I process every file in a directory that matches a certain filename pattern?"

There are several ways to do this, but I normally use the glob function, because I can remember the syntax pretty easily.

Let's look at a couple of glob examples.

Using the glob operator

You can use the glob operator to get a list of all files in the current directory like this:

Perl - How to process every file in a directory

A common Perl FAQ is "How do I do (something) for every file in a directory?"

Here's some sample code that will show you how to loop through each file in a directory:

Ruby “glob”: How to process each file in a directory that matches a certain pattern

Here's some sample Ruby source code that shows how to do something with every file in a directory, where you only work on filenames that match a pattern you're interested in. For example, in my case I'm only interested in processing files that end with the filename extension WMA, so this first snippet of Ruby code shows how to print out the name of each file in a directory with the WMA extension:

Ruby glob example - preview many image by creating a single HTML file

Every once in a while someone will give me a large collection of images (JPG, PNG, GIF) that I need to either look at or print. On the Mac (and recent versions of Windows) there are now tools to look at a group of images, but I still like a little program that I wrote.

I wrote this program in Ruby, and what it lets you do is combine all of your pictures into one web page. Then, with the use of CSS, the HTML document you create lets you print each image on a separate page.

Perl directory list - List all files that match a filename pattern

Summary: A quick Perl tip on how to list all files in a directory that match a given filename pattern, i.e., using the Perl filename "glob" pattern-matching syntax.

As a quick tip today, here's some sample Perl code that prints a listing of every file in the current directory that ends with the filename extension .html: