Operating System - Linux
1748217 Members
4161 Online
108759 Solutions
New Discussion юеВ

Script help needed (easy one)

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Script help needed (easy one)

OK...brain fart happening here....
I want to take a list that is in a file and execute a chown on each entry so that the owner is the same....

OK...I didn't even understand that. Let me lay it out like this...

File 1 contains the following entries:
abc
def

I want to execute a chown on each in a way like this...

chown abc abc

Therefore making the owner the same as the name of the file.

However I have a very long list to do...I'm lazy....and scripting is not my strength. Any help is appreciated.
8 REPLIES 8
Steven E. Protter
Exalted Contributor
Solution

Re: Script help needed (easy one)

while read -r filename
do
chown $filename:$filename $filename
done < filelist

Thats a simple shell that changes ownership and group.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Muthukumar_5
Honored Contributor

Re: Script help needed (easy one)

Only abc is as user rgt. You have to use as,

while read line;
do
chown $line $line
done < filename

or

awk '{ print "chown "$0" "$0 }' filename | sh

hth.
Easy to suggest when don't know about the problem!
TwoProc
Honored Contributor

Re: Script help needed (easy one)

cat filename.list | xargs -i chown {} {}

We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor

Re: Script help needed (easy one)

for i in `cat filename.list'
do
chown $i $i
done
We are the people our parents warned us about --Jimmy Buffett
Muthukumar_5
Honored Contributor

Re: Script help needed (easy one)

You can do also as,

perl -pi -e 's/^(.*)/chown \$1 \$1/'

sh

hth.
Easy to suggest when don't know about the problem!
Sandman!
Honored Contributor

Re: Script help needed (easy one)

Bob,

Here is a one-line awk construct that can be used for your script:

# awk '{system("chown "$1":"$1" "$1)}' File1

cheers!
Rodney Hills
Honored Contributor

Re: Script help needed (easy one)

"xargs" is very handy for this sort of thing.

cat filename | xargs -i: chmod : :

HTH

-- Rod Hills
There be dragons...
Tom Schroll
Frequent Advisor

Re: Script help needed (easy one)

In agreement with one of John's answers, my vote is to use xargs. It's simple, very very fast, portable, and is designed for this purpose.

cat filename.list | xargs -i chown {} {}

You may need to escape the brackets {} depending on what shell you are using.

Things to note:
xargs is one of those small and fast swords to keep at your side. It can do great things at fast speeds in a very small package.

Another example of xargs would be change permissions of files:

cat filelist | xargs chmod 777


Or in another example, use find to pipe directly to xargs and print the cksum of each file:

find . -print | xargs cksum


And of course can be used to destroy things:

find . -print | xargs rm -f

(hehehe, DO NOT ACTUALLY DO THAT unless you want to whack all files in a directory)

Read the man page for xargs for more info.

-- Tom
If it ain't broke, it needs optimized.