2

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.

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

bash: !: event not found
Try after shopt -s extglob.
Its not working while I am writing into the script, any idea?
did you take Kamil's advice?
3

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.

Comments

0

Try

find . -maxdepth 1 -type f \( ! -iname "*.txt" \) -print0 | xargs -0 cp {} /destination

The -print0 and -0 are needed to handle files that could have spaces in the name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.