bash - find piped to xargs with complex command -
i trying process dvd files in many different locations on disk.  thing have in common (each set of input files) in directory named video_ts.  output in each case single file named parent of directory.
i know can qualified path each directory with:
find /volumes/volumename -type d -name "video_ts" -print0 and can parent directory piping xargs:
find /volumes/volumename -type d -name "video_ts" -print0 | xargs -0 -i{} dirname {} and know can parent directory name on own appending:
| xargs -o i{} basename {} what can't figure out how pass these parameters to, e.g. handbrakecli:
./handbrakecli -i /path/to/filename/video_ts -o /path/to/convertedfiles/filename.m4v i have read here expansion capability of shell , suspect that's going here (not using dirname or basename start), more read more confused getting!
you don't need xargs @ all: can read nul-delimited stream shell loop, , run commands want directly there.
#!/bin/bash  source_dir=/volumes/volumename dest_dir=/volumes/othername  while ifs= read -r -d '' dir;   name=${dir%/video_ts} # trim /video_ts off end of dir, assign name   name=${name##*/}      # remove before last remaining / name   ./handbrakecli -i "$dir" -o "$dest_dir/$name.m4v" done < <(find "$source_dir" -type d -name "video_ts" -print0) see the article using find on greg's wiki, or bashfaq #001 general information on processing input streams in bash, or bashfaq #24 understand value of using process substitution (the <(...) construct here) rather piping find loop.
also, find contains -exec action can used follows:
source_dir=/volumes/volumename dest_dir=/volumes/othername  export dest_dir # export allows use subprocesses!  find "$source_dir" -type d -name "video_ts" -exec bash -c '   dir;     name=${dir%/video_ts}     name=${name##*/}     ./handbrakecli -i "$dir" -o "$dest_dir/$name.m4v"   done ' _ {} + this passes found directory names directly on argument list shell invoked bash -c. since default object for loop iterate on "$@", argument list, implicitly iterates on directories found find.
Comments
Post a Comment