Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find command with exec in python subprocess gives error

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.

like image 620
shruthi Avatar asked Mar 21 '12 06:03

shruthi


1 Answers

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 {} \;
like image 95
isaaclw Avatar answered Sep 21 '22 20:09

isaaclw