Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid copying specific file format using cp command

Tags:

bash

shell

I am using cp command in ma bash script

cp /source/* /destination 

I just want to avoid copying all .txt file from my source to destination.

like image 547
Query Avatar asked May 15 '26 16:05

Query


2 Answers

If extended globulation is enabled, you can match al files except txt ones:

cp /source/!(*.txt) /destination

*.txt matches all txt files. The !(...) tells it to match everything except what's in the ... part.

like image 58
KamilCuk Avatar answered May 17 '26 04:05

KamilCuk


I don't know if you can do this in copy but the rsync command has this ability. Try

rsync -av /source/* /destination --exclude "*txt".

See https://www.howtogeek.com/168009/how-to-exclude-files-from-rsync/ for some more details and examples.

like image 23
R.W.K. Avatar answered May 17 '26 06:05

R.W.K.