Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I want to do something like take the part of a wildcard match and use it similarly to a regex match. The command might look something like this

cat foo_* | python > magic_*

If my file names are


I want the output to be in files named


effectively changing the file names from foo_* to magic_*.

Is it possible to do this in a shell command? I'm using bash, but an answer for other shells is fine too.

share|improve this question
up vote 5 down vote accepted

If I understand what you're trying to do, you can accomplish it but it takes a bit more work:

for f in foo_*; do python <"$f" >"${f/foo/magic}"; done

(Note: I also replaced the useless use of cat with a < redirect.)

share|improve this answer

Something just a little different than Gordon's excellent answer:

for sfx in "${suffixes[@]}"; do
    python < "foo_$sfx"  > "magic_$sfx"
share|improve this answer
$ touch foo_{biz,baz}

$ ls foo_*
foo_baz  foo_biz

$ printf "%s\n" foo_* | sed 's/^foo_/magic_/'
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.