Useful-scripts/flatten.sh
2025-02-20 15:10:28 +01:00

24 lines
678 B
Bash
Executable File

#!/bin/bash
src_dir=$1
cd "$src_dir" || { echo "Directory not found: $src_dir"; exit 1; }
# Find all files and move them to the root directory
find . -type f -exec sh -c '
for file_path do
file_name=$(basename "$file_path")
target_file="./$file_name"
if [[ "$file_path" != "$target_file" ]]; then
if [ -e "$target_file" ]; then
count=1
while [ -e "./$file_name.$count" ]; do
count=$((count + 1))
done
target_file="./$file_name.$count"
fi
mv "$file_path" "$target_file"
fi
done
' sh {} +
echo "Flattening completed."