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.

Photo

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


  • Please log in to reply
3 replies to this topic

#1 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:54 PM

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
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

BC AdBot (Login to Remove)

 


#2 Andrew

Andrew

    Bleepin' Night Watchman


  • Moderator
  • 8,260 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:Right behind you
  • Local time:09:54 PM

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]

#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:54 PM

Posted 19 July 2009 - 09:31 PM

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

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#4 Andrew

Andrew

    Bleepin' Night Watchman


  • Moderator
  • 8,260 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:Right behind you
  • Local time:09:54 PM

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.

Edited by Amazing Andrew, 20 July 2009 - 12:26 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users