Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Perl/ksh script to find files used by users...

 
SOLVED
Go to solution
Highlighted
Super Advisor

Perl/ksh script to find files used by users...

Ok,

We have directories like /home11 that store users directories. I need a perl/ksh script that searches the directories, lists the files from biggest to smallest and detirmines whther or not any of these diirectories or files is an orhan.

-TIA
20 REPLIES 20
Highlighted
Honored Contributor

Re: Perl/ksh script to find files used by users...

What do you mean by "an orphan"? The owning user no longer exists?
Decay is inherent in all compounded things. Strive on with diligence
Highlighted
Super Advisor

Re: Perl/ksh script to find files used by users...

Orphan file = not owned by anyone listed in the /etc/passwd file.
Highlighted
Acclaimed Contributor

Re: Perl/ksh script to find files used by users...

This will get you started:

find . -type f -nouser -print
If it ain't broke, I can fix that.
Highlighted
Honored Contributor

Re: Perl/ksh script to find files used by users...

Find and sort:

# perl -MFile::Find -le'find(sub{-f and push@{$f{-s$_}},$File::Find::name},@ARGV);for$s(sort{$b<=>$a}keys%f){printf"%9d %s\n",$s,$_ for sort@{$f{$s}}}' /home11


Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Highlighted
Acclaimed Contributor

Re: Perl/ksh script to find files used by users...

Hi Jody:

# find /home -xdev -nouser
# find /home -xdev -nogroup

...will find your unowned (no password or no group)

For listing the contents from largest to smallest:

# du -xk /home|sort -k1nr

Regards!

...JRF...
Highlighted
Honored Contributor

Re: Perl/ksh script to find files used by users...

And plus ORPH indicator:

# perl -MFile::Find -le'find(sub{-f and push@{$f{-s$_}},[$File::Find::name,(stat$_)[4]]},@ARGV);for$s(sort{$b<=>$a}keys%f){printf"%4s %9d %s\n",($_->[1]eq getpwuid$_->[1]?"ORPH":""),$s,$_->[0] for sort@{$f{$s}}}' /home11

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Highlighted
Honored Contributor

Re: Perl/ksh script to find files used by users...

What I came up with...

for file in $(find . -type f);
do
filesize=$(ls -l $file | awk '{print $5}')
fileowner=$(ls -l $file | awk '{print $3}')
orphan=""
if [[ $(grep -c "^$fileowner:" /etc/passwd) -eq 0 ]];
then orphan="ORPHANED FILE!"
fi
echo "$filesize $file $orphan"
done | sort -nr +0



HTH,
Jon
Decay is inherent in all compounded things. Strive on with diligence
Highlighted
Honored Contributor

Re: Perl/ksh script to find files used by users...

OK, I really need to learn more perl :)
Decay is inherent in all compounded things. Strive on with diligence
Highlighted
Super Advisor

Re: Perl/ksh script to find files used by users...

Procura,

Is there a way to combine the two perl statements?

Sorry but, I am still trying to learn perl.

THanks,