home PYTHONJAVA
 

Perl directory operation

The following is a list of standard functions for some of the action directories:

opendir DIRHANDLE, EXPR # Open Directory
Readdir DIRHANDLE # Read Directory
Rewinddir DIRHANDLE # Position the pointer to the beginning
Telldir DIRHANDLE # Back to the current location of the directory
Seekdir DIRHANDLE, POS # locate the POS location assigned to the directory
Closedir DIRHANDLE # Close directory

Show all files

Show all the files in the directory. The following example uses the glob operator, which is demonstrated as follows:

Show all files

Show all the files in the directory. The following example uses the glob operator, which is demonstrated as follows:

Instance

#!/usr/bin/perl # Display all files in the /tmp directory $dir= " /tmp/*"; my @files = glob( $dir ); foreach (@files ){ print $_ . "\n"; } # Display all files ending in .c in the /tmp directory $dir= " /tmp/*.c"; @files = glob ( $dir ); foreach (@files ){ print $_ . "\n"; } # Show all hidden files $dir= " /tmp/.*"; @files = glob ( $dir ); foreach (@files ){ print $_ . "\n"; } # Display all files in the /tmp and /home directories $dir= " /tmp/* /home/*"; @files = glob ( $dir ); foreach (@files ){ print $_ . "\n"; }

The following example lists all the files in the current directory:

Instance

#!/usr/bin/perl opendir (DIR, '.') or die "Unable to open the directory, $!"; while ($file = readdir DIR) { print "$file\n"; } closedir DIR;

If you want to display all files ending in .c in the /tmp directory, you can use the following code:

Instance

#!/usr/bin/perl opendir(DIR, '.') or die "Unable to open the directory, $!"; foreach (sort grep(/^.*\.c$/,readdir(DIR ))){ print "$_\n"; } closedir DIR;

Create a new directory

We can use the mkdir function to create a new directory, you need to have sufficient permissions to create the directory before execution:

Instance

#!/usr/bin/perl $dir= " /tmp/perl"; # Create a perl directory under the /tmp directory mkdir( $dir ) or die "Unable to create$dir directory, $!"; print "The directory was created successfully\n";

Delete directory

We can use the rmdir function to delete a directory, and we need to have sufficient permissions to perform this operation. In addition, the directory to be deleted must have an empty directory:

Instance

#!/usr/bin/perl $dir= " /tmp/perl"; # Remove the perl directory under the /tmp directory rmdir( $dir ) or die "Unable to delete$dir directory, $!"; print "Directory deletion succeeded\n";

Switch directory

We can use the chdir function to switch the current directory, which requires sufficient permissions to perform this operation. An example is as follows:

Instance

#!/usr/bin/perl $dir= " /home"; # Move the current directory to the /home directory chdir( $dir ) or die "Unable to switch directory to$dir , $!"; print "The directory you are currently in is $dir\n ";

Execute the above program, the output is:

The directory you are currently in is  /home





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.