118 lines
2.8 KiB
Bash
Executable file
118 lines
2.8 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Default values
|
|
VERBOSE=false
|
|
INCLUDE_PART=false
|
|
|
|
# Function to display usage
|
|
usage() {
|
|
echo "Usage: $0 [options] <search_word> <source_directory> <destination_directory>"
|
|
echo "Options:"
|
|
echo " -v, --verbose Show detailed output"
|
|
echo " -p, --include-part Include files ending in .part (excluded by default)"
|
|
echo " -h, --help Show this help message"
|
|
exit 1
|
|
}
|
|
|
|
# Parse command line options
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-v|--verbose)
|
|
VERBOSE=true
|
|
shift
|
|
;;
|
|
-p|--include-part)
|
|
INCLUDE_PART=true
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
;;
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Check if we have exactly 3 arguments after options
|
|
if [ "$#" -ne 3 ]; then
|
|
usage
|
|
fi
|
|
|
|
SEARCH_WORD=$1
|
|
SOURCE_DIR=$2
|
|
DEST_DIR=$3
|
|
|
|
# Validate search word
|
|
if [ -z "$SEARCH_WORD" ]; then
|
|
echo "Error: Search word cannot be empty"
|
|
exit 1
|
|
fi
|
|
|
|
# Ensure source directory exists
|
|
if [ ! -d "$SOURCE_DIR" ]; then
|
|
echo "Error: Source directory '$SOURCE_DIR' does not exist."
|
|
exit 1
|
|
fi
|
|
|
|
# Initialize counter
|
|
moved_count=0
|
|
|
|
# Function to process files
|
|
process_files() {
|
|
local find_cmd="find \"$SOURCE_DIR\" -maxdepth 1 -type f -iname \"*$SEARCH_WORD*\""
|
|
|
|
# Exclude .part files and macOS resource fork files by default
|
|
if [ "$INCLUDE_PART" = false ]; then
|
|
find_cmd="$find_cmd -not -name \"*.part\""
|
|
fi
|
|
find_cmd="$find_cmd -not -name \"._*\""
|
|
|
|
# First, show what would be moved
|
|
echo "The following files would be moved:"
|
|
eval "$find_cmd"
|
|
|
|
# Count files that would be moved
|
|
local file_count=$(eval "$find_cmd" | wc -l)
|
|
|
|
if [ "$file_count" -eq 0 ]; then
|
|
echo "No matching files found."
|
|
exit 0
|
|
fi
|
|
|
|
# Show what actions will be taken
|
|
echo
|
|
if [ ! -d "$DEST_DIR" ]; then
|
|
echo "The destination directory '$DEST_DIR' will be created."
|
|
fi
|
|
echo "Files will be moved to: '$DEST_DIR'"
|
|
|
|
# Prompt for confirmation
|
|
echo -n "Do you want to proceed? (y/n): "
|
|
read -r response
|
|
|
|
if [[ "$response" =~ ^[Yy]$ ]]; then
|
|
# Create destination directory if needed
|
|
if [ ! -d "$DEST_DIR" ]; then
|
|
mkdir -p "$DEST_DIR"
|
|
fi
|
|
|
|
while IFS= read -r file; do
|
|
if [ "$VERBOSE" = true ]; then
|
|
echo "Moving: $file"
|
|
fi
|
|
if mv "$file" "$DEST_DIR"; then
|
|
((moved_count++))
|
|
else
|
|
echo "Error: Failed to move '$file'" >&2
|
|
fi
|
|
done < <(eval "$find_cmd")
|
|
|
|
echo "Operation completed. $moved_count file(s) moved to '$DEST_DIR'"
|
|
else
|
|
echo "Operation cancelled."
|
|
fi
|
|
}
|
|
|
|
# Execute the move operation
|
|
process_files
|