Skip to content

Instantly share code, notes, and snippets.

@katronai
Last active October 12, 2017 23:18
Show Gist options
  • Save katronai/ba534553ad0eb186f03dea252ad9734e to your computer and use it in GitHub Desktop.
Save katronai/ba534553ad0eb186f03dea252ad9734e to your computer and use it in GitHub Desktop.
Bash script for finding and merging all text files under a directory and its all subdirectories by grouping their parent directory names
outputLocation="/myDrive/myWorkingDirectory" &&
cd "$outputLocation" &&
for dir in $(find -type d)
do
cat "$dir"/*.txt >> "$outputLocation"/${dir##*/}.txt
done
############################################################
# for a folder structure like this #
# myRootDirectory #
# |mySubDirectory1 #
# |part0001.txt #
# |part0002.txt #
# |mySubDirectory2 #
# |part0001.txt #
# |part0002.txt #
############################################################
# here is the example script for #
# merging all files starting with 'part' #
# and grouping them into output files #
# #
# myLocation="/d/Users/myUser/SparkDataResults" && #
# cd "$myLocation" && #
# for dir in $(find -type d) #
# do #
# cat "$dir"/part* >> "$myLocation"/${dir##*/}.txt #
# done #
############################################################
# here is the output files containing the merged content #
# of relevant files under the root directory #
# myRootDirectory #
# |mySubDirectory1.txt #
# |mySubDirectory2.txt #
# |mySubDirectory1 #
# |part0001.txt #
# |part0002.txt #
# |mySubDirectory2 #
# |part0001.txt #
# |part0002.txt #
############################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment