Perl Study notes--File test

Source: Internet
Author: User
Tags bit set

The file test is used primarily to view information such as the existence of a file, file size, file update time, and so on.

File Test Operators

Whether the-e test file exists;

Die "oops! A file called ' $filename ' already exists.\n "if-e $filename;

-m test files maintain continuous updates, such as testing whether a program's log files remain updated every week or two weeks.

The file handle is tested here.

Warn "Config file is looking pretty old!\n" if-m config>28;

-s the size of the test file, in bytes to calculate the file size.

Example: The following test is a file that is larger than 100 bytes in size and the update date is less than two days under this folder.

#!/usr/bin/env Perl
#filename: test2.pl Use 5.010; while(<>){ if(/\a-.*/){ my @line=Split/\s+/,"$&";#say "$line [8]";Say"$line [8]\tis bigger than 100bytes." if-S$line[8] > -And-m$line[8] >2; }}
Run command: ls-l | ./<filename>

How to view the size of a file in M: My size= (-s $filename)/1024;

Be sure to add parentheses.

Other file test items

-r: File or directory for this (valid) User (effective User) or group is readable-w: file or directory on this (valid) user or group is writable by-x: File or directory on this (valid) user or group is executable-o: File or directory owned by this (valid) user All-r: file or directory to this user (real user) Or a group is readable-W: A file or directory to which this user or group is writable-x: A file or directory to which this user or group is executable-o: File or directory owned by this user-e: File or directory name exists-Z: file exists, size is 0 (directory constant is False)-S: File or directory exists, Size greater than 0 (value is file size, Unit: bytes)-F: For plain text-D: For directory-L: For Symbolic Link-s: for Socket-p pipe (Entry is a named pipe (a "FIFO"))-B: For block-special Files (such as mounted disks)-C: Files or directories-g:setgid files or directories-u:setuid for character-special files (such as I/O devices)-k:file or directory has the sticky bit set-t: The handle is the return result of the TTY (System function isatty (); Cannot use this test for file names)-T: Files are somewhat like "text" Files-B: Files are somewhat like "binary" FILE-M: Modified time (in days)-A: The time of Access (in days)-C: Index node Modification time (units: Days)

test multiple properties of the same file

if (-R $file and-w _) # Virtual file Handle _

{... }

Stack file Operators

Use 5.010;

if (-r-w-x-o-D $file) {

Print "My directory is Readable,writable,and executable!\n";

}

Perl Study notes--File test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.