LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy the contents of a txt file to other txt files (with similar names) by cp command (https://www.linuxquestions.org/questions/linux-newbie-8/copy-the-contents-of-a-txt-file-to-other-txt-files-with-similar-names-by-cp-command-817592/)

Aquarius_Girl 07-02-2010 04:43 AM

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.

brianL 07-02-2010 04:58 AM

Quote:

-R, -r, --recursive
copy directories recursively
That could be the problem: -r applies to directories.

Aquarius_Girl 07-02-2010 05:00 AM

Quote:

Originally Posted by brianL (Post 4021514)
That could be the problem: -r applies to directories.

Thanks for bothering.
Just tried it without -r, same error repeats.

knudfl 07-02-2010 05:08 AM

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
.....

rical 07-02-2010 05:13 AM

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

druuna 07-02-2010 05:37 AM

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.

MTK358 07-02-2010 07:58 AM

Quote:

Originally Posted by anishakaul (Post 4021496)
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.

bsat 07-03-2010 12:54 AM

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



All times are GMT -5. The time now is 06:19 PM.