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
























  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    Dec 26 '18 at 17:34










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    Dec 27 '18 at 0:54
















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
























  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    Dec 26 '18 at 17:34










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    Dec 27 '18 at 0:54














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















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















share|improve this question













share|improve this question




share|improve this question








edited Dec 26 '18 at 18:10









wjandrea

8,47842259




8,47842259










asked Dec 26 '18 at 16:57









CazzCazz

233




233












  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    Dec 26 '18 at 17:34










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    Dec 27 '18 at 0:54


















  • Use find. More on stackoverflow.com/a/1767402/5456794
    – mja
    Dec 26 '18 at 17:34










  • Possible duplicate askubuntu.com/q/307876/295286
    – Sergiy Kolodyazhnyy
    Dec 27 '18 at 0:54
















Use find. More on stackoverflow.com/a/1767402/5456794
– mja
Dec 26 '18 at 17:34




Use find. More on stackoverflow.com/a/1767402/5456794
– mja
Dec 26 '18 at 17:34












Possible duplicate askubuntu.com/q/307876/295286
– Sergiy Kolodyazhnyy
Dec 27 '18 at 0:54




Possible duplicate askubuntu.com/q/307876/295286
– Sergiy Kolodyazhnyy
Dec 27 '18 at 0:54










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








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
});


}
});














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
















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














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 Dec 26 '18 at 17:38

























answered Dec 26 '18 at 17:35









VijayVijay

1,463618




1,463618












  • 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


















  • 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
















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




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




2




@Cazz search on piping output to sort command.
– WinEunuuchs2Unix
Dec 26 '18 at 19:17




@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




find /var/www/html/lib/ -type d | sort >> file.txt
– user1330614
Dec 27 '18 at 2:56


















draft saved

draft discarded




















































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

Index of /

Tribalistas

Listed building