Search code examples
pythonfindsubprocesspython-2.6

find command with subprocess not working with out Shell=True


I have below lines in my code. I have embedded a short a line which fetches the list of files that are older than 10 mins. My sub process have been failing with few errors. It seems to work when I give Shell=True, but I read that it is a very risky to use that option and I am very new to the Python, Don't want to mess up with something I am not understanding. I have tried changing quotes with in and around that find statement, but its not helping me. can you please suggest how could I get the list of files using find command. I have looked into other questions with these find and subprocess combinations, I did not find any wildcard directory matches. I could not find solution for this.

cmd = 'find /myapp/uat/aws/6.3/domains/*/appnodes/*/*/log/bwappnode.log -type f -mmin +10'

 apps_in_proc = subprocess.Popen(cmd,stdout=subprocess.PIPE, universal_newlines=True)

Solution

  • if it works with shell=True, and not without, that means that the pattern is expanded with shell=True.

    To emulate this behaviour just use glob.glob and compose your command argument list like this:

    cmd = ['find'] + glob.glob('/myapp/uat/aws/6.3/domains/*/appnodes/*/*/log/bwappnode.log') + ['-type','f','-mmin','+10']
    

    Which could be written very easily in pure python:

    import glob,os,time
    current = time.time()
    old_files = [x for x in glob.glob("/myapp/uat/aws/6.3/domains/*/appnodes/*/*/log/bwappnode.log") if current - os.path.getmtime(x) > 600]