Need help with quick shell script

Posted on 2006-03-23
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.
Question by:vizaJason
    LVL 3

    Expert Comment

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


    Author Comment

    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.



    Author Comment

    Oh... and I don't care of the format of the permissions text file.  That was just an example I made.
    LVL 3

    Accepted Solution

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


    Author Comment

    Excellent job... Thank you very much!

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now