find -exec with 2 commands doesn't work (error incomplete staement)


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users find -exec with 2 commands doesn't work (error incomplete staement)
# 1  
Old 09-23-2011
find -exec with 2 commands doesn't work (error incomplete staement)

Hi Gurues,

I need to modify an existing script that uses find to search a folder, and then move its contents to a folder. What I need to do is run gzip on each file after it's moved.

So, I ran this little test:

Put a ls.tar file on my $HOME, mkdir tmp, and then:
Code:
virtuo@tnpmprd01: find . -name '*.tar' -exec \(mv {} tmp; cd tmp; gzip {}\) \;
find: incomplete statement
gzip: {}): No such file or directory
gzip: ;: No such file or directory
virtuo@tnpmprd01:

with parenthesis off, same error:
Code:
virtuo@tnpmprd01: find . -name '*.tar' -exec mv {} tmp; cd tmp; gzip {} \;
find: incomplete statement
ksh: tmp:  not found
gzip: {}: No such file or directory
gzip: ;: No such file or directory
virtuo@tnpmprd01:

Any clue?

Thanks a lot,

Leo

Last edited by Scott; 09-23-2011 at 11:58 AM.. Reason: Please use code tags...
# 2  
Old 09-23-2011
The first semicolon after -exec is not escaped, so the shell interprets it as the end of the find command. find then complains because its -exec is not properly terminated. Then the cd tmp fails because tmp does not exit. Then gzip is annoyed because it can't find any files named {} and ;.

However, prematurely terminated find command aside, you'd still have problems; find's -exec primary does not execute a shell unless you ask it too. For example, the following will not work:

Code:
find . -name '*.tar' -exec 'cmd1 ' {} '; cmd2 ' {} '; cmd3 ' {} \;

At best, that would call cmd1 with everything that follows it as an argument.

For what you're trying to do, you need to ask -exec to execute a shell to process your sequence of commands (which is a small shell script):

Code:
find . -name '*.tar' -exec sh -c 'cmd1 "$1"; cmd2 "$1"; cmd3 "$1"' sh {} \;


Regards and welcome to the forum,
Alister

Last edited by alister; 09-23-2011 at 12:04 PM..
These 2 Users Gave Thanks to alister For This Post:
# 3  
Old 09-23-2011
Thanks Alister,

From the command line it works ok. However, from inside my sh script, it doesn't, no matter how do I scape the quotes, doubles, singles, etc.

Currently I have in the script:
Code:
${find} $_dir -type f -exec sh -c \'${mv} \"\$1\" ${_archive_dir}; cd ${_archive_dir}; ${gzip} \"\$1\"\' sh {} \;

and the scripts is doing:
Code:
+ /usr/bin/find /appl/virtuo/var/loader/spool/ericssonumtsmgw_r51/R51/bad -type f -exec sh -c '/usr/bin/mv "$1" /appl/virtuo/var/loader/archive/20110923093606/ericssonumtsmgw_r51-bad
/usr/bin/find: incomplete statement
+ cd /appl/virtuo/var/loader/archive/20110923093606/ericssonumtsmgw_r51-bad
+ /usr/bin/gzip "$1"' sh {} ;
gzip: "$1"': No such file or directory
gzip: sh: No such file or directory
gzip: {}: No such file or directory
gzip: ;: No such file or directory

As You see, only the first command of the SH is being executed...

You know what could be wrong?

Thanks again,

Leo

---------- Post updated at 09:38 AM ---------- Previous update was at 09:36 AM ----------

Forgot to add that the directory is empty... the mv didn't worked, the cd did, and the gzip is not translating "$1" to the file name...

Moderator's Comments:
Mod Comment You also forgot to add code tags. It's really rather simple Smilie

Last edited by Scott; 09-23-2011 at 12:47 PM.. Reason: Code tags
# 4  
Old 09-23-2011
Quote:
Originally Posted by llagos
Currently I have in the script:
Code:
${find} $_dir -type f -exec sh -c \'${mv} \"\$1\" ${_archive_dir}; cd ${_archive_dir}; ${gzip} \"\$1\"\' sh {} \;

