1

I would like to improve my shell script skills. What I want to do is that I will copy all the files in a specific folder into other destination folder and then save them into one file by concatenating them.

Below is my simple code.

path=/Users/sclee/Dropbox/project/java/projects/WaferPointDraw/src/main/resources/pattern
dest=/Users/sclee/Dropbox/journal/clustering_ieee_semi_2020/data/small_datasets/noise200000other2000
output=large_datast_3.txt

mkdir -p $dest; echo "$dest directory is created" for i in find ${path} -iname '*.txt'; do cp $i $dest; done echo "all the files is copied"

cat ${dest}/* > ${dest}/${output} echo "created the combined file"

I can achieve my goals with above codes, but I would like to process all the logic in the for loop. As you can see, the cat command is done apart from the for loop. I would like to process them in the for loop for the simple code.

Any help will be appreciated.

Thanks.

sclee1
  • 179

3 Answers3

0

Oneliner using find :

find "$path" -iname '*.txt' -exec cp {} "$dest" \; -exec sh -c "cat {} >> $dest/$output" \;
annahri
  • 2,075
0

If I understand right you could replace

do
  cp $i $dest;
done

with

do
  cat $i>>${dest}/${output};
done

That appends the findings to your output file

Dbrg
  • 1
0

question: the txt files in $path, are there subdirectories into which you want to descend in order to find all the txt files? If not, you don't need find, for file in "$path"/*.txt will suffice.

In fact, you don't need a loop if all the files are in one directory:

cp -t "$dest" "$path"/*.txt
cat "$path"/*.txt > "$dest/$output"

Note that a variable in braces ${var} is not the same as a quoted variable "$var"

glenn jackman
  • 85,964