5

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}"
    fi
done

it gave me

/var/www/html/lib/folder1
/var/www/html/lib/folder2
/var/www/html/lib/folder3
...

But the folders all have subfolders like

/var/html/lib/folder1/1990
...

My idea was to make a loop and then use

myfolder= "${D}"
addresstoremove="var/www/html/"

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

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

3

1 Answer 1

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

or

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .