LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-18-2010, 05:05 AM   #1
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Rep: Reputation: 15
How to copy a file to all subfolders in a directory using a single command?


I'd like to copy a file, say widgets/water.txt, to all subfolders in the folder widgets using a single command. So if the folder widgets has 10 subfolders like widgets/blue, widgets/green, etc. I'd like to copy water.txt to all of them with one command.

I tried the commands
Code:
cp water.txt ./*/water.txt
cp water.txt ./*/
However these don't seem to work. The latter gives 'cp: omitting directory' errors. What is the right way to do this?
 
Old 07-18-2010, 05:27 AM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
depends on what you mean by "with a single command". It is possible with a single commandline, using the for command (in most shells a builtin):
Code:
for d in */; do cp water.txt "$d"; done
PS: the reason your command doesn't work is that '*/' is expanded by the shell before invoking cp, and cp interprets only the LAST argument (in that case the last subdir found by your shell) as the target to copy to, so that's the reason for the "directory omitted" message, it refuses to copy directories without the -r switch.

The "for" command does the right thing by invoking cp individually for each target directory.

Last edited by zirias; 07-18-2010 at 05:32 AM. Reason: further explanation
 
1 members found this post helpful.
Old 07-18-2010, 07:55 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Something like 'find ${PWD}/* -type d -print0 | xargs -0 -iD cp /path/to/water.txt 'D';'?
 
Old 07-18-2010, 08:35 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by SirTristan View Post
I'd like to copy a file, say widgets/water.txt, to all subfolders in the folder widgets using a single command. So if the folder widgets has 10 subfolders like widgets/blue, widgets/green, etc. I'd like to copy water.txt to all of them with one command.

I tried the commands
Code:
cp water.txt ./*/water.txt
cp water.txt ./*/
However these don't seem to work. The latter gives 'cp: omitting directory' errors. What is the right way to do this?
It's not possible with a single command. The cp command copies all but the last in the list of files to the last item, which specifies the directory. And remember that the shell, not cp, expands the wildcards based on existing files.

Code:
for i in * # iterate over all files in current dir
do
	if [ -d "$i" ] # if it's a directory
	then
		cp water.txt "$i" # copy water.txt into it
	fi
done
The find command won't work because it searches in subdirectories.

Last edited by MTK358; 07-18-2010 at 08:41 AM.
 
Old 07-18-2010, 08:52 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Quote:
Originally Posted by MTK358 View Post
The find command won't work because it searches in subdirectories.
As long as the OP is in the /path/to/widgets/ directory it'll work. If it doesn't then do show it doesn't by posting output.
 
Old 07-18-2010, 09:24 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
But i don't think the OP wants water.txt in all the subdirectories and their subderectories, too.
 
Old 07-18-2010, 09:58 AM   #7
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
What's this silly argument good for? I don't think the OP wanted to copy it recursively into a directory tree, so i didn't use find -- finding all directories in the current directory is done fine by the shell using the pattern '*/'.

But, in case you actually WANT all subdirectories recursively, find is the way to go, as posted by unSpawn.

Matching all files with '*' and later checking wheter it is a directory doesn't make sense.

And, as I already explained, it's not possible using a single command, there are always at least two commands involved, BUT it is very well possible using a single line.
 
Old 07-19-2010, 12:19 AM   #8
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks guys

Yes zirias is right, I didn't want to copy to the whole directory tree, just the direct subfolders. zirias' code works great:
Code:
for d in */; do cp wp-salesengine-page.php "$d"; done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to make a new directory and then cd into with single command ? mynameisthomas Linux - Newbie 4 09-13-2009 02:29 PM
command to copy a directory ceantuco Linux - Newbie 6 09-17-2008 05:09 AM
Copy single file to multiple locations - What am I doing wrong decartes Linux - Newbie 7 04-26-2008 05:09 PM
Copying a single file into each directory of a directory tree mlapl1 Linux - Newbie 2 06-27-2007 11:18 PM
copy all of one filetype found by locate command to a single directory socratesone Linux - Software 3 06-22-2005 02:23 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration