SylvainPineau In fact there are 3: one is find and for two times in this case cp. Not to say that find need a lot of time. If the definition of a "command" is an "exec" call, maybe, but this is "one command" in my book.
I keep coming back to this one, so thought I should give an upvote :D — Jonathan. Show 4 more comments. Steve Barnes 4 4 bronze badges. Rmano Rmano For the passing reader: According to the spec , a for loop is a compound command.
Therefore this is still one command, and meets all the requirements for the question. You can create a help script , or you can do it with xargs and a print function in this case, echo : echo firstDir secondDir xargs -n 1 cp test This will make each directory as an argument to the cp function , using test file as a parameter. Evandro Silva Evandro Silva 8, 6 6 gold badges 34 34 silver badges 44 44 bronze badges. After a long search this work like a Charm also!
I share it hope it helps others too. Extended globbing is one of the reason I like it over bash. See zsh. It should be voted much higher! If you want to copy the file test. Just thought to give a variation to the answer of Sylvain Pineau where dir1 and dir2 are not in your current directory.
Joshi Joshi 3 3 silver badges 15 15 bronze badges. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked You could also use tar -u if you can deal with files not being automatically deleted from the tarball.
Along the lines of the previous answers, but with the relatively modern multiple FROM support:. You can change the src stage's base image to whatever, but it needs to have the mv binary, obviously. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Copy multiple directories with one command Ask Question. Asked 5 years, 7 months ago.
Active 4 months ago. Viewed 40k times. Improve this question. Claudiu Claudiu k gold badges silver badges bronze badges. Documented behavior. As it turns out, reinventing the wheel is counter-productive. Add a comment. Viewed k times. Is it possible to copy a single file to multiple directories using the cp command? But is it possible using the gnu cp command? Improve this question. Tom Feiner Tom Feiner Add a comment. Active Oldest Votes. You can't do this with cp alone but you can combine cp with xargs : echo dir1 dir2 dir3 xargs -n 1 cp file1 Will copy file1 to dir1 , dir2 , and dir3.
Improve this answer. Robert Gamble Robert Gamble k 24 24 gold badges silver badges bronze badges. More information on xargs: cyberciti. Simon how so? NickC The asker specified : "I know it's possible using a for loop, or find. Stipulating that answers be accepted based on whether they technically answer the question would be insufferably pedantic and not really useful. Very helpful! For other beginners like me: you can't use regex with cp, but you can in first part of this command e.
Show 7 more comments. Thanks for the answer! Now that I think about it a bit more, without extra flags which do not exist cp will not know what is the source and what is the DEST dir. This is not the correct answer. Please see Robert Gamble's answer. Nocturne: which part of what I said is incorrect? Robert gives an example of using another tool to invoke cp multiple times as I suggest. There are many possible solutions that involve using other tools or constructs to do that, but the OP did indicate he was already aware that is possible.
Sad, it would be great for cp and scp to support that feature. Yet we can use tee to do the split. I think PlagueHammer wants to say that in this context the other answer is more useful. Perhaps you may include something like that in your answer too? Wildcards also work with Roberts code echo. Paul Paul 6 6 silver badges 2 2 bronze badges. Used it to quickly propagate Finder folder view settings which live in a. Copied to all destinations except to itself luckily.
Coping the. But that's another issue. Alex Telon 12 12 silver badges 29 29 bronze badges. A cursory test I just ran shows this to be the best performer if your destinations are different physical drives keeping two physical copies of a file base like source video for a digital film.
If you're using bash 3. As far as I can see it you can use the following: ls xargs -n 1 cp -i file. Marvin Pinto Evgeny Evgeny 2 2 silver badges 5 5 bronze badges. While it is good to have added the -i guard, won't that prompt you about over writing every file? Hedgehog Well, I think that this command is useful when there is a file in the directory and one wants to copy it to subdirectories of the directory. In this case a file can be overwritten only once. Thanks for your question!
0コメント