Tuesday, 6 December 2016

Common file operations in perl part 1

Perl empowers the user with several useful functions for interacting with and manipulating files. In this atricle I'll talk about & demonstrate file copy, move, rename, delete operations. modify file permissions/ownerships & also about the stat function using which we can get some useful information about files.

I'll not take too much time explaining the syntax & usage of each of the functions since we can understand the usage from the function name itself but I'll talk a bit about the stat function.

So, the stat function is basically a thirteen element array & the array elements store information about different attributes of the file. Here is the list of elements:

0 Device number of file system
1 Inode number
2 File mode (type and permissions)
3 Number of (hard) links to the file
4 Numeric user ID of file.s owner
5 Numeric group ID of file.s owner
6 The device identifier (special files only)
7 File size, in bytes
8 Last access time since the epoch
9 Last modify time since the epoch
10 Inode change time (not creation time!) since the epoch
11 Preferred block size for file system I/O
12 Actual number of blocks allocated

To sum up the file manipulation operations, I've written the following perl script:

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

use File::Copy;
use File::stat;

$file_name = "oldfile";
$copy = "oldfile_copy";
$second_copy = "oldfile_copy2";
$old_name = "oldfile";
$new_name = "newfile";
$new_path = "/tmp";

if ( -e $file_name )
 {
        ##copy file contents##

        copy ($file_name, $copy) || die "couldn't copy file $!";
        copy ($file_name, $second_copy) || die "couldn't copy file $!";
        copy ("$file_name", "${file_name}.bkp") || die "couldn't take a backup copy $!";

        ##copy file contents to the screen i.e STDOUT##

        copy ($file_name, \* STDOUT ) || die "couldn't print file to stdout  $!";

        ##move a file##

        move ("$old_name", "$new_path/") || die "couldn't move file $!";

        ##rename a file##

        rename ("$new_path/$old_name", "$new_path/$new_name") || die "couldn't rename file $!";

        ##list file & delete it##
        system ("ls /root/oldfile_copy2");
        unlink $second_copy ;

        ##change ownership & permissions##
        $uid = "1000" ;
        $gid = "1000" ;
        chown $uid, $gid, "$copy" ;
        chmod 0777, "$copy" ;

        system ("ls -l $copy") ;


        ##using stat function##

        $file_size = (stat ($copy))->size;
        $file_owner = (stat ($copy))->uid;

        print "file is owned by user with uid ", $file_owner, " size of the file is: ",  $file_size , " bytes \n"  ;

 }


The output of the execution of the script is as follows:

root@buntu:~# ./fileop.pl
this is a test file
/root/oldfile_copy2
-rwxrwxrwx 1 sa sa 20 Dec  6 21:52 oldfile_copy
file is owned by user with uid 1000 size of the file is: 20 bytes


This script performs the following tasks in order:
  • copies a file
  • prints the contents of the file to the screen
  • moves & renames the file
  • deletes a copy of the file
  • changes permissions of a copy of the file
  • uses stat function to get size of the file & the uid of the file owner & prints them

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...