Notice
First, find -type f -name Localis* | xargs mv Localization.json
will never work ... You need something like xargs -I {} mv {} Localization.json
or set the xargs
's option -n 1
.
Second, mv
is not the right choice here as it will result in all files named e.g. Localisation.json
(or whatever files find
matches and outputs) in all sub-directories of the current working directory moved to the current working directory and renamed as Localization.json
overwriting this file every time ... e.g. find
might output something like ./subdirectory/Localisation.json
and that will translate with mv
like so:
mv ./subdirectory/Localisation.json Localization.json
changing destination directory to the current working directory ... You, definitely, don't want that.
Therefore, ...
Suggestion
You can format find
's output to be NULL delimited(To try to avoid breaking filenames containing e.g. whitespace) with -print0
then pipe it to xargs -0
with rename
like so:
find -type f -name "Localisation.json" -print0 | xargs -0 rename -n 's/Localisation\.json$/Localization.json/'
Or. even better, and comparatively efficient ... you can use find
's own -exec
with rename
like so:
find -type f -name "Localisation.json" -exec rename -n 's/Localisation\.json$/Localization.json/' {} \+
and it's important to quote the argument to -name
e.g. -name "Localis*"
Notice that everything in the CurrentName
part of rename
's 's/CurrentName/NewName/'
is considered a regular expression ... Hence the escaped dot \.
to match a literal .
and the $
to match the end of the line excluding directories from being renamed in case you have a parent directory with the same filename e.g. Localisation.json/Localisation.json
(That can also be achieved with the rename
's option --nopath
)
The rename
's option -n
will only simulate renaming ... Remove it when satisfied with the output to do the actual renaming.
Workaround
You can use find
's -execdir
(Which runs the specified command from the subdirectory containing the matched file) with mv
like so:
find -type f -name "Localisation.json" -execdir echo mv -n -- {} Localization.json \;
You can, also, use find
's -exec
with mv
in a bash
command string utilizing bash
's parameter expansion and achieve the same result like so:
find -type f -name "Localisation.json" -exec bash -c 'echo mv -n -- "$1" "${1/%Localisation.json/Localization.json}"' _ {} \;
That will do only a renaming dry-run(simulation) ... Remove echo
when satisfied with output to do the actual renaming.
Alternatively in bash
, you can use recursive shell globbing and parameter expansion with only mv
to achieve the same result like so:
shopt -s globstar; for f in **/Localisation.json; do
# Renaming dry-run(simulation) ... Remove "echo" when satisfied with output to do the actual renaming.
echo mv -n -- "$f" "${f/%Localisation.json/Localization.json}"
done; shopt -u globstar