Modifying replace string in xargs Modifying replace string in xargs bash bash

Modifying replace string in xargs


The following command constructs the move command with xargs, replaces the second occurrence of '.' with '.bar.', then executes the commands with bash, working on mac OSX.

ls *.txt | xargs -I {} echo mv {} foo/{} | sed 's/\./.bar./2' | bash


It is possible to do this in one pass (tested in GNU) avoiding the use of the temporary variable assignments

find . -name "*.txt" | xargs -I{} sh -c 'mv "$1" "foo/$(basename ${1%.*}).new.${1##*.}"' -- {}


In cases like this, a while loop would be more readable:

find . -name "*.txt" | while IFS= read -r pathname; do    base=$(basename "$pathname"); name=${base%.*}; ext=${base##*.}    mv "$pathname" "foo/${name}.bar.${ext}"done

Note that you may find files with the same name in different subdirectories. Are you OK with duplicates being over-written by mv?