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

时间:2013-12-04 04:06:27

标签: perl shell unix

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

示例:我有一个文件夹 Cform12 ,文件里面有 note1.txt note2.rtf note3.ldt < / em>的

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脚本。

3 个答案:

答案 0 :(得分:1)

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

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);

答案 1 :(得分:1)

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

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;

答案 2 :(得分: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