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

2 Answers 2


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
    if [ -d "$i" ] # if it's a directory
        cp water.txt "$i" # copy water.txt into it

Info from this linuxquestions thread

  • Hey, what if I only want to put water.txt in the end subfolder of tar_dir?
    – June Wang
    Dec 19, 2020 at 4:23

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> {} \;

  • 2
    This does all subdirectories recursively, not just the immediate subdirectories
    – Anake
    Feb 27, 2017 at 11:13
  • 2
    @Anake updated my answer
    – ortang
    Feb 27, 2017 at 15:37
  • This answer is excellent! Oct 23, 2019 at 9:48
  • I had to reverse flags -maxdepth 1 -type d on RHEL Dec 17, 2021 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .