Okay, I'm going in circles. I'm using this command
find . -print0 -name '*.1.*' | sed -e 'p;s/\.1//' | xargs -0 -n2 mv
To try and rename hundreds of files that had ".1." added just before the file extension when they were archived after someone accidentally deleted 200GB worth of data.
I'm caught between the mac xargs interpreting spaces in filenames as separate arguments, and not being able to set the delimiter to only newlines, not spaces. I cannot figure out how to have find print the '\0' character as well as newlines between. Any ideas on how to get this to work? I've been searching in circles and it seems simply being on a mac environment is making this more complicated than necessary.
Alternatively trying rename command but still having issues
find . -name '*.1.*' -type f -exec rename -n 's/\.1//' '{}' \;
ANSWER as per @Wildcard below
find . -name '*.1.*' -type f -exec sh -c '
for f do
suf="${f##*.1}"
new="${f%.1.*}$suf"
if [ -e "$new" ]; then
printf "Cannot move file <%s>\n" "$new"
else
mv -n "$f" "$new"
fi
done
' find-sh {} +
-z, --null-data
argument for precisely this purpose. – Att Righ Dec 05 '17 at 00:44strace -e execve -F
to work out precisely what is being executed. It might be choking on the newlines. I am pretty hopeful about the -z argument for sed by the way. – Att Righ Dec 05 '17 at 00:48rename
on OSX. Also, there's nosed -z
either, that's agnu
extension. This is an easy job for any shell that supports${var/string/replace}
. – don_crissti Dec 05 '17 at 00:51gnu sed
man pages... The one in your 2nd link is just too old that's why it doesn't mention-z
... Anyway, here's the official Options section – don_crissti Dec 05 '17 at 01:14