LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   copying 1 file to many directories (https://www.linuxquestions.org/questions/linux-newbie-8/copying-1-file-to-many-directories-822110/)

lothario 07-25-2010 07:55 PM

copying 1 file to many directories
 
The current directory contains:
  • A file called "original.txt"
  • Many directories called "source_001", "source_002", "source_003" ...
From the command line how do you copy "original.txt" to
"source_001"
and
"source_002"
and
"source_003" ...

The total number of these source directories is unknown, it changes every week.

ghostdog74 07-25-2010 08:57 PM

Code:

for i in source_???
do
  cp original.txt "$i"
done


jefro 07-26-2010 04:30 PM

Could it be done with links instead?

SaintDanBert 07-26-2010 08:56 PM

Quote:

Originally Posted by jefro (Post 4046142)
Could it be done with links instead?

Those in the know understand what you mean by "links". Someone new to *nix, won't know that "links" implies the ln command. Use man ln for details of the command itself.

Here is a short description of "links". Every file system uses some variation of the following. A file exists as a series of bytes on the disk, often called an "extent". A structure called an "inode" keeps track of where the data lives on disk. A folder or directory is just another data file that happens to contain "filenames", some other details, and details about the data extent.

In most cases, there is a one-to-one relationship between what appears in the directory and the data file. Like an entry in a library card index, a "link" makes an entry in some directory for data that lives on the disk somewhere else just like one library book might have dozens of index cards. A given set of data might have a large number of these index-card-like duplicate entries. There is no reason for these links to have identical names, but you need to keep track of them.

A major advantage is that you have one copy of the data regardless of how many directories are used to name it and which users own those directories and link entries.

A major disadvantage of links is that you must remember to clean-up all of them when you delete the associated data file. (I vaguely remember that the fsck family of commands will notice and report and help deal with this issue. I welcome comments from someone who knows.)

One example of link use is the System-V style system initialization.
The system startup scripts get stored in /etc/init.d -- for example, apm, named, networking, samba, gdm, and so on. Then each of the runlevel folders holds a link, for example /etc/rc5.d/S15networking, that then points to the actual script.

One copy of the script content. Edit one -- edit them all. Link to that content each place that you want to use it.

~~~ 0;-Dan

pixellany 07-31-2010 08:37 AM

Moved to Linux--Newbie


All times are GMT -5. The time now is 12:57 AM.