-2

I cannot delete folder created by php mkdir

for I in `echo $*`
do
    find $I -type f -name "sess_*" -exec rm -f {} \;
    find $I -type f -name "*.bak" -exec rm -f {} \;
    find $I -type f -name "Thumbs.db" -exec rm -f {} \;
    find $I -type f -name "error.log" -exec sh -c 'echo -n > "{}"' -f {} \;
    find $I -type f -path "*/cache/*" -name "*.*" -exec rm -f {} \;
    find $I -path "*/uploads/*" -exec rm -rdf {} \;
done

I want to delete under /uploads/ all files and folders please help me thanks...

8
  • 2
    All I can offer is a guess unless you say what error you observe. The guess: it's probably permissions due to php being run as different user than your find/rm commands. Mar 27, 2012 at 20:21
  • Is the path actually /uploads and exists in the top of your file system?
    – Marc B
    Mar 27, 2012 at 20:21
  • How about sudo rm -r */uploads?
    – Dave
    Mar 27, 2012 at 20:21
  • 1
    Please post the error messages you're seeing. Without that, we can only guess. Mar 27, 2012 at 20:22
  • I that use in nautilus script. I don't see any error message. Right click tihs folder => /var/www and run that nautilus script
    – ali
    Mar 27, 2012 at 20:26

3 Answers 3

1

You should consider changing your find command to use the -o pragma to join your conditions together as the final exec is basically the same. This will avoid recursing the file system repeatedly.

The other answers address your concern about php mkdir. I'll just add that it has nothing to do with the fact it was created with php mkdir rather than any other code or command. It is due to the ownership and permissions.

0

I think this is most likely because php is running in apache or another http server under a different user than you are invoking the bash script. Or perhaps the files uploaded in uploads/ are owned by the http server's user and not the user invoking it.

Make sure that you run the bash script under the same user as your http server.

To find out which user owns which file do:

ls -l

If you run you bash script as root, you should be able to delete it anyway, but that is not recommended.

Update

To run it as root for nautilus script use the following as your nautilus script:

gksudo runmydeletescript

Then put all the other code into another file with the same path as whatever you have put for runmydeletescript and run chmod +x on it. This is extremely dangerous!

2
  • drwxrwxrwx 3 ali ali 4096 2012-03-27 21:22 (..)/uploads/products drwxr-xr-x 2 www-data www-data 4096 2012-03-27 21:22 (..)/uploads/products/11 how to run as root naitulus script?
    – ali
    Mar 27, 2012 at 20:35
  • On debian based systems (like ubuntu) at least you would use gksudo. Maybe you need to make one script that runs your script with gksudo: gksudo myotherscript Mar 27, 2012 at 20:46
0

You should probably add -depth to the command to delete sub-directories of upload before the directory itself.

I worry about the -path but I'm not familiar with it.

Also consider using + instead of \; to reduce the number of commands executed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.