Last updated on August 6th, 2022 at 05:07 pm

Find the size of a file using perl and display it in KB (Kilo Bytes). You need to provide the name of the file which the size needs to be checked as an argument and the script will return it size accordingly in Bytes and Kilo Byte(KB).

#!/usr/bin/perl -w

$filename = $ARGV[0];
if(@ARGV == 0)
{
print "Please pass the name of file as an argument to check its size\n";
exit;
}
$filesize = -s $filename;
if ($filesize == 0)
{
print "File Size Is Zero";
}
else
{
print "Size of the file $ARGV[0] is ".$filesize/1024 ." Bytes\n";
print "Size of the file $ARGV[0] is ".$filesize/1024 ." KB\n";
}

Output

root@production-server:~# ./filesize.pl
Please pass the name of file as an argument to check its size
root@production-server:~# ./filesize.pl  email.sh
Size of the file email.sh is 0.3056640625 Bytes
Size of the file email.sh is 0.3056640625 KB
root@production-server:~# ./filesize.pl data_2022-06-10_17_18_21.sql
Size of the file data_2022-06-10_17_18_21.sql is 345305.563476562 Bytes
Size of the file data_2022-06-10_17_18_21.sql is 345305.563476562 KB
root@production-server:~#

Leave a Reply

Your email address will not be published. Required fields are marked *