How to deal with duplicate file names when moving files in terminal? So I'm trying to move images of a specific dimension to another folder. But working with a huge number of files, the same filename once and a while presents itself. And I have been using this script:

#!/bin/bash
targetDir="$HOME/Documents/pixx"

find "$targetDir" -iname '*.jpg' -o -iname '*.png' -o -iname '*.bmp' -o -iname '*.jpeg' 2&gt;/dev/null | \
while read -r filename; do
    hw="$(sips -g pixelHeight -g pixelWidth "$filename" 2&gt;/dev/null)"
    h="$(awk '/pixelHeight/{print $2}'&lt;&lt;&lt;"$hw")"
    w="$(awk '/pixelWidth/{print $2}'&lt;&lt;&lt;"$hw")"
    if [[ $h -eq 270 ]] &amp;&amp; [[ $w -eq 360 ]]; then
        mv "$filename" "$targetDir/hehe"
    fi
done


I found out that mv had a backup command mv --backup=t but that is just for linux, so I can't use that. But it would be great to have some sort of equal function in this script above.

So how can I add a incremental number if the same filename appears?
If you are already a homebrew user, you could install GNU coreutils and get quick access to GNU mv. This is installed as gmv so that it doesn't break any scripts that rely on BSD mv's syntax.

If you don't use homebrew and do not want to install it, you'll need to do tests with [ -f ${filename} ] etc. (which I expect is what you wanted).

Once you have homebrew installed, run,

brew install fileutils


Then you can gmv --backup=t as you wished to.