Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

chown question

Status
Not open for further replies.

dandan123

Technical User
Sep 9, 2005
505
US
I would like to change the owner of a bunch of files from one person to another.

chown -R will change all files which I do not want to do, I want to only change ownership from user A to user B.

How do I do this ?

TIA
 
If you want to change the ownership of some files rather than recursively changing the ownership of a directory an it's contents (i.e. -R), just use:

[tt]chown newowner file1 file2 file3 ...[/tt]

Annihilannic.
 
annihilannic,

There are a lot of files and subdirectories and some of the files have different owners which I do not want to change.
 
Like kHz said, [tt]man find[/tt].
Code:
find /startdir -user olduser -exec chown newuser {} \; -print
The "[tt]-print[/tt]" isn't needed, it's just there so you can see which files were changed.
 
Found this script

Code:
#!/usr/bin/perl

# Make sure this is being run by root. Otherwise, all of the chowns
# are likely to fail
if ($< != 0)
{       print "fixowner must be run with root permissions. Try running\n";
        print "it with sudo.\n";
        print "\n";
        exit -1;
}

if (@ARGV < 3)
{       print "USAGE: fixowner olduid shortname dirtofix\n";
        print "where  olduid is the old UID of the changed user\n";
        print "       shortname is the short username of the changed user\n";
        print "       dirtofix is the directory tree that you want to fix\n";
        print "\n";
        exit -1;
}

$olduser = $ARGV[0];
$newuser = $ARGV[1];
@newuserpw = getpwnam($newuser);
if (@newuserpw == 0)
{       printf "No such user: $newuser\n";
        exit -1;
}

$newuid = $newuserpw[2];
$start = $ARGV[2];

open (LIST, "find \"$start\" -user $olduser \! -path \"/dev/*\" |") ||
        die "Can't pipe in find results\n";

while (<LIST>)
{       ($file) = /(.*)\n$/;
        chown $newuid, -1, $file;
        print "$file\n";
}

may help

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top