Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I am in a directory called /usr/share/tcl8.3/encoding, what command would copy all files begining "cp" that also contain an even number (from the following list):

cp1250.enc  cp1255.enc  cp737.enc  cp857.enc  cp864.enc  cp932.enc
cp1251.enc  cp1256.enc  cp775.enc  cp860.enc  cp865.enc  cp936.enc
cp1252.enc  cp1257.enc  cp850.enc  cp861.enc  cp866.enc  cp949.enc
cp1253.enc  cp1258.enc  cp852.enc  cp862.enc  cp869.enc  cp950.enc
cp1254.enc  cp437.enc   cp855.enc  cp863.enc  cp874.enc
share|improve this question

3 Answers 3

up vote 5 down vote accepted

Have you tried this?

cp cp*[24680].enc destination
share|improve this answer
    
cp*[24680].* for mixed extensions –  Rob Jun 5 '12 at 16:51
    
SOLVED: This works. Thank you so much! –  user98496 Jun 5 '12 at 18:11

Try: cp cp*[02468]* /path/to/dest/

share|improve this answer
    
Thanks. I was originally inclined to do that command too, but it also captures numbers that aren't even (ie: cp1251.enc). I should only be captureing cp files followed by a number that can be divided evenly. Any other suggestions? –  user98496 Jun 5 '12 at 15:34
    
cp cp*[02468].* should work if the extensions aren't all the same. –  Rob Jun 5 '12 at 16:51
    
I'd argue that cp1251.enc "contains" an even number, per the wording of the original question, but that's neither here nor there...looks like others have tweaked this to do what you want. –  mboldt Jun 5 '12 at 20:25
    
Hey mbolt. Yes,you are correct. Definitely my bad on the original delivery of the question. I'm thankful though for everyone's thoughtful input toward my quest for an answer :-) –  user98496 Jun 6 '12 at 3:20
    
What does this do? Why trust a command if you don't know what it does. –  Tom Wijsman Jun 11 '12 at 18:30

Command

find . -maxdepth 1 | grep -P "/cp\d*[02468]\.enc$" | xargs -I '{}' cp '{}' destination

How it works

  • find . -maxdepth 1 non-recursively (-maxdepth 1) lists all files in the current directory (.)

  • grep -P "..." matches each line against the regular expression ...

    • / and \.enc are the strings / and .enc.

    • \d* is any number of digits.

    • [02468] is exactly one even digit.

    • $ signals the end of a line.

  • xargs -I '{}' cp '{}' destination executes the command

    cp '{}' destination
    

    where '{}' gets substituted by each line piped from the previous command.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.