How to use Shell loop In Linux
Here, we are using bash for loop to copy the xyz.txt file in the /home/kalilinux/dir2 /home/kalilinux/dir3 directories and also a folder dir1, directly where:
- for: It is illustrating the folders and directories.
- dest: It is used to create a stream for writing metadata objects to the file system.
- dir1: It is the folder
- /home/kalilinux/dir2 /home/kalilinux/dir3: These are the directories that are separated by the spaces that we give between them.
- do: It is executing the cp command.
- -v: It is used to display informative messages as the copy is performed.
- ‘xyz.txt‘: It is the file name that we want to copy in the folder and directories.
- done: is used to end the shell script.
Syntax:
for dest in dir1 /home/kalilinux/dir2 /home/kalilinux/dir3 ; do cp -v xyz.txt “$dest” ; done
Output:
How to Copy a File to Multiple Directories in Linux
In this article, we will discuss how to copy a file to multiple directories in Linux. xargs, find, tee, shell loop, and parallel are the commands that can help you to copy a File to Multiple Directories.
Suppose we have a file with the name “xyz.txt” and we want to copy it into 3 directories with the name dir1, dir2, and dir3 then we use the following methods: