Need help with quick shell script

I need 2 scripts:

1) a shell script that can recursively get all the permissions / owners of a directory and store them in a file.  I imagine it would basically be a list of paths with their attributes.

2) a shell script that can take the file of permssions / owners and recursively apply all those chown / chmod commands.

Both scripts will be run as root.

You can use any language... bash, sh, perl, php.. etc... preferably not python as I do not know python at all.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Not quite sure what you mean (permissions/owners of a directory?)

You can start by doing
       ls -lR >/tmp/foo

If you want to move the files, you can tar/untar -- and that will preserve ownership/permissions...

Do you want numeric or ascii uid/gid?

You can also use find with the printf to do whatever you want, and parse it on the way in...
find . -printf "%p %U %G %m\n" | head -10
. 500 500 755
./linux_cpu_scheduler.lyx 500 500 644
./linux_cpu_scheduler.log 500 500 644
./linux_cpu_scheduler.tex 500 500 644
./linux_cpu_scheduler.aux 500 500 644
./linux_cpu_scheduler.toc 500 500 644
./linux_cpu_scheduler.dvi 500 500 644
./linux_cpu_scheduler.pdf 500 500 644
./ctut.texi 500 500 644
./ctut.log 500 500 644

You can play around with the printf to get a file which looks like what you want....

vizaJasonAuthor Commented:
Hey thanks for your response.

Unfortunatly, thats not what I am looking for.  I am offering 500 points... which is alot... for someone to write 2 quick scripts to do what I asked.  I just don't have time to sit and mess with it or i would do it myself.

I know about tar preserving permissions.  Again... not what I am looking for.  I am not looking for an alternative method to do something you think I am doing.

As for your question regarding owner as a string or numerical id... it doesn't matter.  I only care that the 2nd script can read the file, and restore the permissions.


Script 1 may product a text file looking like this:

/home/jason/document.txt jason.jason 0700
/home/jason/dir/song.mp3 jason.jason 0744
/home/mike/picture.jpg mike.mike 0744

Script 2 will read that file and then execute the following

chown jason.jason /home/jason/document.txt
chmod 0700 /home/jason/document.txt

chown jason.jason /home/jason/dir/song.mp3
chmod 0744 /home/mike/picture.jpg

chown mike.mike /home/mike/picture.jpg
chmod 0744 /home/jason/dir/song.mp3

It would be nice if script 2 checked if the file existed before it executed the commands

Its really really simple.  I hope this helps explain things better.  I would want to execute the scripts like so:

./script1   /home   /root/mydir/permissions-file.dat
./script2   /root/mydir/permissions-file.dat

Thanks again for posting.  Remember... I really don't care on what language you write it in.  If you know php... thats find with me!  Though... shell script would be ideal since it is the most portable.


vizaJasonAuthor Commented:
Oh... and I don't care of the format of the permissions text file.  That was just an example I made.
To get the ownership and permissions:

use Cwd;
use File::Find;
open(StatusFile, ">/tmp/statusfile") || die "Cannot open /tmp/statusfile ";
sub process_file {
($mode,$uid,$gid) = (stat($_))[2,4,5];
        my $path = cwd;
        print StatusFile "$path\/$_:$uid:$gid:$mode\n";
if (!$ARGV[0]){
        die "I need a Directoy as input \n";
find(\&process_file, @ARGV);

To Set the ownership and permissions:
open(StatusFile, "/tmp/statusfile") || die "Cannot open /tmp/statusfile ";
while (<StatusFile>){
($file,$uid,$gid,$mode)= split(/:/,$_);
chown($uid, $gid, "$file");
chmod($mode, "$file");

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vizaJasonAuthor Commented:
Excellent job... Thank you very much!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.