Looping through directories recursively in Bash
This article explains how to recursively loop through directories in Bash.
Looping through directories recursively in Bash
When dealing with different directories, it is often necessary to traverse the directories. We can use similar commands in all Linux terminals including Bash to recursively loop through the directories.
First, we use the find command to view the structure of the current directory. The find command will display all files and folders in the current or given directory.
View all files:
find . -type f -print0
To view the files in a specific directory:
find /mnt/c/Users/Sheeraz/DemoFolder1 -type f -print0
/mnt/c/Users/Sheeraz/DemoFolder1 is the directory path we want to check.
The -type f option is used to get only the files of that directory and not the folders. The output of this command is as follows.
/mnt/c/Users/Sheeraz/DemoFolder1/Jiyik.java/mnt/c/Users/Sheeraz/DemoFolder1/jiyik.rtf/mnt/c/Users/Sheeraz/DemoFolder1/demo.bmp/mnt/c/Users/Sheeraz /DemoFolder1/DemoFolder2/Jiyik.java/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/jiyik.rtf/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/demo.bmp/mnt/c/Users/Sheeraz/DemoFolder1 /DemoFolder2/Example1.java/mnt/c/Users/Sheeraz/DemoFolder1/Example1.java/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/Jiyik.java/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/jiyik .rtf/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/demo.bmp/mnt/c/Users/Sheeraz/D
If you also want to get the folders of the directory, remove -type f from the above command .
find /mnt/c/Users/Sheeraz/DemoFolder1 -print0
Once we know the file name structure, we can recursively loop through the directory using the following code:
for x in $(find /mnt/c/Users/Sheeraz/DemoFolder1 -type f -print0)
do
echo $x;
done;
The above code recursively loops through the directory /mnt/c/Users/Sheeraz/DemoFolder1 and echoes the name of each file. Look at the output of this command:
/mnt/c/Users/Sheeraz/DemoFolder1/Jiyik.java/mnt/c/Users/Sheeraz/DemoFolder1/jiyik.rtf/mnt/c/Users/Sheeraz/DemoFolder1/demo.bmp/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/Jiyik.java/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/jiyik.rtf/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/demo.bmp/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/Example1.java/mnt/c/Users/Sheeraz/DemoFolder1/Example1.java/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/Jiyik.java/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/jiyik.rtf/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/demo.bmp/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/Example1.java
Recursively loop through a directory to get files with a specific extension
As we can see, we don't need to display or process all files. We can also use this command with the -name option to get files with a specific extension.
View the command:
for x in $(find /mnt/c/Users/Sheeraz/DemoFolder1 -type f -name '*.java');
do
echo $x;
done;
The above code will only print the files with extension .java in /mnt/c/Users/Sheeraz/DemoFolder1. See the output:
/mnt/c/Users/Sheeraz/DemoFolder1/Jiyik.java
/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/Jiyik.java
/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/Example1.java
/mnt/c/Users/Sheeraz/DemoFolder1/Example1.java
/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/Jiyik.java
/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/Example1.java
Recursively loop through directories to move files to another directory
We can also move files by recursively looping through directories using the Bash mv command; the syntax of this command is as follows.
mv -v $filename $destination directory
We can use this command in a recursive loop to move files to a target directory. Let us take an example.
for x in $(find /mnt/c/Users/Sheeraz/DemoFolder1 -type f -name '*.java');
do
mv -v $x /mnt/c/Users/Sheeraz/DemoFolder2
done;
DemoFolder2 is an empty directory.
Output:
renamed '/mnt/c/Users/Sheeraz/DemoFolder1/Jiyik.java' -> '/mnt/c/Users/Sheeraz/DemoFolder2/Jiyik.java'
renamed '/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/Jiyik.java' -> '/mnt/c/Users/Sheeraz/DemoFolder2/Jiyik.java'
renamed '/mnt/c/Users/Sheeraz/DemoFolder1/DemoFolder2/Example1.java' -> '/mnt/c/Users/Sheeraz/DemoFolder2/Example1.java'
renamed '/mnt/c/Users/Sheeraz/DemoFolder1/Example1.java' -> '/mnt/c/Users/Sheeraz/DemoFolder2/Example1.java'
renamed '/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/Jiyik.java' -> '/mnt/c/Users/Sheeraz/DemoFolder2/Jiyik.java'
renamed '/mnt/c/Users/Sheeraz/DemoFolder1/SubFolder/Example1.java' -> '/mnt/c/Users/Sheeraz/DemoFolder2/Example1.java'
As we can see, the command recursively loops through the directories and moves the files with the java extension to the target directory.
Recursively loop through directories to delete files
We can rm -rf
remove files from a given directory using the command. The syntax of this command is as follows.
rm -rf $filename
Let's put this command in a recursive loop to recursively delete all the files from the directory.
for x in $(find /mnt/c/Users/Sheeraz/DemoFolder1 -type f);
do
rm -rf $x
done;
The above script will delete all files in the main directory and subdirectories; it will not delete the folder because only files are selected in the command. See the output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Hosting Docker Internal in Linux
Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM
-
Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t
Setting the working directory in Docker
Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM
-
If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR
How to get IP address in CentOS
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for
Updating YUM in Linux
Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM
-
This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux
Installing Deb Files in Linux
Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM
-
In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy
lsof Command in Linux
Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM
-
In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U
How to solve the problem of not being able to execute binary files in Linux
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
In this article, we will learn how to execute binary files in Linux. We will also learn how to troubleshoot the error if Linux fails to execute the binary file. Usually, this error occurs when we run shell scripts in Linux. This article wil
Error in Linux Mesg: Ttyname Failed: Inappropriate Ioctl for Device Error
Publish Date:2025/03/23 Views:178 Category:OPERATING SYSTEM
-
In this article, we will learn how to fix the error mesg: ttyname failed: Inappropriate ioctl for device in Linux . We will discuss some of the causes of this error and show you how to fix it. Let's start with what causes this error in Linu
ps aux command in Linux
Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM
-
If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou