LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do you copy a single file or directory into a multiple locations? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-you-copy-a-single-file-or-directory-into-a-multiple-locations-4175524853/)

Alexkeinjas4 11-09-2014 12:40 AM

How do you copy a single file or directory into a multiple locations?
 
Hi, i am a new user to Linux/Unix and the forums. I was wondering what the script would look like to copy a file or directory and have it go into multiple locations such as /tmp /bin /usr /local/ or the home directory? Anything will help

RobertP 11-09-2014 08:14 AM

for f in /bin /usr/bin /home/fred;do echo $f;ls $f|head;done

will loop through the list of directories to do operations on each item of the list referenced by $f. Replace the ls |head part with some copy operation like cp a.sh $f

That will do the operations sequentially. If you have a lot of files and file-systems, it may be faster to do the operations in parallel. In that case replace the ;done with &done.

For information read man bash

It is unusual to do copies like this. Usually, there is one copy made and in the other locations there is a link to the first copy, like this:
for f in a b c;do ln -s /usr/bin/myscript.sh $f/myscript.sh ;done

Further, for executables, one's PATH variable lists likely locations of executables so an executable or link need be found in only one of several directories. One can add some new directory to one's PATH.
echo $PATH
/home/pogson/bin:/usr/local/bin:/usr/bin:/bin:/usr/games
export PATH=/home/pogson/z:$PATH
echo $PATH
/home/pogson/z:/home/pogson/bin:/usr/local/bin:/usr/bin:/bin:/usr/games

This can be done in one's ~/.bashrc file to set the PATH variable on each login.


All times are GMT -5. The time now is 10:14 AM.