0

I am looking for script to capture the folder time stamp and the files inside the folder

Example: I have a folder Cform12 with files inside say note1.txt , note2.rtf , note3.ldt

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

and

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

Now i have output as

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

Need help me with the shell or perl script for the same.

Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
user3064227
  • 96
  • 1
  • 1
  • 8

3 Answers3

1

I think this script will help you if my understanding is correct..

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


}

The script below does the same but recursively. Is this what you want?

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);
G. Cito
  • 6,210
  • 3
  • 29
  • 42
Thiyagu ATR
  • 2,224
  • 7
  • 30
  • 44
  • Hi This is some what closer to what i am looking for but not exact solution i want to list all the files inside the /var/inner and the timestamp for the folder that it contains example if /var/inner has files a,b and folders f1,f2 and files inside folders f1 as a1 andf2 as a2 then i want the output to be timestamp of inner, inner ,a timestamp of inner, inner ,b timestamp of f1, f1 ,a1 timestamp of f2, f2 ,a2 – user3064227 Dec 19 '13 at 03:37
  • you mean the timestamp will be inner directory timestamp right? – Thiyagu ATR Dec 19 '13 at 07:42
  • yes inned directory time stamp and the directory name also inner directory name but the filenames will be the fine names inside the inner directory. They may be inside 1 level or multiple level – user3064227 Dec 19 '13 at 08:12
  • done...there is small change in the above script..now check the new one – Thiyagu ATR Dec 19 '13 at 09:32
  • Hi Thiyagu thanks this is exactly i want however a small change i want the folder and its time stamp and the folder names just one level down the /export/home/tarumugam/shellshop however it should list files under al the subfolders.Sorry to ask so many changes i am new to perl. example directory structure /export/home/tarumugam/shellshop/scf432/mylog.log /export/home/tarumugam/shellshop/scf432/packages/mypkg.pkg /export/home/tarumugam/shellshop/scf432/xxkns/12.0.0/admin/sql/mytrg.trg /export/home/tarumugam/shellshop/scf543/java/ob/ai/xte.xml – user3064227 Dec 19 '13 at 23:33
  • expected output is line1: timestamp of /export/home/tarumugam/shellshop/scf432 , scf432 , mylog.log line2: timestamp of /export/home/tarumugam/shellshop/scf432 , scf432 , mypkg.pkg line3: timestamp of /export/home/tarumugam/shellshop/scf432 , scf432 , mytrg.trg line4: timestamp of /export/home/tarumugam/shellshop/scf543 , scf543 , xte.xml – user3064227 Dec 19 '13 at 23:34
  • 1
    Hi Thyagu,With your script i have managed to get what i am looking for thanks for all the help – user3064227 Dec 20 '13 at 06:50
  • #!/usr/bin/perl 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; – user3064227 Dec 20 '13 at 06:56
1

I have created the following script to help with my question .Thanks to the contributors for all the directions

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;
G. Cito
  • 6,210
  • 3
  • 29
  • 42
user3064227
  • 96
  • 1
  • 1
  • 8
0

Are you looking for something like the following?

$ 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

A bash scripting guide could help you out a lot. Maybe take a look at http://mywiki.wooledge.org/BashGuide or http://wiki.bash-hackers.org/doku.php ?

Ehtesh Choudhury
  • 7,452
  • 5
  • 42
  • 48
  • I do not have stat in my system.Also i do not want to restrict to one level or 2 level depth it should go to any level of depth. Also I want the out put in a specific format not as shown above. Please let me know if you have any other alternatives – user3064227 Dec 04 '13 at 05:53
  • [This SO thread](http://stackoverflow.com/q/17725526/2019415) on how to *Find a file that was created within 5 days of another* lists a few alternatives to `stat`. Formatting your output should not be difficult with "pure" `perl`: in your shell you will surely have `printf`, `awk` ... etc. if you take a shell command approach – G. Cito Jun 20 '14 at 05:30
  • @Shurane note that `stat` can be a different animal on OS/X, Linux, BSD, Solaris. `perl` has a `stat` function that might hide some of that I guess. I agree it seems like a simple shell task though. – G. Cito Jun 20 '14 at 05:34