[英]Bash - How to loop through sub directories and copy in a file
I'm new to coding in bash. 我是bash编码的新手。
I'm trying to create something which will loop through all subdirectories, and within each one it should copy a file to that directory. 我正在尝试创建一些循环遍历所有子目录的东西,并且在每个子目录中它应该将文件复制到该目录。
So for example, if I have the following directories 例如,如果我有以下目录
/dir1/
/dir2/
/dir3/
...
...
/dirX/
And a file fileToCopy.txt
还有一个文件fileToCopy.txt
Then I want to run something which will open every single /dirX
file and put fileToCopy.txt
in that directory. 然后我想运行一些将打开每个/dirX
文件并将fileToCopy.txt
放在该目录中的东西。 Leaving me with: 离开我:
/dir1/fileToCopy.txt
/dir2/fileToCopy.txt
/dir3/fileToCopy.txt
...
...
/dirX/fileToCopy.txt
I would like to do this in a loop, as then I am going to try to modify this loop to add some more steps, as ultimately the .txt file is actually a .java file, I am wanting to copy it into each directory, compile it (with the other classes in there), and run it to gather the output. 我想在循环中执行此操作,因为我将尝试修改此循环以添加更多步骤,因为最终.txt文件实际上是.java文件,我想将其复制到每个目录中,编译它(与其中的其他类),并运行它来收集输出。
Thanks. 谢谢。
for i in dir1, dir2, dir3, .., dirN
do
cp /home/user1068470/fileToCopy.txt $i
done
Alternatively, you can use the following code. 或者,您可以使用以下代码。
for i in *
do # Line breaks are important
if [ -d $i ] # Spaces are important
then
cp fileToCopy.txt $i
fi
done
查找当前目录(。)下的所有目录并将文件复制到其中:
find . -type d -exec cp fileToCopy.txt '{}' \;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.