LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2009, 11:39 PM   #1
mokku
Member
 
Registered: Feb 2007
Posts: 197

Rep: Reputation: 30
find command question


I have to find the ownership of the file recursively and if the ownership is "A", I need to change it to B.

Can we do it in one line?
 
Old 06-08-2009, 11:41 PM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
We can, you can't

But seriously, man chown, with particular interest in the --from option
 
Old 06-09-2009, 08:05 AM   #3
mokku
Member
 
Registered: Feb 2007
Posts: 197

Original Poster
Rep: Reputation: 30
Can we use the find command to search and do it? If you can, why can't you share /
 
Old 06-09-2009, 08:13 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Well.. jamescondron already shared the solution. Also you have to read the man page of find and see how can you execute a command on the files found... see the action -exec. The reason for which we cannot give the exact solution is that your question smells as homework and according to the LQ rules, we cannot help so much. For obvious reasons.
 
Old 06-09-2009, 08:14 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

This should work:

find /start/directory -user A -exec chown B {} \;

First try this: find /start/directory -user A and check the output. If correct rerun the command with the -exec chown B {} \; part attached.

This must be run by a user that has sufficient rights to do the chown command (probably root).

Hope this helps.

EDIT
Thanks to saivin for pointing out the mistake in the chown command (changed in the above text)
/EDIT

Last edited by druuna; 06-09-2009 at 09:54 AM. Reason: Switched 2 options in command.
 
Old 06-09-2009, 08:23 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Edit: While I typing and reading, the more experienced folk have given the answer....

Quote:
Originally Posted by mokku View Post
I have to find the ownership of the file recursively and if the ownership is "A", I need to change it to B.

Can we do it in one line?
This sounds a bit like homework---why do you need to do this?

You cannot do anything recursively to one file---perhaps you mean a group of files / directories.

I think you want something like:

find <path> -user <name of owner> -exec chown <newname> '{}' \;

"man find" for the details......

Last edited by pixellany; 06-09-2009 at 08:25 AM.
 
Old 06-09-2009, 08:51 AM   #7
mokku
Member
 
Registered: Feb 2007
Posts: 197

Original Poster
Rep: Reputation: 30
Thank you for all the help. It is not a home work. What I'm trying to archive is if the owner is A, I need to check the group and if the group is C, I need to change it to D. Can I do it in one line?

Thanks in advance.
 
Old 06-09-2009, 09:06 AM   #8
saivin
Member
 
Registered: Dec 2008
Location: Bengaluru, India
Distribution: CentOS, openSUSE, Mandriva, Slackware
Posts: 111

Rep: Reputation: 19
Quote:
Originally Posted by druuna View Post
Hi,

This should work:

find /start/directory -user A -exec chown {} B \;
Code:
saivin@sv-debian:~$ sudo find ~/test -user saivin -exec chown {} root \;
chown: invalid user: `/home/saivin/test'
chown: invalid user: `/home/saivin/test/3'
chown: invalid user: `/home/saivin/test/2'
chown: invalid user: `/home/saivin/test/1'

saivin@sv-debian:~$ sudo find ~/test -user saivin -exec chown root '{}' \;
saivin@sv-debian:~$ ls -l test/
total 0
-rw-r--r-- 1 root saivin 0 2009-06-09 18:25 1
-rw-r--r-- 1 root saivin 0 2009-06-09 18:25 2
-rw-r--r-- 1 root saivin 0 2009-06-09 18:26 3
 
Old 06-09-2009, 09:11 AM   #9
mokku
Member
 
Registered: Feb 2007
Posts: 197

Original Poster
Rep: Reputation: 30
How about this? Is this ok?

find /directory -group group_name -a -user owner_name -exec chgrp newgroup {}\;
 
Old 06-09-2009, 09:12 AM   #10
mokku
Member
 
Registered: Feb 2007
Posts: 197

Original Poster
Rep: Reputation: 30
Is this ok?

find /directory -group group_name -a -user owner_name -exec chgrp newgroup {}\;
 
Old 06-09-2009, 09:53 AM   #11
mokku
Member
 
Registered: Feb 2007
Posts: 197

Original Poster
Rep: Reputation: 30
I'm all set. Thanks a lot for all of you.
 
Old 06-09-2009, 11:38 AM   #12
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
You're all doing it wrong, there is no need for a find command, please see my suggestion, and the man page
 
Old 06-09-2009, 11:50 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
You're all doing it wrong
No we don't

Remember: *nix has more then one way of doing things.........

I agree that some of those solutions are elegant, some are resource unfriendly, some are unreadable and some just are.

In the end a solution for the problem is needed and the work must be done.

Your solution is just one of the solutions for this problem, maybe it is the most elegant or fastest of them all, but that does not make all the other solutions wrong....
 
Old 06-09-2009, 12:02 PM   #14
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
No, the Unix philosophy always has and always will be 'Best Tool For The Job'. The Unix philosophy has never been about multiple ways to solve a problem.

You could even, as Raymond (I think) did, put it down to the KISS principle; Keep It Simple, Stupid.
Code:
find /start/directory -user A -exec chown B {} \;
is not simple, or not as simple as:
Code:
chown --from=A: B *
Which also fills the tenet 'Do One Thing, Do It Well'- a find/exec command does two things in a bash context (They almost do the same thing at code level, but not quite).
 
Old 06-09-2009, 12:18 PM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@jamescondron: In theory you are 100% correct.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on Find Command: - Vs + JockVSJock Linux - Newbie 4 04-29-2009 02:44 AM
Question about find command centosfan Linux - Server 2 11-10-2008 08:36 PM
fgrep or find command question. swamprat Linux - Newbie 2 02-13-2008 02:26 PM
Question on find command rytrom Linux - Newbie 3 08-07-2003 03:14 AM
Find Command - Basic Question tlb04 Linux - General 3 05-06-2003 09:45 PM


All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration