LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   copying file/folder into subdirectories (https://www.linuxquestions.org/questions/linux-newbie-8/copying-file-folder-into-subdirectories-923314/)

AngelDeaD 01-11-2012 03:05 PM

copying file/folder into subdirectories
 
I have over 50 folder in home (using Debian 6 LAMP).

/home/xcz1/xzcx2/name1/name2
/home/zsd1/zd2/name1/name2
/home/.../.../name1/name2
...


/home/update/name2/

how can i copy the name2 from update folder and paste him into the name1 folder of each folder in the home folder?

i have tried:
cd /home/update
cp -R name2 /home/*/*/name1
and
cp -r name2 /home/../../name1
but it did not work.

Thank's

hydraMax 01-11-2012 08:04 PM

Your request (or at least the way you are explaining it) is somwhat odd, and a bit vague or ambiguous. Can you perhaps give us some more background on what you are trying to accomplish?

AngelDeaD 01-11-2012 08:20 PM

Quote:

Originally Posted by hydraMax (Post 4572337)
Your request (or at least the way you are explaining it) is somwhat odd, and a bit vague or ambiguous. Can you perhaps give us some more background on what you are trying to accomplish?

I'm trying to input few files in subfolders with name 'name1'.
These files are not there, so overwrite is not needed.

or as simple as possible:
It's game hosting machine, these 50 folders contain the files and folders of the game, so i want to input some files, which block exploits and etc.
how i gonna do it?

Sorry for my bad English.

john_erlandsson 01-11-2012 08:30 PM

If i understand you correctly, you want to copy ~/src/dir/file to ~/dst/dir/file

Could the problem be that the ~/dst/dir/ folder doesent exist?

In that case you could either just mkdir ~/dst/dir/ or do some fancy find thing.

AngelDeaD 01-11-2012 08:42 PM

Quote:

Originally Posted by john_erlandsson (Post 4572359)
If i understand you correctly, you want to copy ~/src/dir/file to ~/dst/dir/file

Could the problem be that the ~/dst/dir/ folder doesent exist?

In that case you could either just mkdir ~/dst/dir/ or do some fancy find thing.


All directories has been already created, but i need files on the right position, so i want input them to one directory named as 'dir' and input them (whole 'dir' folder) in an subdirectory where is the directory with name 'dir'.
But i don't want input it in just one directory, i want input it in all directory in the 'home' dir. (all those directories have created 'dir' directory).

for example:
/home/michael/today/dir
/home/carey/evening/dir
/home/jay/birthday/dir

and i want input 'dir' folder from
/home/update/dir
to those above.
(Please do not tell me, i have to do it one by one, it must be some trick with 'cp' command or something like that.

hydraMax 01-11-2012 11:37 PM

I don't know if there is a single command that can do that, but I think something like this shell script should work:

Code:

XYZ=`ls /home/*/*/dir -d`
for i in $XYZ
do
    cp -R /home/update/dir/* $i
done

I tested it in a directory on my system (leaving off the leading forward slashes, for obvious reasons). I started with this directory:

Code:

.
|-- home
|  |-- carey
|  |  `-- evening
|  |      `-- dir
|  |-- jay
|  |  `-- birthday
|  |      `-- dir
|  |-- michael
|  |  `-- today
|  |      `-- dir
|  `-- update
|      `-- dir
|          `-- update-file.txt
`-- mcopy.sh

(mcopy.sh is the shell script) I ran the shell script with "bash mcopy.sh". And the result was:

Code:

|-- home
|  |-- carey
|  |  `-- evening
|  |      `-- dir
|  |          `-- update-file.txt
|  |-- jay
|  |  `-- birthday
|  |      `-- dir
|  |          `-- update-file.txt
|  |-- michael
|  |  `-- today
|  |      `-- dir
|  |          `-- update-file.txt
|  `-- update
|      `-- dir
|          `-- update-file.txt
`-- mcopy.sh


jschiwal 01-11-2012 11:58 PM

You will need to use a loop.

Code:

for dir in /home/*/*/dir1; do
cp name2 "$dir"/
done


AngelDeaD 01-12-2012 06:33 AM

Quote:

Originally Posted by hydraMax (Post 4572458)
I don't know if there is a single command that can do that, but I think something like this shell script should work:

Code:

XYZ=`ls /home/*/*/dir -d`
for i in $XYZ
do
    cp -R /home/update/dir/* $i
done

I tested it in a directory on my system (leaving off the leading forward slashes, for obvious reasons). I started with this directory:

Code:

.
|-- home
|  |-- carey
|  |  `-- evening
|  |      `-- dir
|  |-- jay
|  |  `-- birthday
|  |      `-- dir
|  |-- michael
|  |  `-- today
|  |      `-- dir
|  `-- update
|      `-- dir
|          `-- update-file.txt
`-- mcopy.sh

(mcopy.sh is the shell script) I ran the shell script with "bash mcopy.sh". And the result was:

Code:

|-- home
|  |-- carey
|  |  `-- evening
|  |      `-- dir
|  |          `-- update-file.txt
|  |-- jay
|  |  `-- birthday
|  |      `-- dir
|  |          `-- update-file.txt
|  |-- michael
|  |  `-- today
|  |      `-- dir
|  |          `-- update-file.txt
|  `-- update
|      `-- dir
|          `-- update-file.txt
`-- mcopy.sh


i make script and run her on this way:

./mcopy.sh

it's work, but when finish show this error:
p: missing destination file operand after `/home/update/dir/subdirectory'
Try `cp --help' for more information.

It's not so mather, beacause it's work :D
Thank you.


Quote:

Originally Posted by jschiwal (Post 4572469)
You will need to use a loop.

Code:

for dir in /home/*/*/dir1; do
cp name2 "$dir"/
done


i try your script, but it's say direcyory 'name2' doesnt exist, i put 'dir' instead of 'name2'. No errors and nothing happend.
Thank's anyway.

jschiwal 01-13-2012 01:32 AM

name2 is from your original post. I didn't enter a script to run but an example of the commands to run in the terminal.

Since you are copying into several users' home directories, you will need to run the cp command as root.

1) The first argument of cp is the file to copy. If you are copying a directory, then use the -r flag. It is to recurse through the source, not the destination. Be in the same directory as the source file, or include the path to it in the filename.

2) Wildcards are expanded before the command is run. /home/*/*/dir1 will expand to a list of directories named "dir1" two levels deep from each users home directory. Such as /home/mike/www/dir1/ for example. I think it would be better to explicitly give which directory it is, and use a variable for the user name:

Code:

for user in sam mike sally ted; do
sudo cp file /home/$user/www/dir1/
done

Or if more than a few users:
Code:

for homedir in /home/*; do
sudo cp file $homedir/http/dir1/
done

Use the particular subdirectories you

need, and watch what you do.


All times are GMT -5. The time now is 07:51 PM.