- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display all the Files in Perl
There are various ways to list down all the files available in a particular directory using Perl. First let's use the simple way to get and list down all the files using the glob operator −
#!/usr/bin/perl # Display all the files in /tmp directory. $dir = "/tmp/*"; my @files = glob( $dir ); foreach (@files ) { print $_ . "\n"; } # Display all the C source files in /tmp directory. $dir = "/tmp/*.c"; @files = glob( $dir ); foreach (@files ) { print $_ . "\n"; } # Display all the hidden files. $dir = "/tmp/.*"; @files = glob( $dir ); foreach (@files ) { print $_ . "\n"; } # Display all the files from /tmp and /home directories. $dir = "/tmp/* /home/*"; @files = glob( $dir ); foreach (@files ) { print $_ . "\n"; }
Here is another example, which opens a directory and list out all the files available inside this directory.
#!/usr/bin/perl opendir (DIR, '.') or die "Couldn't open directory, $!"; while ($file = readdir DIR) { print "$file\n"; } closedir DIR;
One more example to print the list of C source files you might use is −
#!/usr/bin/perl opendir(DIR, '.') or die "Couldn't open directory, $!"; foreach (sort grep(/^.*\.c$/,readdir(DIR))) { print "$_\n"; } closedir DIR;
- Related Articles
- Opening and Closing Files in Perl
- Reading and Writing Files in Perl
- Copy, Rename and Delete Files in Perl
- How to display files/folders including hidden files/folders in PowerShell?
- Java program to delete all the files in a directory recursively (only files)
- Move All Files Including Hidden Files Into Parent Directory in Linux
- How to enable the display of hidden files in a JFileChooser in Java?
- How to get all the files, sub files and their size inside a directory in C#?
- Display all deadlock logs in MySQL?
- How to close all the opened files using Python?
- How to touch all the files recursively using Python?
- Recursively List All Files in a Directory Including Symlinks
- How to Remove All Directories and Files in Golang?
- The ? : Operator in Perl
- Python - Read all CSV files in a folder in Pandas?

Advertisements