I need a script that looks through the same folder as the script is in, takes all the .mkv files, looks at their filename (not extension), creates a folder with that same name, if folder is not already created, and moves that file into it's own folder, if a file with that same name is already in the folder it doesn't prompt me to overwrite or not, but skips it and let's me know which files was skipped and why.
Assistant
Certainly! Here is a Bash script that accomplishes what you've described:
bash
#!/bin/bash
# Initialize an empty variable to store skipped files
skipped=""
# Check if there are any .mkv files in the current directory
shopt -s nullglob
mkv_files=(*.mkv)
shopt -u nullglob
if [ ${#mkv_files[@]} -eq 0 ]; then
echo "No .mkv files found in the current directory."
exit 0
fi
# Loop over all .mkv files in the current directory
for filepath in "${mkv_files[@]}"; do
# Get the filename without the extension
filename="${filepath%.*}"
# Create a folder with the same name if it doesn't exist
if [ ! -d "$filename" ]; then
mkdir "$filename"
echo "Created directory: $filename"
fi
# Check if a file with the same name already exists in the target folder
if [ -e "$filename/$filepath" ]; then
echo "Skipping '$filepath' because '$filename/$filepath' already exists."
skipped="$skipped\n$filepath was skipped because $filename/$filepath already exists."
continue
fi
# Move the file into the folder
mv "$filepath" "$filename/"
echo "Moved '$filepath' to '$filename/'"
done
# Display the list of skipped files, if any
if [ -n "$skipped" ]; then
echo -e "\nFiles skipped:"
echo -e "$skipped"
fi
Explanation:
Checks for .mkv Files: The script first checks if there are any .mkv files in the current directory. If none are found, it exits gracefully.
Handles Filenames with Spaces: By using quotes around variables and array handling, it safely processes filenames that contain spaces or special characters.
Creates Directories as Needed: For each .mkv file, it extracts the base filename (without the extension) and creates a directory with that name if it doesn't already exist.
Moves Files Without Overwriting: Before moving a file, it checks if a file with the same name already exists in the target directory. If it does, it skips the file and records the reason.
Provides Informative Output: The script echoes messages to inform you of actions taken, directories created, files moved, and files skipped, along with reasons for skipping.
Summary of Skipped Files: After processing all files, it prints a summary of any files that were skipped.
Usage:
Save the Script:
Save the script to a file in the directory containing your .mkv files. Let's say you name it organize_mkv.sh.
Make the Script Executable:
Open a terminal in that directory and run:
bash
chmod +x organize_mkv.sh
Run the Script:
bash
./organize_mkv.sh
Note: Ensure that you have the necessary permissions to create directories and move files in the directory where you're running the script.
Example Output:
code
Created directory: Movie1
Moved 'Movie1.mkv' to 'Movie1/'
Created directory: Sample Video
Moved 'Sample Video.mkv' to 'Sample Video/'
Skipping 'Example.mkv' because 'Example/Example.mkv' already exists.
Files skipped:
Example.mkv was skipped because Example/Example.mkv already exists.
This script should fulfill your requirements by organizing your .mkv files into their respective directories, avoiding overwriting any existing files, and informing you of any files that were skipped.