LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How can I do this with a BASH trick? (https://www.linuxquestions.org/questions/linux-general-1/how-can-i-do-this-with-a-bash-trick-739223/)

the_gripmaster 07-10-2009 11:49 AM

How can I do this with a BASH trick?
 
I have several directories and a file residing inside a directory:
Code:

$ ls
dir1 directory2 direc3 dir4 dirs5 file1

Now I want to place a copy of file1 in every directory (dir1, directory2, direc3, dir4, dirs5). How do I do this with 1 simple bash command, if possible?

Thanks in advance.

unSpawn 07-10-2009 12:16 PM

It's crude but "for dir in `eval echo dir*`; do Something; done" could do.

Kenhelm 07-10-2009 12:34 PM

Try
Code:

eval 'cp file1 dir*'{1..5}'*;'

the_gripmaster 07-10-2009 12:40 PM

Quote:

Originally Posted by unSpawn (Post 3603532)
It's crude but "for dir in `eval echo dir*`; do Something; done" could do.

Very crude indeed :D. But what if the directories don't start with dir*? I mean like this:

Code:

$ ls
folder mydir directory metallica_songs file1

If one line trick is not possible, what are the other alternatives?

David the H. 07-10-2009 01:12 PM

Try

Code:

find . -type d -exec cp file1 '{}' \;
It should find every directory under the current one and run the copy command on each one.

You can include '-maxdepth 2' if you want to limit the number of subdirectory levels it will descend into.

unSpawn 07-10-2009 01:14 PM

If they're in the same level then you'd prolly 'find ./* -maxdepth 0 -type d|xargs doSomething' or something.


All times are GMT -5. The time now is 09:25 PM.