Skip to content

Instantly share code, notes, and snippets.

@sadimanna
Created June 9, 2023 13:10
Show Gist options
  • Save sadimanna/1616b555db5a126a523a657ab57dde61 to your computer and use it in GitHub Desktop.
Save sadimanna/1616b555db5a126a523a657ab57dde61 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set the paths for Folder A and the three target folders
folder_a_path="/path/to/FolderA"
train_folder_path="/path/to/train"
val_folder_path="/path/to/val"
test_folder_path="/path/to/test"
# Create the target folders if they don't exist
mkdir -p "$train_folder_path"
mkdir -p "$val_folder_path"
mkdir -p "$test_folder_path"
# Clear the target folders if they are not empty
rm -rf "$train_folder_path"/*
rm -rf "$val_folder_path"/*
rm -rf "$test_folder_path"/*
# Set the split ratios (adjust these values as per your requirements)
train_ratio=0.7
val_ratio=0.2
test_ratio=0.1
# Get the total number of files in Folder A
total_files=$(ls "$folder_a_path" | wc -l)
# Calculate the number of files for each split
train_files=$(awk "BEGIN {printf \"%.0f\n\", $train_ratio * $total_files}")
val_files=$(awk "BEGIN {printf \"%.0f\n\", $val_ratio * $total_files}")
test_files=$(awk "BEGIN {printf \"%.0f\n\", $test_ratio * $total_files}")
# Move files from Folder A to the train folder
ls "$folder_a_path" | shuf -n $train_files | xargs -I {} mv "$folder_a_path/{}" "$train_folder_path"
# Move files from Folder A to the val folder
ls "$folder_a_path" | shuf -n $val_files | xargs -I {} mv "$folder_a_path/{}" "$val_folder_path"
# Move the remaining files from Folder A to the test folder
mv "$folder_a_path"/* "$test_folder_path"
echo "Files divided successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment