Python Fabric - Remove files matching a pattern in a directory w/ spaces?
bash, fabric, regex, unix
Solution
Found a solution, edited in the answer to the question above as 'EDIT 2'
I didn't realize there was a `cd` context manager in Fabric, so using that I found a solution:
from fabric.api import cd
with cd('/myfolder/name with spaces/files/'):
sudo('rm -f *.pyc')
I also discovered a separate issue: using `rm *.pyc` caused Fabric to fail because there were no *.pyc files in the directory, so using `rm -f *.pyc` allowed the command to fail silently and allow Fabric to continue.
Problem
I'm trying to remove all files in a folder /myfolder/name with spaces/files/ that have an extension like *.pyc, but leave other files in the same folder untouched. I tried running: ``` $ rm "/myfolder/name with spaces/files/*.pyc" ``` but I get a `"No such file or directory"` error. I also tried running: ``` $ rm "/myfolder/name with spaces/files"*.pyc ``` but this seems to fail if there are no files with the *.pyc extension in the folder (it works when there are *.pyc files present). Escaping the spaces doesn't seem to work either. What would be the best way to do this (assuming I can't rename the path)? EDIT: I found a solution, and I have a follow-up question: I just found that running the below command works, with some inspiration from this similar post: ``` $ find "myfolder/name with spaces/files/" -name *.pyc -exec rm '{}' ';' ``` Is there a way to do the same as above with solely `rm`? Is the above command the best way to do this? My understanding is that in general `-exec` should be used carefully. EDIT 2: A better solution! Some better context: I'm trying to run these commands remotely using Fabric. I didn't realize there was a `cd` context manager, but using that I found a solution: ``` from fabric.api import cd with cd('/myfolder/name with spaces/files/'): sudo('rm -f *.pyc') ``` I also discovered a separate issue: using `rm *.pyc` caused Fabric to fail because there were no *.pyc files in the directory, so using `rm -f *.pyc` allowed the command to fail silently and allow Fabric to continue.