I want to use the find command to look for files and create symbolic links for every result, if a condition matches.
The script nearly works. Just the ln -s $f $destDir;
command doesnt.
ln always wants to create the link in the same directory where the found files is.
error" File exists"
In detail:
I use find to look for special files. (this works)
find $startSearchDir find -L $startSearchDir \( -iname '*.avi' -or -iname '*.mp4' \) -size -3000k
.
within the results and within the special folder, I check a (movie) file for duration by using "mediainfo". (this works)
-execdir sh -c 'f="{}"; D=$(LC_ALL=C.UTF-8 /opt/bin/bc <<< $(mediainfo --Inform="Video;%Duration%" "$f")/1000);
If the duration equals my condition if [ $D -le 3 ] then ln -s $f $destDir; fi ' \;
a symbolic link should be added at the destination Path.
(the if part works, I can printf or echo the result $f)
but the ln -s $f $destDir
part doesnt work.
all together in one line:
find -L $startSearchDir \( -iname '*.avi' -or -iname '*.mp4' \) -size -3000k -execdir sh -c 'f="{}"; D=$(LC_ALL=C.UTF-8 /opt/bin/bc <<< $(mediainfo --Inform="Video;%Duration%" "$f")/1000); if [ $D -le 3 ]; then ln -s $f $destDir; fi ' \;
and the relevant part of my script
#!/bin/bash
set -euo pipefail
startSearchDir='/share/Multimedia2/VideoLink/tmp/a/'
destDir='/share/Multimedia2/VideoLink/tmp/b/'
rm -r $destDir
mkdir $destDir
path1=" ";
#find $startSearchDir -type f -size -3000k \( -iname '*.avi' -or -iname '*.mp4' \) | xargs -I {} ln -s {} $destDir ;
find -L $startSearchDir \( -iname '*.avi' -or -iname '*.mp4' \) -size -3000k -execdir sh -c 'f="{}"; D=$(LC_ALL=C.UTF-8 /opt/bin/bc <<< $(mediainfo --Inform="Video;%Duration%" "$f")/1000); if [ $D -le 3 ]; then path1=$(realpath "$f"); echo $path1; $(ln -s $path1 $destDir); fi ' \;
#find -L $startSearchDir \( -iname '*.avi' -or -iname '*.mp4' \) -size -3000k -exec ln -s '{}' $destDir ';'
find -L $startSearchDir \( -iname '*.avi' -or -iname '*.mp4' \) -size -3000k -execdir sh -c 'f="{}"; D=$(LC_ALL=C.UTF-8 /opt/bin/bc <<< $(mediainfo --Inform="Video;%Duration%" "$f")/1000); if [ $D -le 3 ]; then ln -s $f $destDir; fi ' \;
As you can see in the script, I have tried different approches to get "ln" to work. I have tried "realpath" to create a variable that stores the full path.
With xargs I can create symbolic links. Also with find -exec
as long as I just use one command. Together with if and the mediainfo part within the subshell, I fail.
What do I have to change, to tell "ln" the correct source and destination Path?