How to Execute Commands on Directories Only in Ubuntu (find -type d -exec)

The Directory Management Challenge

When restructuring a web server in Ubuntu Linux, you frequently need to execute bulk permission changes that apply only to folders and strictly ignore the files contained within them. For example, a standard WordPress installation requires all directories to possess 755 permissions, while all files must strictly remain at 644. If you run a blanket chmod -R 755, you will instantly compromise the security of every single file in the hierarchy. You must combine the filtering power of find with the execution engine of -exec to surgically target folders.

Using the find Command with -type d and -exec

The Linux find command allows you to stack the -type d (Directory) filter directly onto the -exec action flag to isolate structural nodes.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To scan the /var/www/html/ directory and apply chmod 755 exclusively to the folders, type the following command exactly:
  3. find /var/www/html/ -type d -exec chmod 755 {} \;
  4. Press Enter.

Targeted Execution Forensics

The syntax processes left to right. First, -type d evaluates the filesystem and completely discards any node that is a file, symlink, or socket. Only true directories are passed down the chain. The -exec flag then takes that filtered list and injects the directory paths one by one into the {} placeholder, running the chmod 755 command. Finally, the \; terminates the execution string. This command is an absolute necessity for system administrators repairing broken permission matrices on complex web application stacks.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.