Search code examples
unixfindexecnassynology

Multiple find exec options: synology index command line


I want to index some files on my NAS after moving them in the correct folder. My command would be something like :

find *.$ext -exec mv "{}" $path \; -exec synoindex -a $(echo $path)$(basename "{}") \;

The first part is working. All files with $ext extension are moved to destination $path. But the second part, which is supposed to index these files in their new $path folder, does not work.

This is strange because :

  • {} contains the right value => the complete old path of each file processed To make sure of that, I added a third part which only does : -exec echo {} \;
  • Executing separately $(echo $path)$(basename "{}") works, after replacing {} by one real
    value taken for example => gives the the complete new path => syntax is correct
  • Executing separately synoindex -a $(echo $path)$(basename "{}") works, after replacing {} by one real value taken for example => command is correct

Thanks for any idea.

Regards,


Solution

  • Your command substitutions $(echo $path) and $(basename "{}") are executed by your shell before find is executed. And you don't need to echo the $path variable. You could execute a small shell script instead:

    find . -type f -name "*.$ext" -exec sh -c '
      targetpath=$1; shift # get the value of $path
      for file; do
        mv -i "$file" "$targetpath"
        synoindex -a "$targetpath/${file##*/}"
      done
    ' sh "$path" {} +
    

    This starts find in the current directory . searching for regular files (-type f) ending with the file extension $ext (-name "*.$ext") and executes a small shell script passing the $path variable as first argument to the script. The following arguments are the filepaths found by find.

    The parameter expansion ${file##*/} removes the longest prefix */ from the file and the result is the basename. If your $path variable already contains a trailing slash /, then omit the / after $targetpath.