AboutLet me explain what the code does, I added the details in the code as comments foram trying to do by e.g. so you can understand better context,. Let's say I have 100 .torrent files in a directory. 2 of them will download xxx.epub and clarityyyy. The idea isepub respectively if added to provide it with file namea bittorrent client, but I don't know which 2 out of the 100.
So what my script does is, (s1) and it will start goinguse find to go through all the .torrent files in the current directory (and 1 level under), parsing each onepwd and checkingpass each .torrent file, as it forcomes by, to transmission-show which will parse the .torrent file and output metadata in human readable format. We'll then use awk to get the file name(s) provided the torrent file will download and run that against the list.txt which has file names we are looking for, i.e. xxx.epub and yyy.epub.
#! /bin/bash
#
# Search .torrent file based on 'Name' field.
#
# USAGE:
# cd ~/myspace # location of .torrent files
# Run `findtor ~/list.txt` (if `findtor.sh` is placed in `~/bin` or `~/.local/bin`)
# Turn the list of file names from ~/list.txt (or any file passed as argument) into an array
readarray -t FILE_NAMES_TO_SEARCH < "$1"
# For each file name from the list...
for FILE_NAME in "${FILE_NAMES_TO_SEARCH[@]}"
do
# In `pwd` and 1 directory-level under, look for .torrent files and search them for the file name
find . -maxdepth 2 -name '*.torrent' -type f -exec bash -c "transmission-show \"\$1\" | awk '/^Name\: / || /^File\: /' | awk -F ': ' '\$2 ~ \"$FILE_NAME\" {getline; print}'" _ {} \; >> ~/torrents.txt
# The `transmission-show` command included in `find`, on it own, for clarity:
# transmission-show xxx.torrent | awk '/^Name: / || /^File: /' | awk -F ': ' '$2 ~ "SEARCH STRING" {getline; print}'
done