From 5b16582597be45cd3e33b64e4e3dee911da5316a Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Thu, 20 Feb 2025 15:10:28 +0100 Subject: [PATCH] flatten script --- README.md | 4 ++++ flatten.sh | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100755 flatten.sh diff --git a/README.md b/README.md index 7d501a7..6d26109 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,7 @@ returns a matrix access token ### convert_md_to_pdf.sh (need to be used with listings-setup.tex in the same folder, xelatex and pandoc are required) convert infile.md in the current folder to a pdf file (outfile.pdf) with code blocks in red boxes. + +### flatten.sh +move all the files in $1 subdirectories to $1. +**Careful**, this operation is irreversible and manually moving back all the files to where they were supposed to be will be a pain if you flatten your home folder. diff --git a/flatten.sh b/flatten.sh new file mode 100755 index 0000000..8b579a6 --- /dev/null +++ b/flatten.sh @@ -0,0 +1,23 @@ +#!/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."