Find and excute

I use this command quite often to set the permissions on multiple files or directories recursively¬†inside a directory tree. I’ve prepared a couple of sample commands first one change the permissions on files only, second change permissions on directories only.

The base command is

user$ find ./ -type _x_ -execdir _command_ {} +
argument list:
./ - the current directory.
-type _x_ what to look for replace _x_ with, d for directories, f for files, l for links and many more in the man page of the find command.
-execdir _command_ the command and its argument to execute on the target, from the directory were the file is located.
{} +  find replaces this with the target file.

The first example set the permissions to rw-r–r– (644) on all files inside a directory tree.

user$ find ./ -type f -execdir chmod 644 {} +

The second is to change the permissions of directories for rwxr-xr-x (or 755) for all the directories in a directory tree

user$ find ./ -type d -execdir chmod 755 {} +

Now imagine that you want to list all the links and its associated target

user$ find ./ -type l -exec file {} +

NOTE: this time I’m using -exec and not -execdir this because I want to see the relative path from where I’m executing the command.

Comments are closed.