We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Need help with quick shell script

vizaJason asked
Medium Priority
Last Modified: 2010-04-22
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.
Watch Question

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


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.



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");

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Excellent job... Thank you very much!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.