I'm currently having an assignment that wants me to copy a select amount of files. More specifically files that are even using the cp command. The assignment is to copy files that start with cp and are even. This was my command at first cp[24680]* but it returned all files that had an even number in it. I just want files that end in the even number.

example I want to copy only these files:
cp1250.enc cp1256.enc cp852.enc cp864.enc cp932.enc
cp1252.enc cp1258.enc cp860.enc cp866.enc cp936.enc
cp1254.enc cp850.enc cp862.enc cp874.enc cp950.enc.

But the command I make gives me :
cp437.enc cp852.enc cp857.enc cp861.enc cp863.enc cp865.enc cp869.enc
cp850.enc cp855.enc cp860.enc cp862.enc cp864.enc cp866.enc cp874.enc

Recommended Answers

What about:
cp*[24680].enc or cp*[24680].* ?

So if the files are all in the current directory and you want to copy the even numbered ones to a sub-directory called "even" you could do the following:
Using cp (or mv):
cp ./cp*[24680].enc ./even/
or
cp ./cp*[24680].* ./even/

or using the find command:
find ./ -type …

Jump to Post

All 2 Replies

What about:
cp*[24680].enc or cp*[24680].* ?

So if the files are all in the current directory and you want to copy the even numbered ones to a sub-directory called "even" you could do the following:
Using cp (or mv):
cp ./cp*[24680].enc ./even/
or
cp ./cp*[24680].* ./even/

or using the find command:
find ./ -type f -iname "cp*[24680].*" -execdir cp {} ./even/ \;

EDIT:
Note: Unless you specify a maxdepth for find, it will look for files recursively in any sub-directories, which you might not want it to do.
To make it only search in the current directory you can do this:
find ./ -maxdepth 1 -type f -iname "cp*[24680].*" -execdir cp {} ./even/ \;

Your example gets those that start with an even number, not the ones that have an even number at the end of the number string. IE, it will properly return "cp2.enc" as well as "cp4.enc", "cp6*.enc", etc. So, that would include "cp211.enc" and such.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.