Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Execute a Command in Multiple Directories on Linux
As a Linux user, it's common to run the same command in multiple directories. For example, you might want to search for all files with a certain extension or run a shell script in multiple directories. This task can be time-consuming if done manually, and becomes even more tedious as the number of directories increases. Fortunately, Linux provides several methods for running a command across multiple directories efficiently.
Using the Find Command to Execute Commands
The find command is one of the most powerful commands available in Linux. It allows you to search files and directories based on different criteria like name, size, date, etc. You can also execute commands on the files and directories found by the find command using the -exec option.
Example Execute Command on Files
find /path/to/directories -type f -name "*.txt" -exec cat {} \;
This command searches for all '.txt' files in directories at "/path/to/directories" and runs the cat command on each. The {} represents the file found, and \; indicates the end of the command to execute.
This is the contents of file1.txt This is the contents of file2.txt This is the contents of file3.txt
Example Execute Command in Each Directory
find /path/to/parent -type d -exec sh -c 'cd "{}" && ls -la' \;
This executes ls -la in each subdirectory found.
Using the xargs Command
The xargs command executes commands using input from another command. It's particularly useful when combined with find to process multiple directories efficiently.
Example Process Multiple Files
find /path/to/directories -name "*.txt" | xargs cat
This finds all .txt files and passes them to cat for processing.
Example Execute in Multiple Directories
find /path/to/parent -type d | xargs -I {} sh -c 'cd "{}" && pwd && ls'
The -I {} option allows you to specify where the input should be placed in the command.
Using a For Loop
A simple for loop can iterate through directories and execute commands in each one.
for dir in /path/to/directories/*/; do
cd "$dir"
echo "Processing directory: $dir"
ls -la
cd -
done
This script changes to each directory, executes commands, then returns to the original location with cd -.
Using a Shell Script
For more complex operations, create a dedicated shell script that can be reused and customized.
#!/bin/bash
# Script to execute commands in multiple directories
PARENT_DIR="/path/to/directories"
COMMAND="ls -la"
for dir in "$PARENT_DIR"/*/; do
if [ -d "$dir" ]; then
echo "Executing in: $dir"
cd "$dir"
eval "$COMMAND"
cd - > /dev/null
echo "---"
fi
done
This script is more robust, checking if each item is actually a directory before processing.
Comparison of Methods
| Method | Best For | Advantages | Disadvantages |
|---|---|---|---|
| find -exec | File-based operations | Powerful search criteria, built-in | Complex syntax for directory operations |
| xargs | Processing many items efficiently | Handles large outputs well | Requires careful handling of spaces |
| For loop | Simple directory iteration | Easy to understand and modify | Less efficient for large numbers |
| Shell script | Complex, reusable operations | Highly customizable, reusable | Requires separate file |
Key Points
Use
find -execfor file-based operations with complex search criteriaUse
xargswhen processing large numbers of files or directoriesUse for loops for simple directory iteration tasks
Always quote variables containing paths to handle spaces correctly
Test commands on a small subset before running on all directories
Conclusion
Running commands across multiple directories is a common Linux task that can be accomplished efficiently using find, xargs, for loops, or shell scripts. Each method has its strengths choose based on your specific needs and complexity requirements. These techniques will save significant time and reduce manual effort in directory operations.
