Imagine my directory looked like this:
./original.txt
./blah.1.txt
./blah.2.txt
[...]
./blah.999999.txt
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
Just one more option with
tee
:And here's a test case that you can try at home under supervision from a responsible adult:
Use bash's
for
(assuming you're using bash):You could try this, avoiding for-loops and keeping things in one line:
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. Quotingman cp
:Solely using
cp
won't help you out. See also this question.Do this:
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 thels *.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)
Here's another option using
find
: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?