Operating System - HP-UX
1754971 Members
3077 Online
108828 Solutions
New Discussion юеВ

how to list all changed/added files under file systems?

 
SOLVED
Go to solution
A. Clay Stephenson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

This would really be better and more efficiently done with Perl's File::Find module because you have everything you need including the stat() function so that you wouldn't need an external ls -l command. In any event, your find command needs to only do regular files because when you do an ls -l on a directory the files underneath are listed as well.

add "-type f" to your find command so that only regular files with match.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

Okay, this should do what you want. It will list each regular file and its size that has been modified within the last 24 hours.

Use it like this:

find24.pl /aaa /bbb/ccc /xxx
If it ain't broke, I can fix that.
Hanry Zhou
Super Advisor

Re: how to list all changed/added files under file systems?

It works now.

I am sorry, but my next follow-up question is how can we sum up the 5th field (amount of space) of each one of files together? I wanted to calcuate how much these new created/modified files will occupy. Thanks,
none
James R. Ferguson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

Hi Hanry:

...and if you wanted to do this with Perl, as Clay suggests:

# perl -MFile::Find -wle 'File::Find::find(sub{print $_," ",scalar localtime ((stat(_))[9]),"\n" if -f && -M _ <1},@ARGV)' /path

...would give you the filenames and modification (mtime) times for all files below "/path".

Change "[9]" to "[10]" if you want the 'ctime' instead of the 'mtime'.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

Hi (again) Hanry:

If you want the total size summation per your last question, just do this:

# perl -MFile::Find -wle 'File::Find::find(sub{$size+=((stat(_))[7]) if -f && -M _ <1},@ARGV);END{print $size}' /path

Regards!

...JRF...
Hanry Zhou
Super Advisor

Re: how to list all changed/added files under file systems?

James,

If you can, it'd be better if you can use ksh, since we don't know perl here.
none
A. Clay Stephenson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

Then you should learn Perl because the Perl solution is going to execute ~50x as fast as a shell equivalent and since the code is already written, you don't need to know any Perl.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

Hi Hanry:

OK, if you won't use Perl, even though you have it installed, here's a *pure* shell approach to totalling the file sizes in bytes for all files in "/path" that have been modified during the last 24-hours.

#!/usr/bin/sh
typeset -i TOTSIZE;
find /tmp -xdev -type f -mtime -1 -exec ls -l {} \; | \
while read X X X X SIZE D1 D2 D3 NAME
do
echo "${NAME} ${SIZE} ${D1} ${D2} ${D3}"
TOTSIZE=$(( ${TOTSIZE} + ${SIZE} ))
done
echo "\nTotal Size = $TOTSIZE"
exit 0

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

Hi Hanry:

OK, if you won't use Perl, even though you have it installed, here's a *pure* shell approach to totalling the file sizes in bytes for all files in "/path" that have been modified during the last 24-hours.

#!/usr/bin/sh
typeset -i TOTSIZE;
find /tmp -xdev -type f -mtime -1 -exec ls -l {} \; | \
while read X X X X SIZE D1 D2 D3 NAME
do
echo "${NAME} ${SIZE} ${D1} ${D2} ${D3}"
TOTSIZE=$(( ${TOTSIZE} + ${SIZE} ))
done
echo "\nTotal Size = $TOTSIZE"
exit 0

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: how to list all changed/added files under file systems?

In addition to Perl's much superior performance there is still another reason
that you shouldn't use the shell for this: possible integer overflow. If enough files are changed within the timeframe to overflow the shell's 32-bit signed integers then your results are bogus. This can be avoided by using bc to do the may=th but at a cost of even poorer performance.

Here's a refined Perl script that will do everything;

Use it like this:

finder.pl -g -s 86400 /dir1 /dir2 ...

This will descend each directory listed and give a total for each for all regular files that have been modified within the last 86400 seconds (1 day) and (-g) list a grand total. Because I intentionally did not do integer arithmetic, the totals will not overflow.

Invoke as finder.pl -u for full usage.
If it ain't broke, I can fix that.