user1032187
user1032187

Reputation: 211

Copy all files excluding one file in shell script

I am new to shell scripting.

I have a command which copies all tml/xml files in the current directly to another as below:

cp -f *.[tx]ml $path

But I need to exclude one file (excludeme.xml) while executing the above command.
I tried the below command but did not work.

find . -name "excludeme.xml" | xargs -0 -I {} cp -f *.[tx]ml $path

Upvotes: 1

Views: 1566

Answers (3)

glenn jackman
glenn jackman

Reputation: 246799

If this is bash then

shopt -s extglob
cp !(excludeme).[tx]ml destination 

Upvotes: 2

William Pursell
William Pursell

Reputation: 212248

Try:

find . ! -name excludeme.xml | ...

or

ls *.[tx]ml | while read -r file; do 
    test x"$file" = xexcludeme.xml || cp -f "$file" "$path"
done

Note the leading 'x'; it is probably not necessary in modern shells, but will prevent errors when the file name begins with '-'.

Upvotes: 2

btmorex
btmorex

Reputation: 496

ls *.[tx]ml | grep -v excludeme.xml | xargs cp -f -t $path

Upvotes: 0

Related Questions