1

I found a script online that works when entered into custom cronjobs on cpanel.

When i run this script, the code deletes the folder and everything within it.

 rm -rf public_html/storage_area/images/

I would like to delete empty sub-folders housed within the images folder and not the actual images folder itself.

I do not have much technical knowledge so any help would be much appreciated. I have tried a few php scripts that i found online but did not have much luck so if there is something that exists even better.

Thank you for any assistance.

Rafee
  • 3,975
  • 8
  • 58
  • 88
GIDUBZ86
  • 15
  • 8

1 Answers1

1

You just need to modify the command a bit.

If you need to remove only files inside that folder you can use,

rm -rf public_html/storage_area/images/*.* 

The *.* will only remove files within the folder public_html/storage_area/images/ having an extension.

If you need to remove files and sub folders, then you need to use

rm -rf public_html/storage_area/images/* 

If you only need to remove sub folders which are empty, you can use

find  -type d -empty -delete

Before running the above command, you may need to verify whether the command is only returning empty folders. For that you can use,

 find public_html/storage_area/images/ -type d -empty -print
CodeIt
  • 3,492
  • 3
  • 26
  • 37
  • @GIDUBZ86 You can try this command ` find public_html/storage_area/images/ -type d -empty -print`, it will print what it is able to find. – CodeIt Jul 09 '19 at 09:32
  • @codelt Thank you so much for breaking this down for me. I have realised where i was going wrong looking at your help above what i never realised was that i needed to change the print to delete keeping the rest of the code, however i have had a play and can confirm it works as required Thank You – GIDUBZ86 Jul 09 '19 at 09:37
  • @GIDUBZ86 Glad to hear that it helped. Please accept this as answer. – CodeIt Jul 09 '19 at 09:38
  • @Codelt Are you aware of anything similar that would resize images to reduce filesize without renaming or moving? or do i need to find something like imagemgk and call/run that with the cronjob? not quite sure how it works? – GIDUBZ86 Jul 12 '19 at 05:11
  • @GIDUBZ86 I would suggest you to check this [tool](https://www.imagemagick.org/script/mogrify.php). If you are facing any issues do let me know. – CodeIt Jul 12 '19 at 08:17
  • @Codelt I looked into this but have been unable to access imagemagick and tried SSH install but was unable to complete due to errors. is there anything else i can try? – GIDUBZ86 Jul 17 '19 at 10:43
  • @GIDUBZ86 The installation is pretty straight forward, just run `sudo apt-get install imagemagick`. If you are still facing issue do tell me. – CodeIt Jul 17 '19 at 12:24
  • @Codelt Sudo is not found, i looked on the documentation and tried another one rpm, that also was not found. i was able to use $HOME and followed the instructions with that one, however it got to a certain point and stopped doing anything. no output and no errors. When i open cmd i am in bash. i can use composer but dont know how to get this imagemagick with composer. there is one version on github. kind regards. – GIDUBZ86 Jul 18 '19 at 12:04
  • @Codelt Apparently i have gmagick, any idea how to use that one? – GIDUBZ86 Jul 18 '19 at 12:25
  • @GIDUBZ86 If you have gmagick you can follow [this](http://www.graphicsmagick.org/convert.html) guide. – CodeIt Jul 18 '19 at 12:37
  • think i've got it and it works for now so thanks very much for all the help and replies :) also very sorry to be such a pain. i should hopefully be able to work it out for now. – GIDUBZ86 Jul 18 '19 at 12:38
  • @GIDUBZ86 Glad to hear that you got it worked. You can always seek for help. I'm happy to help you. – CodeIt Jul 18 '19 at 12:40
  • @Codelt Im going to be a pain one last time....I have successfully resized images and found some useful information. Do you have any idea how i can cron this `find (public_html/storage_area/images/) -name '*.jpg' -execdir sh -c "mogrify -quality 5 *.jpg" {} \; ` to run in the same images folder as above idea in brackets.. and to only run on files added today? i've seen `find *.jpg -mtime -1 > list.txt mogrify -resize 50% @list.txt ` not sure how to best edit/combine and/or if that would even work. – GIDUBZ86 Jul 19 '19 at 09:51
  • The option `-mtime -1` for files added past 24 hrs. You can try with some empty files created using `touch` command to test it. – CodeIt Jul 19 '19 at 09:56
  • @Codelt i tried this command. it gave an error regarding -execdir any ideas if im doing this correctly or how to fix it? `find public_html/storage_area/images -type f -mtime -1 -name '*.jpg' -execdir sh -c "gm mogrify -quality 25 *.jpg" {} ;` i wanted it to reduce the quality of any jpg uploaded in the last 24hours. this was set to run daily at midnight – GIDUBZ86 Jul 22 '19 at 20:47
  • @GIDUBZ86 This should do it `find public_html/storage_area/images -type f -mtime -1 -name '*.jpg' -exec gm mogrify -quality 25 {} \;`. – CodeIt Jul 23 '19 at 04:04
  • @Codelt Thanks i will run it tonight and let you know how i get on :) – GIDUBZ86 Jul 23 '19 at 08:14
  • @Codelt Hello i ran the above command at 12 last night and it returned the following. `find: missing argument to -exec` – GIDUBZ86 Jul 24 '19 at 05:27
  • @GIDUBZ86 Try this now to see if it is working or not `find public_html/storage_area/images -type f -mtime -1 -name '*.jpg' -exec gm mogrify -quality 25 {} \;` or just `find public_html/storage_area/images -type f -mtime -1 -name '*.jpg' -exec echo {} \;` – CodeIt Jul 24 '19 at 05:31
  • @Codelt Hello, thanks for helping with this, they still dont seem to run as a cronjob, i do seem to be able to get them to run if using the command line only. – GIDUBZ86 Aug 02 '19 at 05:40
  • @GIDUBZ86 You may need to use `absolute path`. – CodeIt Aug 02 '19 at 06:13