Skip to content

Instantly share code, notes, and snippets.

@galatolofederico
Last active September 22, 2020 12:05
Show Gist options
  • Select an option

  • Save galatolofederico/479190ecf14fd2354305e79389932930 to your computer and use it in GitHub Desktop.

Select an option

Save galatolofederico/479190ecf14fd2354305e79389932930 to your computer and use it in GitHub Desktop.
This script takes a folder with a project with multiple files, folders and subfolders and convert it in a project with all the files on the first folder level
#! /bin/bash
# ./project-flattner.sh <source folder> <destination folder>
#
# Some Editorial Management System require LaTeX sources submissions to be on the same folder level.
# This script takes a folder with a project with multiple folders and subfolders and
#
# 1) copies all the files from the source folder to the destination one
# renaming all of them from "path/to/file.ext" to "path_to_file.ext"
# 2) sobsitutes in all the new files all the occurences of
# "path/to/file.ext" to "path_to_file.ext"
#
# The script was originally built for LaTeX projects but it might work in other context too
die() { echo "$*" 1>&2 ; exit 1; }
[[ "$#" -ne 2 ]] && die "$0 <source folder> <destination folder>"
src="$1"
dst="$2"
[[ ! -d "$src" ]] && die "$src does not exists"
[[ ! -d "$dst" ]] && die "$dst does not exists"
subs=$(paste -d " " <(find "$src" -type f -printf "%P\n") <(find "$src" -type f -printf "%P\n" | sed "s/\//_/g"))
echo "$subs" |
while read -r sub
do
src_file=$(echo "$sub" | cut -d" " -f1)
dst_file=$(echo "$sub" | cut -d" " -f2)
cp "$src/$src_file" "$dst/$dst_file"
echo "Renamed $src_file in $dst_file"
done
find "$dst" -type f |
while read -r file
do
echo "$subs" |
while read -r sub
do
src_file=$(echo "$sub" | cut -d" " -f1)
dst_file=$(echo "$sub" | cut -d" " -f2)
sed -i "s/${src_file//\//\\\/}/$dst_file/g" "$file"
done
echo "Updated $file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment