0

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
Bobby
  • 8,974
user98496
  • 723

3 Answers3

5

Have you tried this?

cp cp*[24680].enc destination
Dennis
  • 49,727
broomdodger
  • 3,130
1

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

  • 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 05 '12 at 15:34
  • cp cp*[02468].* should work if the extensions aren't all the same. – Rob Jun 05 '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. – Mikey Boldt Jun 05 '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 06 '12 at 03:20
  • What does this do? Why trust a command if you don't know what it does. – Tamara Wijsman Jun 11 '12 at 18:30
  • Hey Tom. You would probably have to ask the folks at Red Hat as my question was borne of an exercise (on the cp command) from an RHA 030 workbook exercise. It was the only question in the exercise that I found myself struggling with. Anyway, the answer that Rob gave provided the correct result. Thanks. – user98496 Jun 14 '12 at 18:42
1

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.

Dennis
  • 49,727