[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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.
0
vizaJason
Asked:
vizaJason
  • 3
1 Solution
 
leisnerCommented:
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....


0
 
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.

Example:

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.

Thanks!!!
















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

#!/usr/bin/perl
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);
close(StatusFile);


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



0
 
vizaJasonAuthor Commented:
Excellent job... Thank you very much!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now