Science, Tech, Math › Computer Science Globbing a Directory Read a directory in Perl Print Dominik Pabis/E+/Getty Images Computer Science Perl PHP Programming Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Kirk Brown Kirk Brown Computer Science Expert B.A., Abilene Christian University Kirk Brown is a systems software engineer with expertise in Perl, PHP, Ruby, and Python. Learn about our Editorial Process Updated on March 31, 2019 It's very simple to print a list of all files in a directory using the built-in Perl glob function. Let's look over a short script that globs and prints a list of all files, in the directory containing the script itself. Examples of the Perl Glob Function #!/usr/bin/perl -w @files = <*>; foreach $file (@files) { print $file . "\n"; } Read More How to Tell a File From a Directory in Perl By Kirk Brown When you run the program, you'll see it output the filenames of all files in the directory, one per line. The glob is happening on the first line, as the <*> characters pulls the filenames into the @files array. @files = <*>; Then you simply use a foreach loop to print out the files in the array. You can include any path in your filesystem between the <> marks. For example, say your website is in the /var/www/htdocs/ directory and you want a list of all the files: @files = </var/www/htdocs/*>; Or if you just want a list of the files with the extension .html: @files = </var/www/htdocs/*.html>; Cite this Article Format mla apa chicago Your Citation Brown, Kirk. "Globbing a Directory." ThoughtCo, Jul. 31, 2021, thoughtco.com/globbing-a-directory-2641092. Brown, Kirk. (2021, July 31). Globbing a Directory. Retrieved from https://www.thoughtco.com/globbing-a-directory-2641092 Brown, Kirk. "Globbing a Directory." ThoughtCo. https://www.thoughtco.com/globbing-a-directory-2641092 (accessed April 26, 2024). copy citation