tree2flat.sh:
Code: Select all
#!/bin/bash
# Flattens a predetermined tree directory structure.
# The MIT License (MIT)
# Copyright (C) 2015 Squarefractal <squarefractal at gmail {.} com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
echoerr () {
echo "ERROR: $1" >&2
}
print_usage () {
echo "Usage: $0 <input_dir> <output_dir>"
echo "Flattens a predetermined tree directory structure."
exit 0
}
if [[ -z $1 ]] || [[ -z $2 ]] || [[ $1 == "--help" ]]; then
print_usage
fi
input_dir="$(readlink -e "$1")"
output_dir="$(readlink -e "$2")"
if [[ ! -d $input_dir ]]; then
echoerr "Invalid path to localization file"
exit 1
fi
if [[ ! -d $output_dir ]]; then
echoerr "Invalid extraction directory"
exit 1
fi
cd "$input_dir"
find . -type f -name '*.po' | sed -r 's|^./||g' | while read filepath; do
cp -v "$filepath" "$output_dir/$(echo "$filepath" | sed 's|/|!|g')"
done
Code: Select all
#!/bin/bash
# Converts a predetermined flattened directory structure to a tree.
# The MIT License (MIT)
# Copyright (C) 2015 Squarefractal <squarefractal at gmail {.} com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
echoerr () {
echo "ERROR: $1" >&2
}
print_usage () {
echo "Usage: $0 <input_dir> <output_dir>"
echo "Converts a predetermined flattened directory structure to a tree."
exit 0
}
if [[ -z $1 ]] || [[ -z $2 ]] || [[ $1 == "--help" ]]; then
print_usage
fi
input_dir="$(readlink -e "$1")"
output_dir="$(readlink -e "$2")"
if [[ ! -d "$input_dir" ]]; then
echoerr "Invalid input directory."
exit 1
fi
if [[ ! -d "$output_dir" ]]; then
echoerr "Invalid output directory."
exit 1
fi
cd "$input_dir"
find . -type f -name '*.po' | sed -r 's|^./||g' | while read filepath; do
dest="$(echo "$filepath" | sed -r 's|!|/|g;s|(.*)/.*|\1|g')"
fname="$(echo "$filepath" | sed -r 's|.*!||g')"
mkdir -vp "$output_dir/$dest"
cp -v "$filepath" "$output_dir/$dest/$fname"
done
Any developer should really learn bash: it helps a lot!
And batch is not a language, please.