# Need help with -exec cp command.

**URL:** <https://community.unix.com/t/need-help-with-exec-cp-command/210896>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [September 2, 2008, 12:45pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896 "2008-09-02T12:45:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 2, 2008, 12:45pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/1 "2008-09-02T12:45:32Z")

</div>

I have a ksh script that contains the following:

find /dir1/dir2 -type f -name "FILE.\*" -newer /dir1/dir2/afterme.txt -exec cp /dir1/dir2/dir3 {} \;

When I run it from the cli, it runs fine. When I run it from the ksh script I get  
find: missing argument to `-exec'

I also tried -exec cp {} ... and I still got the same error. What's the missing argument?

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [September 2, 2008, 1:07pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/2 "2008-09-02T13:07:24Z")

</div>

Assuming you want to copy the files to the directory /dir1/dir2/dir3:

```nohighlight
find /dir1/dir2 -type f -name "FILE.*" -newer /dir1/dir2/afterme.txt -exec cp {} /dir1/dir2/dir3 \;

```

Regards

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 2, 2008, 3:04pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/3 "2008-09-02T15:04:29Z")

</div>

At the bottom of my first post, I mentioned that I already tried it that way. Still got the same error.

Anyone else?

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 3, 2008, 12:23am UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/4 "2008-09-03T00:23:18Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![jaiankur](https://community.unix.com/letter_avatar/jaiankur/32/6_449c533f586939b6e5f246632a46421f.png) [@jaiankur](https://community.unix.com/u/jaiankur)\
**Post date:** [September 3, 2008, 8:58am UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/5 "2008-09-03T08:58:04Z")

</div>

Might the below script will help you ;

find /dir1/dir2 \(-type f -name "FILE.\*" -a -newer /dir1/dir2/afterme.txt \) -exec cp {} /dir1/dir2/dir3/ \;

Regards,

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 3, 2008, 9:50am UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/6 "2008-09-03T09:50:10Z")

</div>

> [@jaiankur](#):
>
> Might the below script will help you ;
> 
> find /dir1/dir2 \(-type f -name "FILE.\*" -a -newer /dir1/dir2/afterme.txt \) -exec cp {} /dir1/dir2/dir3/ \;
> 
> Regards,

Thanks but nah. It returned this error:

find: invalid predicate `(-type'

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [September 3, 2008, 1:49pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/7 "2008-09-03T13:49:05Z")

</div>

You need a space after the opening parenthesis. It's not clear why you were getting the error message in the first place but maybe just retyping a different command coincidentally fixes whatever was wrong (missing characters? Random invisible characters?)

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 5, 2008, 10:02am UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/8 "2008-09-05T10:02:42Z")

</div>

> [@era](#):
>
> You need a space after the opening parenthesis. It's not clear why you were getting the error message in the first place but maybe just retyping a different command coincidentally fixes whatever was wrong (missing characters? Random invisible characters?)

I took your advice and retyped. Still getting the same error. If I type the command it works. If I run it from the ksh script I get the error:

find: missing argument to `-exec'

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 6, 2008, 10:09pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/9 "2008-09-06T22:09:20Z")

</div>

Hello,

I really still need some help in getting this script to work.

If I type out the command it works. If I put the exact same command in a .ksh script, I get the error:

Find: missing argument to `-exec`.

Why would it do this in the script and not at the command line?

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 7, 2008, 4:19am UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/10 "2008-09-07T04:19:21Z")

</div>

First question:  
Does the first line of the script look like this?

```nohighlight
#!/bin/ksh

```

Next question:  
does the output of this show any weird characters on the problem line

```nohighlight
od -c myscript.sh

```

Finally - cut and paste (do not retype) the whole line that is a problem so we can read it. Include full command line above it and below it. Please give us the OS and shell -- ksh88 or ksh93.

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 7, 2008, 4:40am UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/11 "2008-09-07T04:40:33Z")

</div>

Or a workaround

```nohighlight
find /dir1/dir2 -type f -name "FILE.*" -newer /dir1/dir2/afterme.txt | \
while read filename 
do
    cp $filename /dir1/dir2/dir3/
done

```

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [September 7, 2008, 7:03am UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/12 "2008-09-07T07:03:33Z")

</div>

I'm with Jim on this. The .ksh extension does not at all effect which shell will be invoked. If it's a sh with a different PATH than you run interactively, you could be getting another version of find than you get at the command line. Another thing to watch out for is DOS carriage returns in the script. You're not writing it on a Windoze machine and uploading it to your Unix account, are you?

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 8, 2008, 12:25pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/13 "2008-09-08T12:25:10Z")

</div>

No, I used vi to create the file. I had the #!/bin/ksh at the top of the line but I was getting this error:

: bad interpreter: No such file or directory

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 8, 2008, 12:32pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/14 "2008-09-08T12:32:47Z")

</div>

> [@jim mcnamara](#):
>
> Or a workaround
> 
> ```plaintext
> find /dir1/dir2 -type f -name "FILE.*" -newer /dir1/dir2/afterme.txt | \
> while read filename 
> do
> cp $filename /dir1/dir2/dir3/
> done
> 
> ```

Here is the error I get when I run the above from the script:

: command not founde 1:  
./scriptname.ksh: line 7: syntax error: unexpected end of file

I just don't get it. From the command line it works. From ./scriptname.ksh ERROR!

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [September 8, 2008, 1:05pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/15 "2008-09-08T13:05:39Z")

</div>

Well why not tell what OS you are running?  
Then type which ksh So we know what to put on the first line...  
Then if it works fine on command line, I believe you have typos in your script...  
I would recall the command line AND type v (call vi...) then save the command line with a name such as gogo.ksh, then chmod then ./gogo.ksh.  
If this works, then you have just proven the problem lies in your script

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [September 8, 2008, 1:31pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/16 "2008-09-08T13:31:18Z")

</div>

if #!/bin/ksh does not work, then you prolly do not have the original korn shell.  
what is the output of:

```nohighlight
 echo $0 $SHELL 

```

from your command line?

It really is hard to help if you cannot give us what we ask...

---

<div class="post-metadata">

**Author:** ![avronius](https://community.unix.com/letter_avatar/avronius/32/6_449c533f586939b6e5f246632a46421f.png) [@avronius](https://community.unix.com/u/avronius)\
**Post date:** [September 8, 2008, 2:23pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/17 "2008-09-08T14:23:52Z")

</div>

> [@](#):
>
> No, I used vi to create the file. I had the #!/bin/ksh at the top of the line but I was getting this error:
> 
> : bad interpreter: No such file or directory

Try typing:

```plaintext
which ksh

```

Then, enter the correct bath to ksh in your script header.

On my host, ksh is in /usr/bin/ksh. My script header would be:

```plaintext
#!/usr/bin/ksh

```

edit: added code tags

note: This simply encapsulates the responses of vbe and jim mcnamara

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 8, 2008, 4:53pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/18 "2008-09-08T16:53:46Z")

</div>

> [@jim mcnamara](#):
>
> if #!/bin/ksh does not work, then you prolly do not have the original korn shell.  
> what is the output of:
> 
> ```plaintext
> echo $0 $SHELL 
> 
> ```
> 
> from your command line?
> 
> It really is hard to help if you cannot give us what we ask...

This is the output:

-bash /bin/bash

Also, the OS is Redhat 9 (Shrike).

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [September 9, 2008, 12:45pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/19 "2008-09-09T12:45:36Z")

</div>

So have you tried now with 1rst line:  
#!/bin/bash

---

<div class="post-metadata">

**Author:** ![bbbngowc](https://community.unix.com/letter_avatar/bbbngowc/32/6_449c533f586939b6e5f246632a46421f.png) [@bbbngowc](https://community.unix.com/u/bbbngowc)\
**Post date:** [September 9, 2008, 2:46pm UTC](https://community.unix.com/t/need-help-with-exec-cp-command/210896/20 "2008-09-09T14:46:59Z")

</div>

> [@vbe](#):
>
> So have you tried now with 1rst line:  
> #!/bin/bash

Yeah I tried that and I still got: : bad interpreter: No such file or directory

I also tried #!/usr/bin/bash

Still got the same thing. Thanks for the help so far, keep the suggestions coming.

[Next page](https://community.unix.com/t/need-help-with-exec-cp-command/210896.md?page=2)
