Solved

UNIX - Copy one file into every directory on the box

Posted on 2012-03-19
6
303 Views
Last Modified: 2012-03-22
Assuming root access, what would be a simple way to copy a single file into each directory on the box.  So if there were 1,000 directories, there would then be 1,000 copies of the file.

Conversely, what would be the simple way to remove the same files.
0
Comment
Question by:MajorBigDeal
6 Comments
 
LVL 77

Accepted Solution

by:
arnold earned 250 total points
ID: 37739696
Do you really want 1000 copies of the same file, or would using hard links reference the same file in multiple locations?
Find . -type d | while read a; do
The -d tells it to find directories.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 37739710
find / -type d -exec cp singlefile {} \;

find / -type name singlefile -exec rm {} \;
0
 
LVL 77

Assisted Solution

by:arnold
arnold earned 250 total points
ID: 37739857
The exec method will error out because of too many elements being passed. Using xargs might be a better choice.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 6

Assisted Solution

by:Tomunique
Tomunique earned 125 total points
ID: 37741927
@arnold    find will only pass 1 arg at a time, so exec will be fine.  potentially eat a lot of disk space, but work.  

hard/soft links would be better if the file isn't going to be changing, Identifying the file-system splits would vary depending on OS, (unless you issued a df for each directory to see where it lived.... but, this would be nuts on large systems).
0
 
LVL 11

Author Comment

by:MajorBigDeal
ID: 37743789
Thanks for the great comments - I really do need multiple copies of the same file, not links.  This is a testing situation.
0
 
LVL 11

Author Closing Comment

by:MajorBigDeal
ID: 37755310
Thanks!
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question