Execute a Command in Multiple Directories on Linux


Introduction

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. In this article, we'll take a look at some of these methods and show you how to run a command on multiple directories in Linux.

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. To run a command in multiple directories, you can use the find command with the ‘-exec’ option. The “-exec” option allows you to run a command on each file or directory found by the search command. Here is an example −

$ find /path/to/directories -type f -name "*.txt" -exec cat {} \;

The above command will search for all ‘.txt’ files in directories located at “/path/to/directories” and run the cat command on each of them. The ‘{}’ symbol represents the file found by the search command and the ; The symbol indicates the end of the command to be executed.

This is the contents of file1.txt
This is the contents of file2.txt
This is the contents of file3.txt
...

Using the xargs Command

The xargs command is another useful tool for running a command across multiple directories. Allows you to execute a command on standard input, which can be generated by another command. To run a command in multiple directories, you can use the ls command to list the files in each directory and then pipe the output to the xargs command. Here is an example −

$ ls /path/to/directories | xargs cat

The above command will list the contents of the directories located in /path/to/directories and the xargs command will capture each of the files listed.

This is the contents of file1.txt
This is the contents of file2.txt
This is the contents of file3.txt
...

Using a Shell Script

A shell script is a program written in a shell language, such as bash. It allows you to automate tasks and perform complex operations with a single command. To run a command in multiple directories, you can write a shell script that traverses directories and executes the desired command. Here is an example −

#!/bin/bash

for dir in /path/to/directories/*/; 
do
   cd "$dir"
   cat *
   cd -
done

The above script will go through all directories located in ‘/path/to/directories’ and run the cat command on each of the files in each directory. The cd command changes the current directory to the directory being processed, and the “cd -” command changes the current directory to the original directory after executing the command.

This is the contents of file1.txt
This is the contents of file2.txt
This is the contents of file3.txt
...

Running a command on multiple directories in Linux can take a long time if done manually. However, there are several efficient ways to do this, such as the find command, the xargs command, and shell scripts. In this article, we have discussed each of these methods and show you how to run a command on multiple directories in Linux. Whether you are a beginner or an advanced user, these methods will save you time and make your job easier. The find, xargs, and shell script commands can be used in a variety of ways, and you can customize them to suit your specific needs.

Conclusion

In conclusion, running a command on multiple directories in Linux is an easy task when you know the right tools. Using the find command, xargs command or a shell script, you can save time and simplify your work. Whether you're searching for files, running shell scripts, or performing other tasks, these methods will help you achieve your goals efficiently.

Updated on: 19-Apr-2023

584 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements