recursive cp?

  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Jun 23, 2004
  • Posts: 1616
  • Status: Offline

Post November 15th, 2006, 12:27 pm

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

/home/file.ext
/home/level1/
/home/level1/text/
/home/level1/folderb/
/home/level2/
/home/level2/foldera/
/home/level2/folderb/
/home/level2/folderb/text/
/home/level2/folderb/input/
/home/level2/folderb/output/
/home/level3/input/

-> unix prompt command ->

/home/file.ext
/home/level1/file.ext
/home/level1/text/file.ext
/home/level1/folderb/file.ext
/home/level2/file.ext
/home/level2/foldera/file.ext
/home/level2/folderb/file.ext
/home/level2/folderb/text/file.ext
/home/level2/folderb/input/file.ext
/home/level2/folderb/output/file.ext
/home/level3/input/file.ext

Can anyone help me out?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post November 15th, 2006, 12:27 pm

  • vijayan
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Jul 31, 2006
  • Posts: 52
  • Loc: india
  • Status: Offline

Post November 16th, 2006, 4:13 am

Hello ulstermonkey,

sorry not idea.
but you know scripting knowledge then we can do with it.
  • roca
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Nov 07, 2006
  • Posts: 5
  • Status: Offline

Post November 17th, 2006, 5:43 pm

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;
fi

filename=$1

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

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

regards, roca.
  • PsyckBoy
  • Graduate
  • Graduate
  • No Avatar
  • Joined: Jun 20, 2005
  • Posts: 204
  • Loc: SoCal
  • Status: Offline

Post November 26th, 2006, 6:15 pm

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 59 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.