cp aaa*.png /some/destdir
This would match all filenames starting with the string aaa
and ending in the string .png
and copy them all to the directory /some/destdir
. The *
would match any number of any characters in the middle of the name.
This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.
In that case, use something like the following loop:
for name in aaa*.png; do
cp "$name" /some/destdir
done
This would copy the files one by one.
A more efficient method for many thousands of files would be (using GNU cp
with its -t
option):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir {} +
Or (without GNU cp
):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh {} +
This last find
command would find all regular files (-type f
) under the current directory (only, due to -maxdepth 1
) whose names matches the pattern aaa*.png
, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.
More on find
using -exec
: Understanding the -exec option of `find`
cp
– user1794469 Jan 22 '19 at 00:21grep
to apply would be the put the names into a text file before usinggrep
which isn't necessary whenfind
is already available. – Nasir Riley Jan 22 '19 at 00:30