File test operator
You can use the file test operator to look up various information about a file.
- e Check if the file exists
You can check if the file exists with "-e".
-e $file
Returns a true value if the file exists, a false value if it does not exist. Directories and symbolic links are also treated as files. This is because on Unix, directories are also considered as special files and are considered as files.
I have put up a link to the explanation page of terms.
- f Check if the normal file exists
You can check if the normal file exists with "-f".
-f $file
Normal Returns a true value if the file exists, a false value if it does not exist. A normal file is a file that stores data such as a text file or a binary file.
- d Check if directory exists
You can check if the directory exists with "-d".
-d $file
Returns a true value if the directory exists, a false value if it does not exist.
- s Get file size
You can get the file size with "-s".
-s $file
The unit is bytes.
- M Get the number of days that have passed since the last update
You can get the number of days that have passed since the last update with "-M".
-M $file
- A Get the number of days that have passed since the last access
You can get the number of days that have passed since the last access with "-A".
-A $file
Example program
This is an example file test operator.
Confirmation of existence of normal file
This is an example to check the existence of a normal file using the file operator -f.
use strict; use warnings; # Check the existence of normal files. # -f file name print "1: Check the existence of normal files. -f\n"; my $file = 'a.txt'; if (-f $file) { print "'$file' exists.\n"; } else {print "'$file' does not exist.\n"}
Directory existence check
This is an example to check the existence of a directory using the file operator -d.
use strict; use warnings; # Check for the existence of the directory. # -d directory name print "1: Check the existence of the directory. -D\n"; my $dir = 'd'; if (-d $dir) { print "'$dir' exists.\n"; } else {print "'$dir' does not exist.\n"}
File existence check
This is an example to check the existence of a file using the file test operator "-e".
use strict; use warnings; # Check for the existence of the file. # -e file name # -e can be used for directories, files, symbolic links, etc. # You can check if it exists without distinguishing the . print "1: Check the existence of the file. -e\n"; my $file_all_type = 'a'; if (-e $file_all_type) { print "'$file_all_type' exists.\n"; } else {print "'$file_all_type' does not exist.\n"}
Get file size
This is an example to get the file size using the file operator -s.
use strict; use warnings; # Get the size of the file. # -s filename # Units are bytes. print "1: Get the size of the file. -S\n"; my $file = "a.txt"; if (-f $file) { my $file_size = -s $file; print "The file size of $file is $file_size bytes.\n\n"; } else {print "$file did not exist.\n\n"}
use strict; use warnings; print "2: Stop printing when the file size exceeds a certain byte.\n"; my $file = "output_$$. txt"; if (-e $file) { die "$file exists.\n"; } open my $fh, ">", $file or die "File open error:$!"; while (-s $file < 1_000_000) { my $string = ('a' x 99) . "\n"; print $fh $string; } print "The file size of $file after output is". -S $file. "Bytes.\n"; close $fh;
I am using while statement to output until the file size is 1,000,000 bytes.
Get the number of days that have passed since the last update
This is an example that uses the file operator -M to get the number of days that have passed since the last update.
use strict; use warnings; # Get the number of days that have passed since the last update. # -M $file print "1: Get the number of days that have passed since the last update.\n"; my $file = "a.txt"; if (-e $file) { my $from_last_modify = -M $file; print "$from_last_modify days have passed since the last update.\n"; } else {print "$file does not exist.\n"}
Get the number of days that have passed since the last access
This is an example that uses the file operator -A to get the number of days that have passed since the last access.
use strict; use warnings; # Get the number of days that have passed since the last access (read). # -A $file print "1: Get the number of days that have passed since the last access.\n"; my $file = "a.txt"; if (-e $file) { my $from_last_access = -A $file; print "$from_last_access days have passed since the last access.\n"; } else {print "$file does not exist.\n"}