recursive cp?

  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1614

Post 3+ Months Ago

I need to copy one file (say file.ext) from /home/ into each of the folders and sub-folders under it. i.e.


-> unix prompt command ->


Can anyone help me out?
  • vijayan
  • Beginner
  • Beginner
  • vijayan
  • Posts: 52
  • Loc: india

Post 3+ Months Ago

Hello ulstermonkey,

sorry not idea.
but you know scripting knowledge then we can do with it.
  • roca
  • Newbie
  • Newbie
  • roca
  • Posts: 5

Post 3+ Months Ago

Hello guys!

This is a very basic shell script that should help you:

------------------------------- cut here -----------------------------
#! /bin/bash

if [ $# -ne 1 ]; then
echo "usage: $1 <file to recursively copy>";
exit 1;


for d in `du | cut -c 3-`
cp $filename $d;
----------------------- cut here ---------------------------------

Paste this code on a empty file, with execution rights. Then execute it on your home

regards, roca.
  • PsyckBoy
  • Graduate
  • Graduate
  • PsyckBoy
  • Posts: 204
  • Loc: SoCal

Post 3+ Months Ago

The script above will not work for any directory name in the tree containing spaces. The following command will do what you want.
Code: [ Select ]
find ./ -mindepth 1 -type d -print0 | xargs -L 1 -0 cp file.ext

You should be in the "home" directory when you execute this.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 18 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.