List all folders and subfolder with bash












4














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 '%sn' "${myfolder//$addresstoremove/}" >> textfile.txt


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










share|improve this question









New contributor




Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    7 hours ago










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    3 mins ago
















4














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 '%sn' "${myfolder//$addresstoremove/}" >> textfile.txt


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










share|improve this question









New contributor




Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    7 hours ago










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    3 mins ago














4












4








4







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 '%sn' "${myfolder//$addresstoremove/}" >> textfile.txt


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










share|improve this question









New contributor




Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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 '%sn' "${myfolder//$addresstoremove/}" >> textfile.txt


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







command-line bash directory






share|improve this question









New contributor




Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 6 hours ago









wjandrea

8,26842259




8,26842259






New contributor




Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 7 hours ago









Cazz

232




232




New contributor




Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Cazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    7 hours ago










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    3 mins ago


















  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    7 hours ago










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    3 mins ago
















Use find. More on stackoverflow.com/a/1767402/5456794
– mja
7 hours ago




Use find. More on stackoverflow.com/a/1767402/5456794
– mja
7 hours ago












Possible duplicate askubuntu.com/q/307876/295286
– Sergiy Kolodyazhnyy
3 mins ago




Possible duplicate askubuntu.com/q/307876/295286
– Sergiy Kolodyazhnyy
3 mins ago










1 Answer
1






active

oldest

votes


















5














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


or



find /var/www/html/lib/ -type d >> file.txt





share|improve this answer























  • Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order
    – Cazz
    6 hours ago






  • 1




    @Cazz search on piping output to sort command.
    – WinEunuuchs2Unix
    5 hours ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Cazz is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1104695%2flist-all-folders-and-subfolder-with-bash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














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


or



find /var/www/html/lib/ -type d >> file.txt





share|improve this answer























  • Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order
    – Cazz
    6 hours ago






  • 1




    @Cazz search on piping output to sort command.
    – WinEunuuchs2Unix
    5 hours ago
















5














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


or



find /var/www/html/lib/ -type d >> file.txt





share|improve this answer























  • Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order
    – Cazz
    6 hours ago






  • 1




    @Cazz search on piping output to sort command.
    – WinEunuuchs2Unix
    5 hours ago














5












5








5






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


or



find /var/www/html/lib/ -type d >> file.txt





share|improve this answer














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


or



find /var/www/html/lib/ -type d >> file.txt






share|improve this answer














share|improve this answer



share|improve this answer








edited 7 hours ago

























answered 7 hours ago









Vijay

1,335617




1,335617












  • Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order
    – Cazz
    6 hours ago






  • 1




    @Cazz search on piping output to sort command.
    – WinEunuuchs2Unix
    5 hours ago


















  • Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order
    – Cazz
    6 hours ago






  • 1




    @Cazz search on piping output to sort command.
    – WinEunuuchs2Unix
    5 hours ago
















Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order
– Cazz
6 hours ago




Hmm nice easy code, like that alot but have the problem that it does not list in alphabetical order
– Cazz
6 hours ago




1




1




@Cazz search on piping output to sort command.
– WinEunuuchs2Unix
5 hours ago




@Cazz search on piping output to sort command.
– WinEunuuchs2Unix
5 hours ago










Cazz is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Cazz is a new contributor. Be nice, and check out our Code of Conduct.













Cazz is a new contributor. Be nice, and check out our Code of Conduct.












Cazz is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1104695%2flist-all-folders-and-subfolder-with-bash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo