vizaJason
asked on
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.
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.
ASKER
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-fi le.dat
./script2 /root/mydir/permissions-fi le.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!!!
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-fi
./script2 /root/mydir/permissions-fi
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!!!
ASKER
Oh... and I don't care of the format of the permissions text file. That was just an example I made.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent job... Thank you very much!
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....