Sunday, May 12, 2024
42
rated 0 times [  42] [ 0]  / answers: 1 / hits: 74691  / 2 Years ago, sun, september 4, 2022, 4:46:19

I want to copy a file to all subfolders in a folder. How can I do this with the command line?


More From » command-line

 Answers
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


[#31019] Sunday, September 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
socelebrate

Total Points: 274
Total Questions: 123
Total Answers: 124

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;