Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Script Name: superfind V4.1
- # Author: Subbass
- # Date: May 7, 2024
- # Pastebin: https://pastebin.com/84vhYa9a
- #
- # Description:
- # A versatile file search tool that allows users to search for files based on patterns
- # within user-defined paths stored in a configuration file or a specified directory.
- # Supports creating playlists from the search results, editing the path configuration file,
- # and displays results with colored output.
- #
- # Features:
- # - Search within specified paths using flags or a direct folder path
- # - Option to create a playlist of search results
- # - Colored output for better readability
- # - Ability to open and edit the path configuration file
- #
- # Usage:
- # - Search for files using a flag: ./superfind <flag> "search_pattern"
- # - Search in a specified directory: ./superfind -d <directory> "search_pattern"
- # - Create a playlist of the results: ./superfind -p <flag> "search_pattern"
- # - Edit the path configuration file: ./superfind --edit
- # - Show help message: ./superfind -h
- #
- # Examples:
- # - superfind -m "old movie" - Search for videos in movies directory.
- # - superfind -d /tmp "test file" - Search in a specified directory.
- # - superfind -p -m "funny clip" - Create a playlist from search results.
- # - superfind --edit - Open the path configuration file in an editor.
- #
- # Configuration File: superfind_paths.conf
- #
- # Extended Color Definitions
- col1="\033[0;31m" # Red
- col2="\033[0;32m" # Green
- col3="\033[0;33m" # Yellow
- col4="\033[0;34m" # Blue
- col5="\033[0;35m" # Magenta
- col6="\033[0;36m" # Cyan
- col7="\033[0;37m" # Light Gray
- col8="\033[1;30m" # Dark Gray
- col9="\033[1;31m" # Light Red
- col10="\033[1;32m" # Light Green
- col11="\033[1;33m" # Light Yellow
- col12="\033[1;34m" # Light Blue
- col13="\033[1;35m" # Light Magenta
- col14="\033[1;36m" # Light Cyan
- col15="\033[1;37m" # White
- # Text Style Definitions
- style_bold="\033[1m"
- style_underline="\033[4m"
- style_blink="\033[5m"
- style_reset="\033[0m" # Reset color and style
- # Color Usage in Output
- searchin_color="$col6" # Use Cyan for "[ Searching in ]"
- file_color="$col2" # Use Green for files
- folder_color="$col4" # Use Blue for folders
- no_match="$col9" # Use Light Red for no match
- reset_color="$style_reset" # Reset color and style
- # Determine the directory in which the script resides
- SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
- # Configuration
- PATHS_FILE="$SCRIPT_DIR/superfind_paths.conf"
- EDITOR="${EDITOR:-nano}"
- # Ensure the configuration file exists
- if [ ! -f "$PATHS_FILE" ]; then
- # Default playlist directory using the user's home directory
- default_playlist_dir="/home/$USER/Playlists"
- cat > "$PATHS_FILE" << EOF
- # Default Playlist Directory
- playlist_dir=$default_playlist_dir
- # Example Paths Configuration
- # Use these examples as a guide to add your own:
- # -m /home/user/movies/
- # -mu /home/user/Music
- # --movies /home/user/movies/
- # -tvgroup /path/to/tv1;/path/to/tv2;/path/to/tv3
- #
- EOF
- fi
- # Help function to include examples with wildcards
- show_help() {
- echo "Usage: superfind [options] <search_pattern>"
- echo "Options:"
- echo " -p, --playlist Create a playlist of the results."
- echo " -d, --dir <dir> Specify a directory for a one-time search."
- echo " -h, --help Show this help message."
- echo " -e, --edit Open the paths file in a text editor."
- echo " --copy Copy matching files to the current directory."
- echo " --move Move matching files to the current directory."
- echo " -o, --open Open the created playlist."
- echo " <flag> Search in the paths associated with this flag (e.g., -tvgroup for TV shows)."
- echo "Examples:"
- echo " superfind --move -tvgroup '*episode*' Search and move all files with 'episode' in their name under tvgroup paths."
- echo " "
- echo "User defined path flags:"
- grep '^-' "$PATHS_FILE" | sed 's/^/ /'
- }
- open_editor() {
- $EDITOR "$PATHS_FILE"
- }
- # Search function updated to accommodate wildcard patterns
- search_files() {
- local search_pattern=$1
- local pattern=$2
- local search_flag=$3
- local file_count=0
- local folder_count=0
- local cwd=$(pwd)
- local playlist_dir=$(grep 'playlist_dir=' "$PATHS_FILE" | cut -d'=' -f2)
- if [[ -n "$playlist_dir" ]]; then
- mkdir -p "$playlist_dir"
- else
- echo -e "${no_match}Error: Playlist directory not defined or invalid in configuration.${reset_color}"
- return 1
- fi
- local safe_search_term=$(echo "$pattern" | sed 's/[^a-zA-Z0-9]/_/g')
- if [[ "$create_playlist" == true ]]; then
- local playlist_file="${playlist_dir}/${search_flag}-${safe_search_term}-$(date +%Y%m%d%H%M%S).m3u"
- > "$playlist_file"
- fi
- IFS=';' read -ra paths <<< "$search_pattern"
- for search_path in "${paths[@]}"; do
- echo -e " [ Searching in ] ${searchin_color}$search_path${reset_color}"
- # Using find and grep to match complex patterns across paths
- local file_results=$(find "$search_path" -type f -print | grep -i "$(echo "$pattern" | sed 's/\*/.*/g')")
- if [[ -z "$file_results" ]]; then
- do_nothing=0
- #echo " No files found matching pattern '$pattern' in $search_path."
- else
- if [[ "$create_playlist" == true ]]; then
- while IFS= read -r line; do
- local ext="${line##*.}"
- if [[ ! " ${exclude_list[@]} " =~ " $ext " ]]; then
- echo "$line" >> "$playlist_file"
- ((file_count++))
- fi
- done <<< "$file_results"
- # Sort the playlist after all files are added
- sort "$playlist_file" -o "$playlist_file"
- else
- while IFS= read -r line; do
- echo -e " ${line%/*}/${file_color}${line##*/}${reset_color}"
- ((file_count++))
- if [[ "$move_files" == true ]]; then
- local source_path=$(dirname "$line")
- local target_file="$(basename "$line")"
- if [[ "$source_path" == "$cwd" ]]; then
- echo " File $line is already in the current directory, skipping move."
- elif [[ -f "$target_file" ]]; then
- echo " Skipping move for $line: File already exists in target directory."
- else
- mv "$line" ./
- echo " Moved $line to the current directory."
- fi
- fi
- if [[ "$copy_files" == true ]]; then
- local source_path=$(dirname "$line")
- local target_file="$(basename "$line")"
- if [[ "$source_path" == "$cwd" ]]; then
- echo " File $line is already in the current directory, skipping copy."
- elif [[ -f "$target_file" ]]; then
- echo " Skipping copy for $line: File already exists in target directory."
- else
- cp "$line" ./
- echo " Copied $line to the current directory."
- fi
- fi
- done <<< "$file_results"
- fi
- do_nothing=0
- #echo " ------------------------------------------"
- fi
- local folder_results=$(find "$search_path" -type d -iname "*$pattern*" | grep -i "$(echo "$pattern" | sed 's/\*/.*/g')")
- if [[ -z "$folder_results" ]]; then
- do_nothing=0
- #echo " No folders found matching pattern '$pattern' in $search_path."
- else
- while IFS= read -r folder; do
- local num_files_in_folder=$(find "$folder" -type f -iname "*$pattern*" | wc -l)
- echo -e " ${folder_color}$folder${reset_color} ($num_files_in_folder files)"
- ((folder_count++))
- done <<< "$folder_results"
- fi
- do_nothing=0
- #echo " ------------------------------------------"
- done
- if [[ "$create_playlist" == true ]]; then
- echo "Playlist created: $playlist_file"
- fi
- echo "Results: ${file_count} files : ${folder_count} folders"
- # Open the playlist if the open flag is set
- if [[ "$open_playlist" == true ]]; then
- xdg-open "$playlist_file" &> /dev/null &
- echo "Opening playlist..."
- fi
- }
- # Manual parsing of options
- create_playlist=false
- dir_path=""
- pattern=""
- flag=""
- open_playlist=false
- move_files=false
- copy_files=false # Initialize the flag
- # Updated command-line options parsing
- while [[ $# -gt 0 ]]; do
- case "$1" in
- -p|--playlist)
- create_playlist=true
- shift
- ;;
- -d|--dir)
- dir_path="$2"
- shift 2
- ;;
- -h|--help)
- show_help
- exit 0
- ;;
- -e|--edit)
- open_editor
- exit 0
- ;;
- --copy)
- copy_files=true
- shift
- ;;
- --move)
- move_files=true
- shift
- ;;
- -o|--open)
- open_playlist=true
- shift
- ;;
- -po)
- create_playlist=true
- open_playlist=true
- shift
- ;;
- -*)
- flag="$1"
- pattern="$2"
- shift 2
- ;;
- *)
- pattern="$1"
- shift
- ;;
- esac
- done
- # Validate that a pattern is provided
- if [[ -z "$pattern" ]]; then
- echo "Error: No search pattern provided."
- show_help
- exit 1
- fi
- # Determine the search path based on flag or direct directory option
- if [[ -n "$dir_path" ]]; then
- search_files "$dir_path" "$pattern"
- elif [[ -n "$flag" ]]; then
- while IFS= read -r line; do
- key=$(echo "$line" | cut -d' ' -f1)
- path=$(echo "$line" | cut -d' ' -f2-)
- if [[ "$flag" == "$key" ]]; then
- search_files "$path" "$pattern" "$flag"
- exit 0
- fi
- done < "$PATHS_FILE"
- echo "Error: Flag $flag not found in $PATHS_FILE."
- exit 1
- else
- echo "Error: No valid directory or flag specified."
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement