Skip to content

Instantly share code, notes, and snippets.

@hschne
Created March 28, 2019 21:23
Show Gist options
  • Save hschne/a3e5db637ab9c5e9e634fc6e5f0c4826 to your computer and use it in GitHub Desktop.
Save hschne/a3e5db637ab9c5e9e634fc6e5f0c4826 to your computer and use it in GitHub Desktop.
Extract Files from Subfolders
#!/usr/bin/env bash
# This script extracts all files with the given name from directories within the current one
# into the given directory, while attaching identifiers to the extracted files.
#
# A file 'Testfile.txt' will be extracted from some folder 'Name_Number_Something/subfolder/asubfolder/' to
# the folder 'out' by executing
#
# ./extract Testfile.txt out
#
# Arguments:
#
# $1 - The file to extract
# $2 - The folder to extract to. Will be created if it doesn't exist
#
main() {
local filename=$1
local destination=$2
for file in **/"$filename"; do
name=$(echo "${file}" | cut -d'_' -f 1)
number=$(echo "${file}" | cut -d'_' -f 2)
cp -v "${file}" "$destination/${name}_${number}_$filename"
done
}
shopt -s globstar
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment