Skip to content

Instantly share code, notes, and snippets.

@joshkerr
Created April 15, 2024 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkerr/76a231370e3093fbf235bcf9a5d22791 to your computer and use it in GitHub Desktop.
Save joshkerr/76a231370e3093fbf235bcf9a5d22791 to your computer and use it in GitHub Desktop.
This shell script will allow you to specify a directory on a remote system and then present you a list of files/folders allowing you to choose 1 and then copy it to the current directory using rclone. You must have rclone setup already to use this script.
#!/usr/bin/env bash
# Function to list files and let user choose one to download
download_from_remote() {
# Select a remote folder
remote="remote:/path/to/folder/"
# List files on the specified remote and path
files_json=$(rclone lsjson "$remote")
# Convert JSON to an array of file paths
mapfile -t files < <(echo "$files_json" | jq -r '.[].Path')
# Check if files are available
if [ ${#files[@]} -eq 0 ]; then
echo "No files found in the specified directory."
return
fi
# Display files with an index
for i in "${!files[@]}"; do
echo "$((i+1)): ${files[$i]}"
done
# Ask the user to choose a file to download or quit
echo "Enter the number of the file to download or 'q' to quit:"
read -r selectedIndex
# Handle quit request
if [ "$selectedIndex" = "q" ]; then
echo "Exiting script."
exit 0
fi
# Validate selection
if [[ "$selectedIndex" =~ ^[0-9]+$ ]] && [ "$selectedIndex" -le "${#files[@]}" ] && [ "$selectedIndex" -gt 0 ]; then
selectedFile="${files[$((selectedIndex-1))]}"
# Confirming the file to download
if [ -n "$selectedFile" ]; then
echo "Downloading '$selectedFile'..."
rclone copy -P --sftp-chunk-size=32k --sftp-concurrency=512 --exclude="*.part" "$remote/$selectedFile" .
echo "Download complete!"
else
echo "Invalid selection. Please try again."
fi
else
echo "Invalid input. Please enter a valid number or 'q' to quit."
fi
}
# Main loop
while true; do
download_from_remote
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment