Wednesday, December 29, 2010

The "Find" Command

The find command is not only useful for finding files by name, size, owner, etc, but it is also very powerful in limiting files to which you apply other commands either by using the pipe or arguments to find. For example, to change permissions on files only you can use:

find -maxdepth 1 -type f -exec chmod 644 {} \;

This command will change permissions in the present working directory (-maxdepth 1) to 644 on files only.

To change permissions on directories only use:

find -maxdepth 1 -type d -exec chmod 755 {} \;

This command will also be limited to the present working directory. It will change permissions to 755 on directories only. If you want to control recursion, you can use a different number with the -maxdepth option or leave it out altogether to recurse into all lower directories. Be careful, though, the find command is very powerful and can lead to unexpected and disastrous results if not used properly.

No comments:

Post a Comment