Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Imagine my directory looked like this:


I essentially want to replace every blah.* file with the contents of original. My initial try was to do this:

$ cp original.txt blah.*

But I get:

cp: target ``blah.1.txt' is not a directory

share|improve this question
I don't know much about find, but you could try executing commands through it. – hexafraction Sep 14 '12 at 0:32
up vote 4 down vote accepted

Just one more option with tee:

cat original.txt | tee blah.*

And here's a test case that you can try at home under supervision from a responsible adult:

$ echo "rawr" | tee blah.{1..99}.txt

$ ls
blah.10.txt  blah.26.txt  blah.41.txt  blah.57.txt  blah.72.txt  blah.88.txt
blah.11.txt  blah.27.txt  blah.42.txt  blah.58.txt  blah.73.txt  blah.89.txt
blah.12.txt  blah.28.txt  blah.43.txt  blah.59.txt  blah.74.txt  blah.8.txt
blah.13.txt  blah.29.txt  blah.44.txt  blah.5.txt   blah.75.txt  blah.90.txt
blah.14.txt  blah.2.txt   blah.45.txt  blah.60.txt  blah.76.txt  blah.91.txt
blah.15.txt  blah.30.txt  blah.46.txt  blah.61.txt  blah.77.txt  blah.92.txt
blah.16.txt  blah.31.txt  blah.47.txt  blah.62.txt  blah.78.txt  blah.93.txt
blah.17.txt  blah.32.txt  blah.48.txt  blah.63.txt  blah.79.txt  blah.94.txt
blah.18.txt  blah.33.txt  blah.49.txt  blah.64.txt  blah.7.txt   blah.95.txt
blah.19.txt  blah.34.txt  blah.4.txt   blah.65.txt  blah.80.txt  blah.96.txt
blah.1.txt   blah.35.txt  blah.50.txt  blah.66.txt  blah.81.txt  blah.97.txt
blah.20.txt  blah.36.txt  blah.51.txt  blah.67.txt  blah.82.txt  blah.98.txt
blah.21.txt  blah.37.txt  blah.52.txt  blah.68.txt  blah.83.txt  blah.99.txt
blah.22.txt  blah.38.txt  blah.53.txt  blah.69.txt  blah.84.txt  blah.9.txt
blah.23.txt  blah.39.txt  blah.54.txt  blah.6.txt   blah.85.txt
blah.24.txt  blah.3.txt   blah.55.txt  blah.70.txt  blah.86.txt
blah.25.txt  blah.40.txt  blah.56.txt  blah.71.txt  blah.87.txt

$ cat blah.67.txt 

$ echo "hooray" > original.txt

$ cat original.txt | tee blah.*.txt

$ cat blah.67.txt 
share|improve this answer
Selected for its simplicity :) Thanks @Oli – Nathan J. Brauer Sep 14 '12 at 2:39
+1 because man -a tee – Ryan O'Hara Sep 14 '12 at 3:34
And here's a test case that you can try at home under supervision from a responsible adult -- I don't think I saw this before. Brilliant! – Nathan J. Brauer Feb 12 '13 at 9:36

Use bash's for (assuming you're using bash):

for file in blah.*.txt; do
    cp original.txt "$file"
share|improve this answer
Yeah that's my bad. I thought something else was causing the initial error. – Oli Sep 14 '12 at 10:12

You could try this, avoiding for-loops and keeping things in one line:

ls blah.* | xargs -n 1 cp original

You are getting cp: target blah.1.txt is not a directory in your original attempt, because cp'ing multiple files assumes moving all of them to the directory provided as the last argument. It's simply wrong syntax. Quoting man cp:

       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... -t DIRECTORY SOURCE...

       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Solely using cp won't help you out. See also this question.

share|improve this answer

Do this:

for i in `ls *.txt`; do cat original > $i; done

where original is the text file. cat just dumps the contents and the > pipes the contents into a new file represented by $i (expanded from the list returned by the ls *.txt command)

Note this will completely overwrite those files so you may want to put back them up first in another directory. This will do it for anything with a .txt extension too (including the original file if it is a .txt extension) so you may want to rename things or use a better convention (e.g. blah* instead of *.txt)

share|improve this answer

Here's another option using find:

find ./ -name 'blah.*.txt' -exec cp original.txt {} \;

You're asking find to start in the current directory and find files whose name matches the pattern (in quotes, to avoid the shell expanding it and letting find do the pattern-expansion), and for each file found, execute the command between -exec and \;. In this command, {} is replaced by the found file's name.

Look at this answer for a good deal of information on using find effectively:

How can I use find command more efficiently?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.