Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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.
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
Thank you.
Quote:
Originally Posted by jschiwal
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.