BleepingComputer.com: Copy a File to Root of all Directories in a Directory Tree

Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Copy a File to Root of all Directories in a Directory Tree

#1 User is offline   Billy O'Neal 

  • Bleepin Engineer
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,086
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

Posted 18 July 2009 - 08:47 PM

Hello :thumbsup:

Does Linux (Specifically, Debian) provide an easy way to copy a file to the root of all directories in a folder path? I've got a script for my webserver that shows a pretty directory listing. Example -> http://billy-oneal.com/forums/

I've got a ton of folders to place that script in. Is there an easy way of doing that?

Billy3

#2 User is offline   Andrew 

  • Bleepin' Night Watchman
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 7,329
  • Joined: 05-December 05
  • Gender:Not Telling
  • Location:Right behind you

Posted 19 July 2009 - 08:03 PM

You could try this from the shell:

for i in `ls -R`; do echo $i; if [ -d $i ]; then echo "copying to $i"; cp myscript.sh $i; echo "copy was a success"; fi; done


where myscript.sh is your script.

[Source]
Help us help you. If HelpBot replies, you MUST follow step 1 in its reply so we know you need help.
Posted Image
Boredom Software Stop Highlighting Things

#3 User is offline   Billy O'Neal 

  • Bleepin Engineer
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,086
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

Posted 19 July 2009 - 09:31 PM

Thank you Andrew :thumbsup: You're Amazing :flowers:

Billy3

#4 User is offline   Andrew 

  • Bleepin' Night Watchman
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 7,329
  • Joined: 05-December 05
  • Gender:Not Telling
  • Location:Right behind you

Posted 20 July 2009 - 12:16 AM

Okay, I tested it and the above does NOT work correctly. It will only copy to the first level of subdirectories. Execute this command form the root directory of your tree:

find . -type d -exec cp myscript.sh {} \;


Where myscript.sh is the file being copied.

WARNING: This will overwrite any file named myscript.sh that already exists in a directory without warning!

----
Anatomy of the command:

find - obviously, the find command finds stuff
. - in Linux, the . character represents the current directory. So, we're saying "find in the current directory".
-type d - Type of file is directory, that is "Find all directories in the current directory."
-exec - execute the following on the results of the search
cp - the copy command
myscript.sh - the file to be copied.

So, the command reads:

Find all directories within the current directory and copy the file called myscript.sh into them.

This post has been edited by Amazing Andrew: 20 July 2009 - 12:26 AM

Help us help you. If HelpBot replies, you MUST follow step 1 in its reply so we know you need help.
Posted Image
Boredom Software Stop Highlighting Things

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users