Use a loop and copy each file individually. To compute the new name you can use sed
or bash's built-in parameter expansion.
Both of the following scripts assume that you don't have any directories starting with Pep
.
The following script copies each file starting with Pep
in the working directory. The new names will have the first Pep
removed. The -n
option avoids accidentally overwriting of files.
for f in Pep*; done
cp -n "$f" "${f#Pep}"
done
To include sub-directories you can use bash's globstar built-in.
shopt -s globstar nullglob
for f in ./path/to/main/dir/**/Pep*; do
cp -n "$f" "${f//\/Pep/}"
done
Always start the path with ./
or /
so that the script can distinguish between Pep
at the start of a filename and Pep
inside a filename, for instance Pep do not replace Pep in the middle.txt
.
And here's an equivalent script that should work in every POSIX shell, including ancient bash versions as the one on Macs.
find path/to/your/main/directory -type f -name Pep\* -exec sh -c \
'echo cp -n "$0" "$(printf %s "$0" | sed -E "s:(.*/)Pep:\1:")"' {} \;