Advertisement
Guest User

Untitled

a guest
Oct 14th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.04 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. image_dir="images"
  4. tags_dir="tags"
  5. output_dir="output"
  6.  
  7. # pre-search for the first tag matches
  8. grep --include=*.txt -rFl "$1" "$tags_dir" > /tmp/booru_search_tag_matches.txt
  9.  
  10. # loop through the tags and discard non-matching ones
  11. for tag in "$@"
  12. do
  13.     echo -e "looking for images matching $tag"
  14.     while read -r tag_file ; do
  15.         grep --include=*.txt -rFl "$tag" "$tag_file" >> /tmp/booru_search_tag_matches_tmp.txt
  16.     done < /tmp/booru_search_tag_matches.txt
  17.     mv /tmp/booru_search_tag_matches_tmp.txt /tmp/booru_search_tag_matches.txt
  18. done
  19.  
  20. # clean up previous search results
  21. [ -d "$output_dir" ] && rm -r "$output_dir"
  22.  
  23. mkdir -p "$output_dir"
  24.  
  25. # link matching images to the output directory
  26. while read -r tag_file ; do
  27.     # form paths for image and tags
  28.     img_filename="$(basename ${tag_file%_tags.txt})"
  29.     img_src="$image_dir/$img_filename"
  30.     img_dst="$output_dir/$img_filename"
  31.    
  32.     ln -v "$img_src" "$img_dst"
  33. done < /tmp/booru_search_tag_matches.txt
  34.  
  35. rm /tmp/booru_search_tag_matches.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement