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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing a script to find a user's files then change ownership and permissions - is my syntax OK? 1

Status
Not open for further replies.

bazil2

Technical User
Feb 15, 2010
148
0
0
DE
(Beginner)

I would like to write a script that will navigate to a specific folder and look for files belonging to the user 'billy' also in subfolders.

I would then like to change the owner of the files found to 'jane' with permissions 777.

Can my syntax be improved upon?

#!/bin/sh
#
cd /path/to/billys/files/
find . -user billy && chown jane && chmod 777
# END

Best regards
 
Hi

With that [tt]&&[/tt] you wrote "if [tt]find[/tt] is successful, then ...". Note that successful, means no error, regardless if anything was found or not.

In such case I prefer to use the [tt]-exec[/tt] :
Code:
find . -user billy -exec chown jane '{}' \+ -exec chmod 777 '{}' \+

Feherke.
[link feherke.github.com/][/url]
 
Many thanks!

May I ask, when using the syntax '{}' does this mean 'undertake the follow on command in the same path where you did the search'?

What does \+ mean; does it mean end of the command?

Best regards
 
Hi

man find said:
-exec command ;

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. [gray](...)[/gray]

-exec command {} +

This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. [gray](...)[/gray]

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top