Skip to content

Instantly share code, notes, and snippets.

@ColeMurray
Created September 26, 2024 17:41
Show Gist options
  • Save ColeMurray/25d1fb27772703ee5963f5015b7e67d4 to your computer and use it in GitHub Desktop.
Save ColeMurray/25d1fb27772703ee5963f5015b7e67d4 to your computer and use it in GitHub Desktop.
Script for copying files in a directory into clipboard
#!/bin/bash
# Check for required arguments
if [ $# -lt 2 ]; then
echo "Usage: $0 <directory> <delimiter> [--include=<pattern>] [--exclude=<pattern>]"
exit 1
fi
# Assign the first argument as the directory
DIRECTORY=$1
# Assign the second argument as the delimiter
DELIMITER=$2
# Optional arguments: include and exclude patterns
INCLUDE=""
EXCLUDE=""
# Process optional include and exclude arguments
for arg in "$@"; do
case $arg in
--include=*)
INCLUDE="${arg#*=}"
shift
;;
--exclude=*)
EXCLUDE="${arg#*=}"
shift
;;
esac
done
# Temp file to store the concatenated output
OUTPUT_FILE="/tmp/clipboard_output.txt"
# Clear the output file
> $OUTPUT_FILE
# Check if the directory exists
if [ ! -d "$DIRECTORY" ]; then
echo "Error: Directory $DIRECTORY does not exist."
exit 1
fi
# Build the find command based on the include and exclude patterns
FIND_CMD="find \"$DIRECTORY\" -type f"
if [ -n "$INCLUDE" ]; then
FIND_CMD="$FIND_CMD -name \"$INCLUDE\""
fi
if [ -n "$EXCLUDE" ]; then
FIND_CMD="$FIND_CMD ! -name \"$EXCLUDE\""
fi
# Execute the find command and process each file
eval $FIND_CMD | while read -r FILE; do
# Append the filename as a comment
echo "# Filename: $FILE" >> $OUTPUT_FILE
# Append the file content
cat "$FILE" >> $OUTPUT_FILE
# Append the delimiter
echo -e "\n$DELIMITER\n" >> $OUTPUT_FILE
done
# Copy the output file to clipboard using pbcopy (for macOS), xclip (Linux), or clip (Windows)
if [[ "$OSTYPE" == "darwin"* ]]; then
# For macOS
pbcopy < $OUTPUT_FILE
echo "Content copied to clipboard using pbcopy (macOS)."
elif command -v xclip &> /dev/null; then
# For Linux with xclip
xclip -selection clipboard < $OUTPUT_FILE
echo "Content copied to clipboard using xclip (Linux)."
elif command -v clip &> /dev/null; then
# For Windows in WSL
clip < $OUTPUT_FILE
echo "Content copied to clipboard using clip (WSL)."
else
echo "No clipboard utility found. Please install pbcopy, xclip, or clip."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment