- Perl - Sending Email
- Perl - Regular Expressions
- Perl - Coding Standard
- Perl - Special Variables
- Perl - Error Handling
- Perl - Directories
- Perl - File I/O
- Perl - Formats
- Perl - References
- Perl - Subroutines
- Perl - Date & Time
- Perl - Operators
- Perl - Loops
- Perl - IF...ELSE
- Perl - Hashes
- Perl - Arrays
- Perl - Scalars
- Perl - Variables
- Perl - Data Types
- Perl - Syntax Overview
- Perl - Environment
- Perl - Introduction
- Perl - Home
Perl Advanced
- Perl - Functions References
- Perl - Embedded Documentation
- Perl - Process Management
- Perl - Packages & Modules
- Perl - CGI Programming
- Perl - Database Access
- Perl - Object Oriented
- Perl - Socket Programming
Perl Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Perl - Directories
Following are the standard functions used to play with directories.
opendir DIRHANDLE, EXPR # To open a directory readdir DIRHANDLE # To read a directory rewinddir DIRHANDLE # Positioning pointer to the begining telldir DIRHANDLE # Returns current position of the dir seekdir DIRHANDLE, POS # Pointing pointer to POS inside dir closedir DIRHANDLE # Closing a directory.
Display all the Files
There are various ways to pst down all the files available in a particular directory. First let s use the simple way to get and pst 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 $_ . " "; } # Display all the C source files in /tmp directory. $dir = "/tmp/*.c"; @files = glob( $dir ); foreach (@files ) { print $_ . " "; } # Display all the hidden files. $dir = "/tmp/.*"; @files = glob( $dir ); foreach (@files ) { print $_ . " "; } # Display all the files from /tmp and /home directories. $dir = "/tmp/* /home/*"; @files = glob( $dir ); foreach (@files ) { print $_ . " "; }
Here is another example, which opens a directory and pst 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 "; } closedir DIR;
One more example to print the pst 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 "$_ "; } closedir DIR;
Create new Directory
You can use mkdir function to create a new directory. You will need to have the required permission to create a directory.
#!/usr/bin/perl $dir = "/tmp/perl"; # This creates perl directory in /tmp directory. mkdir( $dir ) or die "Couldn t create $dir directory, $!"; print "Directory created successfully ";
Remove a directory
You can use rmdir function to remove a directory. You will need to have the required permission to remove a directory. Additionally this directory should be empty before you try to remove it.
#!/usr/bin/perl $dir = "/tmp/perl"; # This removes perl directory from /tmp directory. rmdir( $dir ) or die "Couldn t remove $dir directory, $!"; print "Directory removed successfully ";
Change a Directory
You can use chdir function to change a directory and go to a new location. You will need to have the required permission to change a directory and go inside the new directory.
#!/usr/bin/perl $dir = "/home"; # This changes perl directory and moves you inside /home directory. chdir( $dir ) or die "Couldn t go inside $dir directory, $!"; print "Your new location is $dir ";Advertisements