繁体   English   中英

寻找shell / perl脚本来捕获文件夹时间戳和文件夹中的文件

[英]looking for shell / perl script to capture the folder time stamp and the files inside the folder

我正在寻找脚本来捕获文件夹时间戳和文件夹中的文件

示例:我有一个文件夹Cform12 ,文件里面有note1.txtnote2.rtfnote3.ldt

ls -lrt will generate drwxr-xr-x 5 r12applprd dba   4096 Dec  4 02:31 Cform12

ls -lrt SCF6761-PROD will generate 

total 12
-rwxr-xr-x  3 r12applprd dba 4096 Dec  4 02:30 note1.txt
-rwxr-xr-x  3 r12applprd dba 4096 Dec  4 02:30 note2.rtf
-rwxr-xr-x 26 r12applprd dba 4096 Dec  4 02:31 note3.ldt

现在我输出为

Dec  4 02:31 , Cform12 , note1.txt
Dec  4 02:31 , Cform12 , note2.txt
Dec  4 02:31 , Cform12 , note3.txt

需要帮助我使用shell或perl脚本。

如果我的理解是正确的,我认为这个脚本会对你有所帮助。

use strict;
use warnings;
my $path="/var/inner";#directory absolute path
my $mtime=(stat $path)[9];
my $name=`basename $path`;
chomp($name);
$mtime= localtime($mtime);
$mtime=`echo "$mtime" | awk '{print  \$2 , \$3, \$4}'`;
chomp($mtime);
my @files = glob("$path/*");
foreach my $file(@files){

        print "$mtime , $name , ".`basename $file`;


}

下面的脚本执行相同但递归。 这是你想要的吗?

use strict;
use warnings;
my $path="/export/home/tarumugam/shellshop";#directory absolute path
sub rotator{
        (my $path)=@_;
        my $mtime=(stat $path)[9];
        my $name=`basename $path`;
        chomp($name);
        $mtime= localtime($mtime);
        $mtime=`echo "$mtime" | awk '{print  \$2 , \$3, \$4}' 2> /dev/null`;
        chomp($mtime);
        my @files = glob("$path/*");
        foreach my $file(@files){
                if(-f $file){
                        print "$mtime , $name , ".`basename $file`;
                }else{

                        rotator($file);

                }


        }
}

rotator($path);

我创建了以下脚本来帮助解决我的问题。感谢所有方向的贡献者

use strict;
use warnings;
my $dir = '/tmp/';
opendir(DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
  next unless (-d "$dir/$file");
  my $fdir = "$dir/$file";
  print "$fdir\n";
  my $mtime = (stat $fdir)[9];
  $mtime = localtime($mtime);
  $mtime = echo "$mtime" | awk '{print  \$2 , \$3, \$4}' 2 > /dev/ null;
  chomp($mtime);
  my @files1 = find $fdir -type f | awk -F "/" '{ print \$NF }';
  foreach my $fil (@files1) { print "$mtime,$file,$fil"; }
}
closedir(DIR);
exit 0;

你在寻找类似下面的东西吗?

$ stat -c "%x" src; find src -mindepth 1 -maxdepth 1 -exec basename '{}' \;
2013-12-03 22:39:42.911796567 -0500
UndirectedGraphClient.java
UndirectedGraph.java
Bag.java

bash脚本编写指南可以帮助你很多。 也许看看http://mywiki.wooledge.org/BashGuidehttp://wiki.bash-hackers.org/doku.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM