• 1 
瀏覽模式: 普通 | 列表

perl 找出一天內的新檔案

使用 File::Find

use File::Find;

# 從 1970-01-01 到現在所經過的秒數值
$date_time_second_since = time();

# 找 /var/log
find(\&find_file_wanted,'/var/log');

sub find_file_wanted {
        # ↓↓↓ 可以顯示絕對路徑
        $found_file = $File::Find::name;
        # 檔案的屬性
        ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($found_file);

        # 數出時間差值小於 一天的
        if (($date_time_second_since - $mtime) < 86400) {
                print "$found_file ";
        }
}

stat() 說明

  0 dev        device number of filesystem
  1 ino         inode number
  2 mode     file mode  (type and permissions)
  3 nlink       number of (hard) links to the file
  4 uid         numeric user ID of file's owner
  5 gid         numeric group ID of file's owner
  6 rdev       the device identifier (special files only)
  7 size        total size of file, in bytes
  8 atime     last access time in seconds since the epoch
  9 mtime     last modify time in seconds since the epoch
10 ctime      inode change time in seconds since the epoch (*)
11 blksize    preferred block size for file system I/O
12 blocks     actual number of blocks allocated

如果只是要取 mtime,也可以這樣作

($mtime) = (stat ($found_file)) [9];

find 的其它用法 http://www.unix.com.ua/orelly/perl/prog3/ch32_21.htm

find sub { print "$File::Find::name " if -d }, ".";
標籤: perl script 程式

  • 1