LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find command question (https://www.linuxquestions.org/questions/linux-newbie-8/find-command-question-731553/)

mokku 06-08-2009 10:39 PM

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?

jamescondron 06-08-2009 10:41 PM

We can, you can't ;)

But seriously, man chown, with particular interest in the --from option

mokku 06-09-2009 07:05 AM

Can we use the find command to search and do it? If you can, why can't you share /:)

colucix 06-09-2009 07:13 AM

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.

druuna 06-09-2009 07:14 AM

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

pixellany 06-09-2009 07:23 AM

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

Quote:

Originally Posted by mokku (Post 3567530)
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......

mokku 06-09-2009 07:51 AM

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.

saivin 06-09-2009 08:06 AM

Quote:

Originally Posted by druuna (Post 3567837)
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


mokku 06-09-2009 08:11 AM

How about this? Is this ok?

find /directory -group group_name -a -user owner_name -exec chgrp newgroup {}\;

mokku 06-09-2009 08:12 AM

Is this ok?

find /directory -group group_name -a -user owner_name -exec chgrp newgroup {}\;

mokku 06-09-2009 08:53 AM

I'm all set. Thanks a lot for all of you.

jamescondron 06-09-2009 10:38 AM

You're all doing it wrong, there is no need for a find command, please see my suggestion, and the man page

druuna 06-09-2009 10:50 AM

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

jamescondron 06-09-2009 11:02 AM

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

druuna 06-09-2009 11:18 AM

@jamescondron: In theory you are 100% correct.


All times are GMT -5. The time now is 12:37 AM.