LinuxQuestions.org
Visit Jeremy's Blog.
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-02-2010, 04:43 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Copy the contents of a txt file to other txt files (with similar names) by cp command


Code:
cp -r aa123.h aa*.h
results in
Code:
cp: target `aa456.h' is not a directory
Yes I read man page cp (1p). There is something written there about it, I couldn't understand though.

Kindly guide.
 
Old 07-02-2010, 04:58 AM   #2
brianL
LQ 5k Club
 
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Slackware & Slackware64 14.1
Posts: 7,039
Blog Entries: 52

Rep: Reputation: Disabled
Quote:
-R, -r, --recursive
copy directories recursively
That could be the problem: -r applies to directories.
 
Old 07-02-2010, 05:00 AM   #3
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by brianL View Post
That could be the problem: -r applies to directories.
Thanks for bothering.
Just tried it without -r, same error repeats.
 
Old 07-02-2010, 05:08 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2014.08, Slack14.1 DebWheezy, +50+ other Linux OS, for test only.
Posts: 13,924

Rep: Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587Reputation: 2587
cp cannot be used to add text to another file.
The cat command should be used.
cat = Concatenate
http://publib.boulder.ibm.com/infoce.../rzahz/cat.htm
http://www.linfo.org/cat.html

Say you do 'cp -f aa123.h aa456.h' :
aa456.h will disappear / be replaced by aa123.h
.....
 
Old 07-02-2010, 05:13 AM   #5
rical
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 1
This might work (try at your own risk).

Code:
cat aa123.h >> aa*.h
This command will append (to the end) the content of aa123.h to all files that matches aa*.h
 
Old 07-02-2010, 05:37 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Quote:
cat aa123.h >> aa*.h
This won't work. Bash will first expand aa*.h to all it finds. If more then 1 file is found (at least 2 are found in this example: aa123.h and aa456.h), you get an ambiguous redirect.

This works: for THISFILE in aa*.h; do cat aa123.h >> $THISFILE; done

But be careful! aa*.h will also include aa123.h. In the above on-liner it will be picked up (cat: aa123.h: input file is output file), but depending on what commands you use, you could end up with an empty file.

Hope this helps.
 
Old 07-02-2010, 07:58 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by anishakaul View Post
Code:
cp -r aa123.h aa*.h
results in
Code:
cp: target `aa456.h' is not a directory
Yes I read man page cp (1p). There is something written there about it, I couldn't understand though.

Kindly guide.
Let's say that the current dir has these files:

Code:
aa123.h aa456.h aa789.h
When you run this:

Code:
cp aa123.h aa*.h
It expands to this (the names the wildcard expanded to are in bold):

Code:
cp aa123.h aa123.h aa456.h aa789.h
Since there are multiple source files, that means that you want to copy them into a directory. Because the last file in the list should be the destination directory, it fails because it isn't.

Not to mension that two of the source files are the same.

Last edited by MTK358; 07-02-2010 at 07:59 AM.
 
Old 07-03-2010, 12:54 AM   #8
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
You could use a small script to make sure you don't loose the original file

Code:
cp aa123.h temp
for i in aa*.h
do
cat temp >> $i
done
cp temp aa123.h

Last edited by bsat; 07-03-2010 at 12:56 AM.
 
  


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
Using key to match against source.txt file to add xml tags to names in Perl ginny2010 Programming 3 06-22-2010 01:16 PM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
bash command for make difference between two txt file kkpal Linux - Newbie 7 05-12-2008 07:07 AM
shell script to move files from one system to another sytem with file names in a txt coral_km Linux - Newbie 3 02-13-2008 10:23 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration