Skip to content

Instantly share code, notes, and snippets.

@3f8wohoo
Created February 9, 2019 14:02
Show Gist options
  • Save 3f8wohoo/2c675cd2df345f33760d571c5f084d6c to your computer and use it in GitHub Desktop.
Save 3f8wohoo/2c675cd2df345f33760d571c5f084d6c to your computer and use it in GitHub Desktop.
Extract name from torrent and rename .torrent file
#!/bin/bash
# Usage: rename-torrent.sh /path/to/torrents /path/to/output
#
# read-torrent must be installed
# npm install -g read-torrent
if [[ -z $1 ]]; then
echo "No input path specified!"
exit 1
else
inputPath="$1"
fi
if [[ -z $2 ]]; then
echo "No output path specified; creating directory..."
outputPath="done"
if [[ ! -d "$outputPath" ]]; then
mkdir "$outputPath"
fi
else
outputPath="$2"
fi
for filename in "$inputPath/*"; do
echo "Reading $filename"
torrentID=$(basename "$filename")
torrentName=$(read-torrent "$filename" | head -4 | grep name | sed 's/name: //' | sed 's/\///')
echo "Name: $torrentName"
mv "$filename" "$outputPath/$torrentName - $torrentID"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment