-exec cmd in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting -exec cmd in ksh script
# 1  
Old 09-16-2010
-exec cmd in ksh script

Hi,

I discovered the following single-line script works very well to cp a large number of files from a source directory to a destination directory while avoiding the "argument list too large" error:

Code:
# cpmany - copy large number of files
# Takes two parameters - source dir, destination dir
# Copies ALL regular files from source dir to destination dir
# Does not traverse subdirectories in source
# Does not cp hidden files
#
find $1 -maxdepth 1 -type f -name '*' -exec cp -v {} $2 \;

I'm not fluent in the ksh, so I'm hoping someone who is can explain in detail the operation of this script.

1. Since the result of the find cmd is a list of filenames, does the "-exec" command actually spawn a new process for every filename in the list in order to do the cp? Is it a case of spawning a new ksh and then overlaying that new ksh with cp to do the copy of a file in the list?

2. If you remove the single quotes from the *, you get the "argument list too large" error. Exactly why is this?

3. Obviously, the "{}" that follows the cp command means 'use a name from the list' produced by "find". Where can I find more on this construct? It's pretty cool!

4. Why is a final "\" needed at the end of the line, just before the ";"?

5. I've tried running this thing dirrectly from the ksh command prompt (rather than as a script) but I get "missing argument to exec". Why?

6. I've looked and looked for detailed information on the exec cmd in ksh, and what I've found is so limited that I came here to get answers. Is there a difference between "exec" and "-exec" in this context?

Thanks in advance for your help. I hate 'black boxes' even if they work very well.

Last edited by Scott; 09-16-2010 at 05:06 PM.. Reason: Please use code tags
# 2  
Old 09-16-2010
Follow the answers:
1. Each file returned from the find is copied with the cp command. I mean, it "spawns" a cp command;
2. Do you really need the -name argument? Try:
Code:
find $1 -maxdepth 1 -type f -exec cp -v {} $2 \ ;

3. I think find man pages have information about, it is just a placeholder. You could also try the "xargs" command.
4. It is part of the find command structure, but it means that the -exec argument finishes.
5. I am not sure if it is a typo, but in the command you post there is a missing space between the backslash and the semi-colon.
6. exec is a shell builtin, try man exec.

I hope it helps!

Regards!
# 3  
Old 09-16-2010
Quote:
Originally Posted by Tanuka
1. Since the result of the find cmd is a list of filenames, does the "-exec" command actually spawn a new process for every filename in the list in order to do the cp?
Yes
Quote:
Is it a case of spawning a new ksh and then overlaying that new ksh with cp to do the copy of a file in the list?
No. -exec is directly launching the "cp" command with its arguments. There is no ksh involved here.

Quote:
2. If you remove the single quotes from the *, you get the "argument list too large" error. Exactly why is this?
Because the shell expands * and you have too many of them in the current directory.

Quote:
3. Obviously, the "{}" that follows the cp command means 'use a name from the list' produced by "find". Where can I find more on this construct? It's pretty cool!
In the "find" manual page and in the various web pages about it.

Quote:
4. Why is a final "\" needed at the end of the line, just before the ";"?
Because ';' is required to delimit the end of the -exec clause but ';' alone would be picked by the current shell as a command separator if not escaped by '\'.

Quote:
5. I've tried running this thing dirrectly from the ksh command prompt (rather than as a script) but I get "missing argument to exec". Why?
Not sure, did you replace $1 and $2 by something useful ?

Quote:
6. I've looked and looked for detailed information on the exec cmd in ksh, and what I've found is so limited that I came here to get answers. Is there a difference between "exec" and "-exec" in this context?
Absolutely. exec is a ksh builtin command while "-exec" is an unrelated find option.

---------- Post updated at 18:38 ---------- Previous update was at 18:36 ----------

Quote:
Originally Posted by felipe.vinturin
2. Do you really need the -name argument?
It is needed to fulfill this requirement:
Code:
 # Does not cp hidden files



---------- Post updated at 18:41 ---------- Previous update was at 18:38 ----------

Quote:
Originally Posted by felipe.vinturin
5. I am not sure if it is a typo, but in the command you post there is a missing space between the backslash and the semi-colon.
That's the other way around. There is an extra space between the backslash and the semicolon in your suggestion. That space is defeating the backslash requirement.
This User Gave Thanks to jlliagre For This Post:
# 4  
Old 09-16-2010
jlliagre,

Your answers make perfect sense! This is very much appreciated. I'll study the "find" command.

This was sort of 'wigging me out' until you explained matters. Here in this script we have a seemingly bizarre example of connecting the output of one command to the input of another, and all without the use of a pipe! But of course "find" with its "-exec" option is doing much for us in this regard - so things make sense after all.

Thanks again!

---------- Post updated at 01:57 PM ---------- Previous update was at 10:56 AM ----------

One final question:

Given the script:

Code:
    find $1 -maxdepth 1 -type f -name '*' -exec cp -v {} $2 \;

which copies ALL regular files from $1 (source dir) to $2 (dest. dir)

How can I make this script take a file-pattern for -name instead of using '*', so that I can use the script to cp only the files I want?

Code:
          find $1 -maxdepth 1 -type f -name $2 -exec cp -v {} $3 \;

does not work if you use a wildcard like * in your 2nd parameter. What needs to be done to allow the use of a special char like * ?

Last edited by Scott; 09-16-2010 at 05:06 PM.. Reason: Please use code tags
# 5  
Old 09-16-2010
Put it between double-quotes!

Code:
find $1 -maxdepth 1 -type f -name "$2" -exec cp -v {} $3 \;

# 6  
Old 09-16-2010
Hi Filipe,

When I use the double quotes around the positional paramter like this ("$2") I get wierd results when I run the script.

For example, if I have a directory with the following files in it:

Code:
    cpmany
    file1
    file2
    file3
    file4

and I'm cd'ed into this directory and I invoke cpmany like this:

Code:
./cpmany  .  f*  /joe/stuff/

what happens is file1 is copied to file2, with no files copied to the dest dir I specified on the command line, and after doing the one copy operation the script finishes, with no errors.

It should have copied all four files to the dest dir - but it does not do so. What is wrong with my syntax?

Last edited by Scott; 09-16-2010 at 05:34 PM..
# 7  
Old 09-16-2010
Quote:
Originally Posted by Tanuka
What is wrong with my syntax?
That should be:

Code:
./cpmany . "f*" /joe/stuff/

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

2. Shell Programming and Scripting

Run few perl script in CMD using script

Hi, I have few PERL script which I have to run in CMD. I want to create a new script which will take care of running all these scripts in CMD.:confused: can any one suggest which script will be appropriate to solve my problem. Thanks in advance. (1 Reply)
Discussion started by: arup1980
1 Replies

3. Shell Programming and Scripting

exit status from ksh script exec from java using runtime

how do i get the exit status from a ksh or perl script executed in a java program using Runtime? (1 Reply)
Discussion started by: twk
1 Replies

4. Red Hat

setup sudo for cmd exec w/o password

i need to set up a user to execute a restricted command as another user and to be able to do so without entering a password. I understand the security concerns but let's not go there, unless you are really compelled to do so... The directive to permit is that I believe should work and did add to... (2 Replies)
Discussion started by: twk
2 Replies

5. Shell Programming and Scripting

Unix cmd prompt how to get old cmd run?

Hi, I am using SunOS I want to serch my previous command from unix prompt (like on AIX we can search by ESC -k) how to get in SunOs urgent help require. (10 Replies)
Discussion started by: RahulJoshi
10 Replies

6. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

7. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies

8. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

9. Shell Programming and Scripting

How can I execute own ksh function in find -exec

Hi, I wrote a smiple ksh function send_notification() { ... } and want to execute it on each file, matched by the find command. I tried: find / -name "*.err" -mtime -8 -exec send_notification {} \; but it doesn't work. What should I do? I work in ksh on Hp-Ux. Regards, Pit (11 Replies)
Discussion started by: piooooter
11 Replies

10. UNIX for Dummies Questions & Answers

man <cmd> >> cmd.txt

I've noticed most of my postings here are because of syntax errors. So I want to begin compiling a large txt file that contains all the "man <cmd>" of the commands I most have problems with. I ran a "man nawk >> nawk.txt" but it included a header/footer on each "page". Anyone know how I'd be... (6 Replies)
Discussion started by: yongho
6 Replies
Login or Register to Ask a Question