change *.foo to *.bar in unix one-liner change *.foo to *.bar in unix one-liner unix unix

change *.foo to *.bar in unix one-liner


Although basename can work on file extensions, using the shell parameter expansion features is easier:

for file in *.foo; do mv "$file" "${file%.foo}.bar"; done

Your code with basename doesn't work because the basename is only run once, and then xargs just sees {}.bar each time.


for file in *.foo ; do mv $file echo $file | sed 's/\(.*\.\)foo/\1bar/' ; done

Example:

$ ls1.foo  2.foo$ for file in *.foo ; do mv $file `echo $file | sed 's/\(.*\.\)foo/\1bar/'` ; done$ ls1.bar  2.bar$ 


for x in $(find . -name "*.foo"); do mv $x ${x%%foo}bar; done