How to find file size using perl

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

Click to rate this tutorial!
[Total: 0 Average: 0]

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

[email protected]:~# ./filesize.pl
Please pass the name of file as an argument to check its size
[email protected]:~# ./filesize.pl  email.sh
Size of the file email.sh is 0.3056640625 Bytes
Size of the file email.sh is 0.3056640625 KB
[email protected]:~# ./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
[email protected]:~#
Click to rate this tutorial!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published.