Artificial intelligent assistant

Bash script that removes part of a filename in every file under a directory I have several files under a directory whose names are of the form `*.foo.bar`. I would like to write a bash script to rename each of these files by removing the `.foo` part. For example, suppose that issuing `tree ~/dir/` returns /home/user/dir/ |- george.foo.bar |- john.foo.bar |- paul.foo.bar |- ringo.foo.bar |- subdir |- jimmy.foo.bar |- robert.foo Then running `script ~/foo/` should alter the contents of `~/foo` to /home/user/dir/ |- george.bar |- john.bar |- paul.bar |- ringo.bar |- subdir |- jimmy.bar |- robert.foo

You can use parameter expansion mechanism


shopt -s globstar
for file in **/*foo.bar; do
prefix="${file%.foo*}"
suffix="${file##*.foo}"
mv -v -- "$file" "$prefix$suffix"
done


The `${file%.foo*}` removes matching suffix (leaving only prefix), and `${file#*.foo}` removes prefix (leaving suffix). The double star glob (`**`) is needed to traverse all subdirectories and `setopt -s globstar` allows that glob.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy f1688162f67b821919bab6f6e65b7aed