-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...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ali
  • 61
  • 5

3 Answers3

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.

AaronM
  • 2,339
  • 2
  • 17
  • 18
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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
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!

d_inevitable
  • 4,381
  • 2
  • 29
  • 48
  • 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 '12 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` – d_inevitable Mar 27 '12 at 20:46