In Linux system administration, there are many situations where you may need to execute the same command across multiple directories. Automating this task can save significant time and reduce the likelihood of human error, whether it’s cleaning up log files, updating configurations, or managing files in bulk.

In this tutorial, we’ll explore different ways to execute commands in multiple directories using Linux tools, such as for loops, find, xargs, and shell scripting.

Why Execute Commands Across Multiple Directories?

There are several practical scenarios where executing commands across multiple directories is necessary:

  • Updating Git repositories: Running git pull in several directories to keep repositories up to date.
  • Cleaning up logs: Removing old log files from multiple directories.
  • Batch file processing: Converting or moving files across directories.

Using a For Loop in Bash to Execute Commands in Multiple Directories

One of the simplest ways to execute a command in multiple directories is by using a for loop. This method is straightforward and ideal when you have a fixed list of directories.

Here is an example of listing files in multiple directories:

for dir in /home/user/dir1 /home/user/dir2 /home/user/dir3; do
  (cd "$dir" && ls)
done

Output:

file1.txt  file2.txt  file3.txt
file4.log  file5.txt  file6.jpg
file7.mp4  file8.doc  file9.png

In this example:

  • The for loop iterates over each directory listed in /home/user/dir1, /home/user/dir2, and /home/user/dir3.
    The cd “$dir” changes the directory, and && ls lists the files in that directory.

Using the find Command to Target Directories Dynamically

The find command is a handy tool for searching for directories based on specific criteria, such as names, types, or modification dates. You can also use it to execute a command in each found directory.

Here is an example of how to find directories and run ls in each directory.

find /home/user -type d -exec bash -c 'cd "{}" && ls' \;

Output:

file1.txt  file2.txt  file3.txt
file4.log  file5.txt  file6.jpg
file7.mp4  file8.doc  file9.png

In this example:

  • find /home/user -type d searches for all directories (-type d) under /home/user.
  • The -exec option allows us to execute the ls command in each directory found.

This method is ideal for dynamically targeting directories, especially when the directory structure is large or unknown.

Parallel Execution of Commands Using xargs

If you have many directories and want to execute commands in parallel to speed things up, you can use xargs. This tool allows you to run commands concurrently by specifying the number of processes.

Here is an example to run ls in parallel across directories.

find /home/user -type d | xargs -n 1 -P 4 bash -c 'cd "$0" && ls'

Output:

file1.txt  file2.txt  file3.txt
file4.log  file5.txt  file6.jpg
file7.mp4  file8.doc  file9.png

In this example:

  • xargs -n 1 tells xargs to pass one directory at a time to the bash command.
  • -P 4 runs the command in parallel using 4 processes.

Using Shell Scripting for More Complex Operations

For more complex tasks, such as error handling, conditional execution, or logging, writing a shell script is often the best solution. Let’s see how to create a script to process directories and log the output.

Here is a simple shell script for logging file listings.

#!/bin/bash

LOGFILE="output.log"

for dir in /home/user/dir1 /home/user/dir2 /home/user/dir3; do
  if cd "$dir"; then
    echo "Listing files in $dir:" >> $LOGFILE
    ls >> $LOGFILE
  else
    echo "Failed to change directory to $dir" >> $LOGFILE
  fi
done

When you run the above script, the following output will be produced:

Listing files in /home/user/dir1:
file1.txt  file2.txt  file3.txt

Listing files in /home/user/dir2:
file4.log  file5.txt  file6.jpg

Listing files in /home/user/dir3:
file7.mp4  file8.doc  file9.png

This script does the following:

  • It attempts to change to each directory listed in the for loop.
  • If the directory change is successful, it logs the file listing into output.log.
  • If it fails, it logs an error message.

This method is useful for automating tasks that require logging and error handling.

Conclusion

Executing commands in multiple directories is a common task for Linux administrators and developers. By using methods like for loops, find, xargs, and shell scripting, you can automate repetitive tasks efficiently and avoid manual errors.

Try to experiment with these methods to find the one that best suits your needs, and consider combining them for even more complex scenarios on VPS hosting from Atlantic.Net!