<p>Say I have three files (template_*.txt):</p>

<ul>
<li>template_x.txt</li>
<li>template_y.txt</li>
<li>template_z.txt</li>
</ul>

<p>I want to copy them to three new files (foo_*.txt). </p>

<ul>
<li>foo_x.txt </li>
<li>foo_y.txt </li>
<li>foo_z.txt</li>
</ul>

<p>Is there some simple way to do that with one command, e.g. </p>

<p><code>cp --enableAwesomeness template_*.txt foo_*.txt</code></p>

[{'answer_id': 26439, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 1, 'selected': False, 'text': '<p>I don\'t know of anything in bash or on cp, but there are simple ways to do this sort of thing using (for example) a perl script:</p>\n\n<pre><code>($op = shift) || die "Usage: rename perlexpr [filenames]\\n";\n\nfor (@ARGV) {\n    $was = $_;\n    eval $op;\n    die $@ if $@;\n    rename($was,$_) unless $was eq $_;\n}\n</code></pre>\n\n<p>Then:</p>\n\n<pre><code>rename s/template/foo/ *.txt\n</code></pre>\n'}, {'answer_id': 26441, 'author': 'pauldoo', 'author_id': 755, 'author_profile': 'https://Stackoverflow.com/users/755', 'pm_score': 1, 'selected': False, 'text': '<pre><code>for i in template_*.txt; do cp -v "$i" "`echo $i | sed \'s%^template_%foo_%\'`"; done\n</code></pre>\n\n<p>Probably breaks if your filenames have funky characters in them.  Remove the \'-v\' when (if) you get confidence that it works reliably.</p>\n'}, {'answer_id': 26447, 'author': 'Chris Bartow', 'author_id': 497, 'author_profile': 'https://Stackoverflow.com/users/497', 'pm_score': 2, 'selected': False, 'text': "<p>This should work:</p>\n\n<pre><code>for file in template_*.txt ; do cp $file `echo $file | sed 's/template_\\(.*\\)/foo_\\1/'` ; done\n</code></pre>\n"}, {'answer_id': 26451, 'author': 'Matt McMinn', 'author_id': 1322, 'author_profile': 'https://Stackoverflow.com/users/1322', 'pm_score': 2, 'selected': False, 'text': '<pre><code>[01:22 PM] matt@Lunchbox:~/tmp/ba$\nls\ntemplate_x.txt  template_y.txt  template_z.txt\n\n[01:22 PM] matt@Lunchbox:~/tmp/ba$\nfor i in template_*.txt ; do mv $i foo${i:8}; done\n\n[01:22 PM] matt@Lunchbox:~/tmp/ba$\nls\nfoo_x.txt  foo_y.txt  foo_z.txt\n</code></pre>\n'}, {'answer_id': 26456, 'author': 'Chris Conway', 'author_id': 1412, 'author_profile': 'https://Stackoverflow.com/users/1412', 'pm_score': 5, 'selected': True, 'text': '<pre>\nfor f in template_*.txt; do cp $f foo_${f#template_}; done\n</pre>\n'}, {'answer_id': 31349, 'author': 'Jon Ericson', 'author_id': 1438, 'author_profile': 'https://Stackoverflow.com/users/1438', 'pm_score': 0, 'selected': False, 'text': '<p>Yet another way to do it:</p>\n\n<pre><code>$ ls template_*.txt | sed -e \'s/^template\\(.*\\)$/cp template\\1 foo\\1/\' | ksh -sx\n</code></pre>\n\n<p>I\'ve always been impressed with the ImageMagick <a href="http://www.imagemagick.org/script/convert.php" rel="nofollow noreferrer">convert</a> program that does what you expect with image formats:</p>\n\n<pre><code>$ convert rose.jpg rose.png\n</code></pre>\n\n<p>It has a sister program that allows batch conversions:</p>\n\n<pre><code>$ mogrify -format png *.jpg\n</code></pre>\n\n<p>Obviously these are limited to image conversions, but they have interesting command line  interfaces.</p>\n'}, {'answer_id': 63980, 'author': 'Bruno De Fraine', 'author_id': 6918, 'author_profile': 'https://Stackoverflow.com/users/6918', 'pm_score': 1, 'selected': False, 'text': '<p>The command <code>mmv</code> (available in <a href="http://packages.debian.org/etch/mmv" rel="nofollow noreferrer">Debian</a> or <a href="http://pdb.finkproject.org/pdb/package.php/mmv" rel="nofollow noreferrer">Fink</a> or easy to compile yourself) was created precisely for this task. With the plain Bash solution, I always have to look up the documentation about variable expansion. But <code>mmv</code> is much simpler to use, quite close to "awesomeness"! ;-)</p>\n\n<p>Your example would be:</p>\n\n<pre><code>mcp "template_*.txt" "foo_#1.txt"\n</code></pre>\n\n<p><code>mmv</code> can handle more complex patterns as well and it has some sanity checks, for example, it will make sure none of the files in the destination set appear in the source set (so you can\'t accidentally overwrite files).</p>\n'}, {'answer_id': 89102, 'author': 'Roberto Bonvallet', 'author_id': 13169, 'author_profile': 'https://Stackoverflow.com/users/13169', 'pm_score': 2, 'selected': False, 'text': '<p>My preferred way:</p>\n\n<pre><code>for f in template_*.txt\ndo\n  cp $f ${f/template/foo}\ndone\n</code></pre>\n\n<p>The "I-don\'t-remember-the-substitution-syntax" way:</p>\n\n<pre><code>for i in x y z\ndo\n  cp template_$i foo_$\ndone\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/26433', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/437/']