Copying files with multiple extensions
Brace expansion will get the job done. man bash
and search for Brace Expansion
.
cp *.{txt,jpg,png} destination/
EDIT:
In keeping with the OP's request, the command above was missing the verbose option:
cp -v *.{txt,jpg,png} destination/
Related videos on Youtube
eikonal
Updated on September 18, 2022Comments
-
eikonal over 1 year
I would like to copy files with multiple extensions to a single destination directory.
For example, I can use the following command to copy all .txt files in the working directory to a directory called
destination
:cp -v *.txt destination/
And I can use the following to copy all .png directories in the working directory to
destination
:cp -v *.png destination/
But it's time consuming to type these as separate commands (even with the use of command history). So, is there any way that I can tell
cp
to copy files with either the pattern*.txt
or the pattern*.png
todestination
? Ideally, I would like to be able to specify more than two patterns -- like instructingcp
to copy all*.txt
or*.png
or*.jpg
files todestination
, for example.I'm sure that all of this is possible using a shell script -- I'm using
bash
, for example -- but is there any way to accomplish it more simply, just from the console? Could I somehow use brace expansion to do it?I know that it is possible to copy all files in the working directory except those matching certain specified patterns, but since my working directory contains far more file extensions that I don't want to copy than those I do, that would be a pain.
Do you have any thoughts on this?
-
Admin over 9 years
cp -v *.txt *.png destination/
?
-
-
sergiol over 7 yearsCan I do this on Windows?
-
BradGreens about 5 yearsIf I do
cp data/images/*.{jpg,jpeg,png,mp4} destination/
and anmp4
file does not exist, I get aNo such file or directory
error which breaks the script. Can I make the multiple extensions gracefully handle any missing formats? -
Timothy Martin about 5 years@BradGreens You should really post a new question rather than asking here in the comments. Your question may even have an answer already. Posting a new question will not only get you an answer but it will then be searchable by others who need the same help.
-
yoyoma2 over 2 years@BradGreens Try redirecting standard error to the null device
cp -v *.{txt,jpg,png} destination/ 2>/dev/null
which doesn't affect standard output.