Linux Find Command: Locate Files and Change File Owner

Ever need to change file permissions on files that are scattered who know’s where up under some directory?
Here is a great command to not only change the file permissions, but also take the work out of locating the files you need to change the permissions on.

The find command is a very powerful tool for not only finding files but also for executing commands on the found files on the fly!

In this example we will use the find command and its powerful parameters to locate files and change their ownership.

You will need sudo permissions to do this. Remember, with great power comes responsibility. Be very careful that what you are doing is what you want to be doing. There is not “undo button”. Although, if you use the -print flag you may at least have a record of what files you effected so that you can manually undo your last command.

OK so here is the command:

sudo find  -type f -user mrfoo -print -exec chown mrbar:bargroup {} ;

The above command translates to: “Find, by looking for files (-type f ) in a directory named /the/path/to/some/directory/to/look/in/, files owned by the user “mrfoo”, print a list of the found files with path (-print), then execute the command “chown mrbar:bargroup (on each found file as indicated by the curly brackets {}).” (Notice the escaped semicolon for the end of the command).

Now there is no need to go looking fall all the files in a given directory to change their file owner. This command works for sever other command as well. I recommend you look up the man page for find and start getting familiar with this powerful command line tool.

Leave a Reply

Your email address will not be published. Required fields are marked *