Instead, try:
Code:
${find} $_dir -type f -exec sh -c '"'"${mv}"'" "$1" "'"${_archive_dir}"'"; cd "'"${_archive_dir}"'"; "'"${gzip}"'" "$1"' sh {} \;


Alternatively, you can pass all the variables as arguments to the invoked sh:
Code:
${find} $_dir -type f -exec sh -c '"$1" "$4" "$2"; cd "$2"; "$3" "$4"' sh "${mv}" "${_archive_dir}" "${gzip}" {} \;


However, given that you're trying to use variables defined in the current sh execution environment, it's probably easiest (and more efficient than spawning a shell per file found) to just pipe pathnames from find into a while read loop.

Code:
${find} $_dir -type f | while read fn; do
    ${mv} "$fn" ${_archive_dir}
    cd ${_archive_dir}
    ${gzip} "$fn"
    cd -
done


It's always best to provide a full-context in the original problem statement. It often saves everyone involved from wasted time and effort. If none of those suggestions work (be very careful with the quoting), then provide your entire script so that we can have a proper look.

Regards,
Alister

Last edited by alister; 09-23-2011 at 01:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why regex pattern doesn't work in find?

$ find /opt/data_* -maxdepth 3 -type d -name "main*" 2> /dev/null /opt/data_025/maindblogs /opt/data_026/maindblogs /opt/data_027/maindblogs /opt/data_028/maindblogs $ find /opt/data_* -maxdepth 3 -type d -name "rvlogs*" 2> /dev/null /opt/data_002/prod/rvlogs2_archive... (4 Replies)
Discussion started by: urello
4 Replies

2. UNIX for Dummies Questions & Answers

Using find -exec with multiple commands :(-

Hi all, Am wanting to do a ls -l of the files and do a cat of it at the same time, ideally, I am hoping that the following work but obvisouly it is not working to what I am wanting it to ... hu hu hu :wall: find . -name "BACKUP_TIMESTAMP.log" -exec "ls -l basename {} ; cat {}" \; ... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Shell Programming and Scripting

using rm with find -exec gives error

Greetings, Everytime I use rm with find I get errors like find: ./test: No such file or directory For exemple : hostname> mkdir test hostname> ls test hostname> find . -type d -name test -exec rm -rf {} \; find: ./test: No such file or directory hostname>ls hostname> echo $? 1 ... (2 Replies)
Discussion started by: Sekullos
2 Replies

4. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

5. UNIX for Dummies Questions & Answers

How to run multiple piped commands in a find -exec statement?

I can't get this to work. Running a single command works fine: find . -name "*.dat" -exec wc -l '{}' \; gives me the file name and number of lines in each .dat file in the directory. But what if I want to pipe commands, e.g. to grep something and get the number of lines with that pattern... (3 Replies)
Discussion started by: DJR
3 Replies

6. AIX

"/" doesn't work on command prompt for searching commands last typed

When I use "/" to look for a particular command that I typed in the current session it says D02:-/home/user1/temp> /job ksh: /job: not found. D02:-/home/user1/temp> previously it used to fetch all the commands which had job in it.. for example subjob, endjob, joblist etc... may I... (7 Replies)
Discussion started by: meetzap
7 Replies

7. UNIX for Dummies Questions & Answers

find .... -exec rm .... \; works, but gives an error

I am using find to get all the instances of a given directory name, then remove those directories with a -exec rm command. here is an example: mkdir -p foo/bar find foo -name bar -exec rm -rf {} \; foo will get deleted as I desired, however this pops out on standard error: find: foo/bar:... (3 Replies)
Discussion started by: Afifi
3 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

find command with exec doesnt work

Hi There, I have a script which finds for log files and removes them if the file has changed in the last day. The script runs fine without errors. The log file is still there. So, I decided to print the find command and run the command outside the script. Getting "Incomplete statement" Can you... (6 Replies)
Discussion started by: funtochat2002
6 Replies

10. UNIX for Dummies Questions & Answers

How to find size of each subdirect? du -sk | ls doesn't work

Using solaris 2.5.1, and how can I get a summary of the size of each subdirectory, say for /export/home, all the users? usually I do a du -sk dirname but I have to manually type in each name, is there a better way? Thanks, (3 Replies)
Discussion started by: kymberm
3 Replies
Login or Register to Ask a Question