The Workflow Bottleneck
Finding files in an Ubuntu Linux terminal is only half the battle. Once you locate a specific set of files—such as all the .log files modified in the last 24 hours—you usually need to do something with them. You might need to change their permissions, copy them to a backup directory, or delete them entirely. Manually typing out the file paths for fifty different files to run a command on each one is incredibly inefficient. Instead, you can instruct the Linux search engine to automatically pass every single file it finds directly into a secondary command.
Using the find Command with -exec
The Linux find command utilizes the -exec (execute) flag to run any standard terminal command on the matched files in real-time.
- Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
- To search the
/var/www/directory for all files ending in.txtand immediately change their permissions to 644, type the following command exactly: sudo find /var/www/ -name "*.txt" -exec chmod 644 {} \;- Press Enter and provide your administrator password.
Understanding the Syntax
The syntax for -exec requires two specific elements. First, the open and closed brackets ({}) act as a placeholder. The search engine will dynamically replace those brackets with the exact path of the file it just found. Second, you must terminate the execution string with a backslash and a semicolon (\;). This tells the search engine where your secondary command ends, ensuring that the chmod command is executed sequentially on each individual file rather than crashing the terminal.