I have defined a function in bash, which checks if two files exists, compare if they are equal and delete one of them.
function remodup {
F=$1
G=${F/.mod/}
if [ -f "$F" ] && [ -f "$G" ]
then
cmp --silent "$F" "$G" && rm "$F" || echo "$G was modified"
fi
}
Then I want to call this function from a find command:
find $DIR -name "*.mod" -type f -exec remodup {} \;
I have also tried | xargs syntax. Both find and xargs tell that ``remodup` does not exist.
I can move the function into a separate bash script and call the script, but I don't want to copy that function into a path directory (yet), so I would either need to call the function script with an absolute path or allways call the calling script from the same location.
(I probably can use fdupes for this particular task, but I would like to find a way to either
- call a function from
findcommand; - call one script from a relative path of another script; or
- Use a
${F/.mod/}syntax (or other bash variable manipulation) for files found with afindcommand.)
You could manually loop over
find's results.-print0and-d $'\0'use NUL as the delimiter, allowing for newlines in the file names.IFS=ensures spaces as the beginning of file names aren't stripped.-rdisables backslash escapes. The sum total of all of these options is to allow as many special characters as possible in file names without mangling.