Tuesday, May 19, 2020

Globbing a Directory in Perl

Its very simple to print a list of all files in a directory using the built-in Perl glob function. Lets 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; } When you run the program, youll 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;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.