Copying a Batch of Files
December 14, 2015
December 14, 2015
In a project folder, what is the easiest way to copy a batch of files (with a common prefix / suffix) into another directory?
Consider this example: Suppose we have the following files stored in multiple folders:
20151214a/test01.txt
20151214b/test02.txt
20151214c/test03.txt
...
20151214z/test26.txt
We want to copy those 26 text files into a single folder "build"; at the same time, we want to rename them to:
build/01.txt
build/02.txt
...
build/26.txt
In a Makefile, we can do
for file in 20151214*/test*.txt; do
cp -a "$file" build/${file#*/test}
done
The pattern ${file#*/test} removes the prefix "test" in all the matched files.