6

I'm trying to execute the following command using subprocess module (python)

/usr/bin/find <filepath> -maxdepth 1 -type f -iname "<pattern>" -exec basename {} \;

But, it gives the following error :

/usr/bin/find: missing argument to `-exec'

I am guessing it's to do with escaping some characters. But not getting how to get over this.

Any help is appreciated. Thanks.

1
  • 7
    Please provide the line of code where you launch the subprocess Commented Mar 21, 2012 at 6:28

3 Answers 3

15

An answer on another question helped: https://stackoverflow.com/a/15035344/971529

import subprocess

subprocess.Popen(('find', '/tmp/mount', '-type', 'f',
              '-name', '*.rpmsave', '-exec', 'rm', '-f', '{}', ';'))

The thing I couldn't figure out was that the semi-colon didn't need to be escaped, since normally the semi-colon is interpreted by bash, and needs to be escaped.

In bash this equivelent is:

find /tmp/mount -type f -name "*.rpmsave" -exec rm -f {} \;
Sign up to request clarification or add additional context in comments.

Comments

0

One more hint: Using the syntax r'bla' allows using backslashs without having to quote them:

r'... -exec basename {} \;'

Provides better readability.

Comments

-2

remember escaping " is required and also escaping \ used before ; is also required

your command might look something like:

p1 = subprocess.Popen(["/usr/bin/find", "<filepath> -maxdepth 1 -type f -iname \"<pattern>\" -exec basename {} \\;"])
p1.communicate()

4 Comments

If the OP is using single quotes (') for the string, there is no need to escape the double quotes.
Thanks. Single quotes around the whole command string worked.
Even though you succeeded, could you provide the original code @shruthi? And how you fixed it?
Also note: This doesn't really work. It will interpret the whole second arguement (from <filepath> to \\;) as the path, which is not what you expect, or want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.