I want to write a script to find all the files ending with .cpp
and rename them as .cc
. The search will be done recursively from a start route R, which is the script's argument.
If no argument is written, R will be the actual directory.
And I have written:
#!/bin/bash
R=.
if [[ $# == 1 ]]
then
echo "$# == 1"
if [[ -d $1 ]]
echo "$1 is directory"
then
R=$1
else
printf "Error $1 should be a directory"
exit 1
fi
find $R -name "*.cpp" -exec sh -c 'mv {} $(dirname {})${$(basename {})%.cpp}".cc" ' \;
exit 0
else
printf "Invocation is: $0 directory"
exit 1
fi
But I know I have trouble in the find line, because I don't know how to express I want to delete the extension and append the new one when using {}
--
marks the end of command options, actually, it isn't necessary in this case, but I put there for general. the first argument passed to inlinesh
will be the named of inlinesh
process. Try somethingsh -c 'does not exist' foo
andsh -c'does not exist' bar
. – cuonglm Nov 03 '16 at 02:26