I tried to use PHP to read my folder and the subfolder to create a basic menu for me to use, but I noticed when I did that it took some time because it has to run it every time I go to the page.

Then I was thinking, why not use bash and make it create a text file that PHP can read and make it run every night with crontab? So I searched and tried but it was not so easy.

With this code I can get ONE subfolder

for D in /var/www/html/lib/*; do
    if [ -d "${D}" ]; then
        echo "${D}"

it gave me


But the folders all have subfolders like


My idea was to make a loop and then use

myfolder= "${D}"

printf '%s\n' "${myfolder//$addresstoremove/}" >> textfile.txt

so it remove the address I do not need in my PHP code

find /var/www/html/lib/ -type d


find /var/www/html/lib/ -type d >> file.txt
| improve this answer | |
  • Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order – Cazz Dec 26 '18 at 18:57
  • 2
    @Cazz search on piping output to sort command. – WinEunuuchs2Unix Dec 26 '18 at 19:17
  • find /var/www/html/lib/ -type d | sort >> file.txt – user1330614 Dec 27 '18 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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