Copy the content/file to all subdirectory in a directory using terminal
67,685
Solution 1
How to put a file in the current working directory in all subfolders (and maybe their subfolders, depending on what you want to do)
This will put the file in all of the subfolders, but not their subfolders:
for d in */; do cp water.txt "$d"; done
This will put the file water.txt
(change all instances of water.txt to the filename you want to copy) in all the subfolders and their subfolders
for i in ./* # iterate over all files in current dir
do
if [ -d "$i" ] # if it's a directory
then
cp water.txt "$i" # copy water.txt into it
fi
done
Info from this linuxquestions thread
Solution 2
You could use that one-liner:
find <target-dir> -type d -exec cp <the file> {} \;
limit depth to 1 -> only the immediate directories
find <target-dir> -type d -maxdepth 1 -exec cp <the file> {} \;
Related videos on Youtube
![22lk94k943 only](https://i.stack.imgur.com/KFGWe.png?s=256&g=1)
Author by
22lk94k943 only
Updated on September 18, 2022Comments
-
22lk94k943 only almost 2 years
I want to copy a file to all subfolders in a folder. How can I do this with the command line?
-
Anake over 7 yearsThis does all subdirectories recursively, not just the immediate subdirectories
-
ortang over 7 years@Anake updated my answer
-
Jinhua Wang over 4 yearsThis answer is excellent!
-
June Wang over 3 yearsHey, what if I only want to put water.txt in the end subfolder of tar_dir?
-
Richard Tyler Miles over 2 yearsI had to reverse flags
-maxdepth 1 -type d
on RHEL