LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-10-2009, 11:49 AM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
Question 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.
 
Old 07-10-2009, 12:16 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
It's crude but "for dir in `eval echo dir*`; do Something; done" could do.
 
Old 07-10-2009, 12:34 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Try
Code:
eval 'cp file1 dir*'{1..5}'*;'
 
Old 07-10-2009, 12:40 PM   #4
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by unSpawn View Post
It's crude but "for dir in `eval echo dir*`; do Something; done" could do.
Very crude indeed . 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?
 
Old 07-10-2009, 01:12 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 07-10-2009, 01:14 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

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


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: a little vi trick LXer Syndicated Linux News 0 04-29-2009 03:40 AM
vim :gui trick and undo-trick dazdaz Linux - Software 3 09-10-2007 02:45 PM
Trick Yum volvogga Linux - Newbie 4 08-18-2005 05:53 PM
2 problem trick felipe_ced Linux - Newbie 1 08-20-2003 10:57 AM
FDISK didn't do the trick d3funct Linux - General 8 11-08-2001 04:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration