Saturday, 10 December 2016

Common directory operations in perl


This article is about common directory related operations that we'd like to perform in perl. This comprises of creating a directory, creating a directory path similar to mkdir -p, opening directories & deleting directories. I'll conclude the article with a mention of the find file function which is somewhat the find equivallent of the unix find command.

Like earlier articles this article will also illustrate the directory related functions available in perl via a single script which is as follows:

root@buntu:~# cat dirop.pl
#!/usr/bin/perl

use File::Path;
use File::Spec;

my $mydir = "/root" ;

##open a directory##

opendir (dirh, "$mydir") || die "couldn't open directory" ;

my @rdir = readdir(dirh) ;

foreach (@rdir) { print "$_ \n" ; }
closedir (dirh) ;

#my @sdir = `ls -la /root` ;
#foreach (@sdir) {
#my @rd =  split /\s+/ ;
#print "$rd[8] \n" ;
#}

##directory creation ##

my $newdir = "/test" ;
my $testdir = "/test1" ;

mkdir $newdir || die "couldn't create directory" ;

mkdir $testdir || die " couldn't create directory" ;

system ("ls -ld $testdir") ;

rmdir $testdir ;

mkpath ('/test/abc/def/ghi');

mkpath ('/test1/abc/def/ghi') ;

system ("ls -lR $testdir") ;

##directory deletion example similar to rm -r in UNIX##

rmtree ('/test1/abc/def/ghi') ;

##change a directory in a running script##

chdir "/tmp" ;

system ("pwd") ;


The execution of this code yields the following output:

root@buntu:~# ./dirop.pl
file2op.pl
hostlist
.
oldfile_copy
test4.pl
.viminfo
test9.pl
.bash_history
.lesshst
result
test5.pl
.bashrc
.cpan
oldfile.bkp
afile
dirop.pl
test3.pl
.nano
test6.pl
..
.profile
find.pl
test2.pl
outfile.txt
newfile
.cpanm
abc.pl
select.sh
test.pl
passwd
.libnet-openssh-perl
testfile
map.pl
testfile_soft
.ssh
testfile_hard
fileop.pl
test7.pl
drwxr-xr-x 3 root root 4096 Dec 10 12:57 /test1
/test1:
total 4
drwxr-xr-x 3 root root 4096 Dec 10 12:57 abc

/test1/abc:
total 4
drwxr-xr-x 3 root root 4096 Dec 10 22:10 def

/test1/abc/def:
total 4
drwxr-xr-x 2 root root 4096 Dec 10 22:10 ghi

/test1/abc/def/ghi:
total 0
/tmp
root@buntu:~#


Last but not the least I'll talk about the find function. We can use the find function in perl to seacrh for files/directories based on certain attributes such as modified date, size & also regular expression matches.

The below example script searches for any files ending with .txt or .sh in the /root directory which have been modified less than two days ago.

root@buntu:~# cat find.pl
#!/usr/bin/perl -w

use File::Find;

my @files;
my @dirpath=qw(/root);
find(sub {
           push @files,$File::Find::name if (-f $File::Find::name and /\.txt|sh$/ and int(-M $_) < 4);
      }, @dirpath);


print join "\n",@files, "\n";

The following is the output of the script:

root@buntu:~# ./find.pl
/root/select.sh
/root/.cpan/sources/authors/01mailrc.txt.gz
/root/.cpan/sources/modules/02packages.details.txt.gz

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...