recursive cp?

  • lioness
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1615

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.

/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?
  • 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;
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
  • 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 7 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
 

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