Access nth subdirectories depth and count filesWhy *not* parse `ls` (and what to do instead)?How do I read...

Vertical Yagi spun at 1000 RPM: gain properties?

Are Changelings immune to the Polymorph spell?

Is this sentence from a widely distributed current affairs publication correct?

Fantasy series about a human girl with gold tattoos who makes too much blood

Was Switzerland pressured either by Allies or Axis to take part in World War 2 at any time?

How does an Evocation Wizard's Overchannel ability interact with Chaos Bolt?

How to get to Antarctica without using a travel company

Which person is telling the truth?

Is my translation correct?

Modeling the Round (Nearest Integer) function

Repair drywall and protect wires on back of electrical panel

Can US Congress members be (successfully) sued for defamation?

Convexity of a QP

Can Chill Touch prevent Regeneration?

Reverse Voltage?

Does Turkey make the "structural steel frame" for the F-35 fighter?

Why apt asking to uninstall GIMP when installing ardour?

In this scene from the novel, 'The Martian', by Andy Weir, how does Mark Watney store hydrogen made from water in the tank?

What are the advantages to banks being located in the City of London (the Square Mile)?

What can I wear to avoid getting frisked and crotch searched by TSA at the airport?

The use of SlotSequence in If[#1 > #2, ##] &

What plausible reasons why people forget they didn't originally live on this new planet?

How to load GeoJSON data in OpenLayers?

Is the phrase “You are requested” polite or rude?



Access nth subdirectories depth and count files


Why *not* parse `ls` (and what to do instead)?How do I read alternate gnome configuration filesMake all files under a directory read-only without changing permissions?How to execute this particular shell command from Python?Script to count files matching a pattern in subdirectoriesLFS can't cd to lfs/source - permission deniedls: Do not show directories that match same pattern in wildcard searches, only fileslooping a command to varying number of files in subdirectoriesHow to delete all files in a current directory starting with a dot?Looping through all files, skipping some dynamically






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}








0

















Using the cd commands and ls give the tree of your UNIX machine to a depth of 2 subdirectories starting from the root "/". If the number of files or directories is greater than 20, specify only the number of files or subdirectories.



I tried this so far:




cd /../.. | ls -l | wc -l




But I don't really know if that gives me what I need. I know WC. will count files number but I did not get that. cd/../../ since it looks like I heading two levels down. Well it looks confusing.










share|improve this question









New contributor



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
























  • Could you please post what you have tried? And please tell us if this is homework.

    – fd0
    1 hour ago











  • I didn't quite understand that thing about more than 20 directories or files. Is that some total number of the 2nd directory level, or in any single directory on that level (recursively?)? Do you mean "output" when you write "specify"? Besides, wc -l never counts files. It only counts lines of text. If filenames contains newlines, these would be counted multiple times by wc -l.

    – Kusalananda
    56 mins ago













  • wc associated with ls -l through the piping will count number of files . the command should display number of files of. all sub-directories of level 2 with their number of files .

    – SAm
    52 mins ago













  • wc -l does not count files. It counts lines. Try creating an empty directory, and then run touch $'onenfile' inside it. ls -l | wc -l will give you 3 (2 for the file, and one for the header that it always outputs when using its long format output).

    – Kusalananda
    48 mins ago




















0

















Using the cd commands and ls give the tree of your UNIX machine to a depth of 2 subdirectories starting from the root "/". If the number of files or directories is greater than 20, specify only the number of files or subdirectories.



I tried this so far:




cd /../.. | ls -l | wc -l




But I don't really know if that gives me what I need. I know WC. will count files number but I did not get that. cd/../../ since it looks like I heading two levels down. Well it looks confusing.










share|improve this question









New contributor



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
























  • Could you please post what you have tried? And please tell us if this is homework.

    – fd0
    1 hour ago











  • I didn't quite understand that thing about more than 20 directories or files. Is that some total number of the 2nd directory level, or in any single directory on that level (recursively?)? Do you mean "output" when you write "specify"? Besides, wc -l never counts files. It only counts lines of text. If filenames contains newlines, these would be counted multiple times by wc -l.

    – Kusalananda
    56 mins ago













  • wc associated with ls -l through the piping will count number of files . the command should display number of files of. all sub-directories of level 2 with their number of files .

    – SAm
    52 mins ago













  • wc -l does not count files. It counts lines. Try creating an empty directory, and then run touch $'onenfile' inside it. ls -l | wc -l will give you 3 (2 for the file, and one for the header that it always outputs when using its long format output).

    – Kusalananda
    48 mins ago
















0












0








0








Using the cd commands and ls give the tree of your UNIX machine to a depth of 2 subdirectories starting from the root "/". If the number of files or directories is greater than 20, specify only the number of files or subdirectories.



I tried this so far:




cd /../.. | ls -l | wc -l




But I don't really know if that gives me what I need. I know WC. will count files number but I did not get that. cd/../../ since it looks like I heading two levels down. Well it looks confusing.










share|improve this question









New contributor



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












Using the cd commands and ls give the tree of your UNIX machine to a depth of 2 subdirectories starting from the root "/". If the number of files or directories is greater than 20, specify only the number of files or subdirectories.



I tried this so far:




cd /../.. | ls -l | wc -l




But I don't really know if that gives me what I need. I know WC. will count files number but I did not get that. cd/../../ since it looks like I heading two levels down. Well it looks confusing.







linux shell






share|improve this question









New contributor



SAm 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



SAm 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 1 hour ago







SAm













New contributor



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








asked 1 hour ago









SAmSAm

42 bronze badges




42 bronze badges




New contributor



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




New contributor




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


















  • Could you please post what you have tried? And please tell us if this is homework.

    – fd0
    1 hour ago











  • I didn't quite understand that thing about more than 20 directories or files. Is that some total number of the 2nd directory level, or in any single directory on that level (recursively?)? Do you mean "output" when you write "specify"? Besides, wc -l never counts files. It only counts lines of text. If filenames contains newlines, these would be counted multiple times by wc -l.

    – Kusalananda
    56 mins ago













  • wc associated with ls -l through the piping will count number of files . the command should display number of files of. all sub-directories of level 2 with their number of files .

    – SAm
    52 mins ago













  • wc -l does not count files. It counts lines. Try creating an empty directory, and then run touch $'onenfile' inside it. ls -l | wc -l will give you 3 (2 for the file, and one for the header that it always outputs when using its long format output).

    – Kusalananda
    48 mins ago





















  • Could you please post what you have tried? And please tell us if this is homework.

    – fd0
    1 hour ago











  • I didn't quite understand that thing about more than 20 directories or files. Is that some total number of the 2nd directory level, or in any single directory on that level (recursively?)? Do you mean "output" when you write "specify"? Besides, wc -l never counts files. It only counts lines of text. If filenames contains newlines, these would be counted multiple times by wc -l.

    – Kusalananda
    56 mins ago













  • wc associated with ls -l through the piping will count number of files . the command should display number of files of. all sub-directories of level 2 with their number of files .

    – SAm
    52 mins ago













  • wc -l does not count files. It counts lines. Try creating an empty directory, and then run touch $'onenfile' inside it. ls -l | wc -l will give you 3 (2 for the file, and one for the header that it always outputs when using its long format output).

    – Kusalananda
    48 mins ago



















Could you please post what you have tried? And please tell us if this is homework.

– fd0
1 hour ago





Could you please post what you have tried? And please tell us if this is homework.

– fd0
1 hour ago













I didn't quite understand that thing about more than 20 directories or files. Is that some total number of the 2nd directory level, or in any single directory on that level (recursively?)? Do you mean "output" when you write "specify"? Besides, wc -l never counts files. It only counts lines of text. If filenames contains newlines, these would be counted multiple times by wc -l.

– Kusalananda
56 mins ago







I didn't quite understand that thing about more than 20 directories or files. Is that some total number of the 2nd directory level, or in any single directory on that level (recursively?)? Do you mean "output" when you write "specify"? Besides, wc -l never counts files. It only counts lines of text. If filenames contains newlines, these would be counted multiple times by wc -l.

– Kusalananda
56 mins ago















wc associated with ls -l through the piping will count number of files . the command should display number of files of. all sub-directories of level 2 with their number of files .

– SAm
52 mins ago







wc associated with ls -l through the piping will count number of files . the command should display number of files of. all sub-directories of level 2 with their number of files .

– SAm
52 mins ago















wc -l does not count files. It counts lines. Try creating an empty directory, and then run touch $'onenfile' inside it. ls -l | wc -l will give you 3 (2 for the file, and one for the header that it always outputs when using its long format output).

– Kusalananda
48 mins ago







wc -l does not count files. It counts lines. Try creating an empty directory, and then run touch $'onenfile' inside it. ls -l | wc -l will give you 3 (2 for the file, and one for the header that it always outputs when using its long format output).

– Kusalananda
48 mins ago












1 Answer
1






active

oldest

votes


















0


















Your command is nonsensical. First of all, cd /../.. is the same as cd /, i.e. it will just change the current working directory to the top-most directory in the directory hierarchy. Secondly, cd does not produce any output, so piping it to anything is not going to do much good.



Thirdly, ls -l will produce a listing of entries (names) in the current directory. You are using wc -l to count the number of lines in the output of ls -l. This number likely be at least one more than the real number of directory entries in any directory since ls -l outputs a sort of header (BSD ls doesn't do this for empty directories, but GNU ls always does). ls -l will not list hidden filenames, and it will (when piped to wc -l) produce multiple lines for files that have newlines embedded in their filenames.



Don't use ls for anything other than for looking at directory listings (with your eyes), and only ever use wc -l to count lines. There are smarter and quicker ways to count files.



Related:




  • Why *not* parse `ls` (and what to do instead)?




To list the number of directory entries (names) inside all 2nd level subdirectories using bash, use something like



shopt -s nullglob dotglob

for dirpath in /*/*/; do
set -- "$dirpath"/*
printf '%dt%sn' "$#" "$dirpath"
done


This loops over all 2nd level directories. For each directory, it expands the * globbing pattern in the directory and sets the positional parameters to the resulting names. After doing so, the special variable $# will contain the number of positional parameters (names in the directory). We then print the directory pathname together with that count.



The nullglob and dotglob shell options are first set so that we correctly count hidden names and so that we get the correct count (0) for empty directories.



Would you want to have a recursive count of each subdirectory at level 2:



shopt -s nullglob dotglob globstar

for dirpath in /*/*/; do
set -- "$dirpath"/**/*
printf '%dt%sn' "$#" "$dirpath"
done


The globstar shell option in bash enables the use of **, which is a pattern that matches "recursively" down into subdirectories.



Would you want to only list the ones with 20 or more entries in them (here using the second variation from above, which counts names recursively in each subdirectory):



shopt -s nullglob dotglob globstar

for dirpath in /*/*/; do
set -- "$dirpath"/**/*
if [[ $# -ge 20 ]]; then
printf '%dt%sn' "$#" "$dirpath"
fi
done





share|improve this answer






























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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/4.0/"u003ecc by-sa 4.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
    });


    }
    });







    SAm 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%2funix.stackexchange.com%2fquestions%2f546628%2faccess-nth-subdirectories-depth-and-count-files%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









    0


















    Your command is nonsensical. First of all, cd /../.. is the same as cd /, i.e. it will just change the current working directory to the top-most directory in the directory hierarchy. Secondly, cd does not produce any output, so piping it to anything is not going to do much good.



    Thirdly, ls -l will produce a listing of entries (names) in the current directory. You are using wc -l to count the number of lines in the output of ls -l. This number likely be at least one more than the real number of directory entries in any directory since ls -l outputs a sort of header (BSD ls doesn't do this for empty directories, but GNU ls always does). ls -l will not list hidden filenames, and it will (when piped to wc -l) produce multiple lines for files that have newlines embedded in their filenames.



    Don't use ls for anything other than for looking at directory listings (with your eyes), and only ever use wc -l to count lines. There are smarter and quicker ways to count files.



    Related:




    • Why *not* parse `ls` (and what to do instead)?




    To list the number of directory entries (names) inside all 2nd level subdirectories using bash, use something like



    shopt -s nullglob dotglob

    for dirpath in /*/*/; do
    set -- "$dirpath"/*
    printf '%dt%sn' "$#" "$dirpath"
    done


    This loops over all 2nd level directories. For each directory, it expands the * globbing pattern in the directory and sets the positional parameters to the resulting names. After doing so, the special variable $# will contain the number of positional parameters (names in the directory). We then print the directory pathname together with that count.



    The nullglob and dotglob shell options are first set so that we correctly count hidden names and so that we get the correct count (0) for empty directories.



    Would you want to have a recursive count of each subdirectory at level 2:



    shopt -s nullglob dotglob globstar

    for dirpath in /*/*/; do
    set -- "$dirpath"/**/*
    printf '%dt%sn' "$#" "$dirpath"
    done


    The globstar shell option in bash enables the use of **, which is a pattern that matches "recursively" down into subdirectories.



    Would you want to only list the ones with 20 or more entries in them (here using the second variation from above, which counts names recursively in each subdirectory):



    shopt -s nullglob dotglob globstar

    for dirpath in /*/*/; do
    set -- "$dirpath"/**/*
    if [[ $# -ge 20 ]]; then
    printf '%dt%sn' "$#" "$dirpath"
    fi
    done





    share|improve this answer

































      0


















      Your command is nonsensical. First of all, cd /../.. is the same as cd /, i.e. it will just change the current working directory to the top-most directory in the directory hierarchy. Secondly, cd does not produce any output, so piping it to anything is not going to do much good.



      Thirdly, ls -l will produce a listing of entries (names) in the current directory. You are using wc -l to count the number of lines in the output of ls -l. This number likely be at least one more than the real number of directory entries in any directory since ls -l outputs a sort of header (BSD ls doesn't do this for empty directories, but GNU ls always does). ls -l will not list hidden filenames, and it will (when piped to wc -l) produce multiple lines for files that have newlines embedded in their filenames.



      Don't use ls for anything other than for looking at directory listings (with your eyes), and only ever use wc -l to count lines. There are smarter and quicker ways to count files.



      Related:




      • Why *not* parse `ls` (and what to do instead)?




      To list the number of directory entries (names) inside all 2nd level subdirectories using bash, use something like



      shopt -s nullglob dotglob

      for dirpath in /*/*/; do
      set -- "$dirpath"/*
      printf '%dt%sn' "$#" "$dirpath"
      done


      This loops over all 2nd level directories. For each directory, it expands the * globbing pattern in the directory and sets the positional parameters to the resulting names. After doing so, the special variable $# will contain the number of positional parameters (names in the directory). We then print the directory pathname together with that count.



      The nullglob and dotglob shell options are first set so that we correctly count hidden names and so that we get the correct count (0) for empty directories.



      Would you want to have a recursive count of each subdirectory at level 2:



      shopt -s nullglob dotglob globstar

      for dirpath in /*/*/; do
      set -- "$dirpath"/**/*
      printf '%dt%sn' "$#" "$dirpath"
      done


      The globstar shell option in bash enables the use of **, which is a pattern that matches "recursively" down into subdirectories.



      Would you want to only list the ones with 20 or more entries in them (here using the second variation from above, which counts names recursively in each subdirectory):



      shopt -s nullglob dotglob globstar

      for dirpath in /*/*/; do
      set -- "$dirpath"/**/*
      if [[ $# -ge 20 ]]; then
      printf '%dt%sn' "$#" "$dirpath"
      fi
      done





      share|improve this answer































        0














        0










        0









        Your command is nonsensical. First of all, cd /../.. is the same as cd /, i.e. it will just change the current working directory to the top-most directory in the directory hierarchy. Secondly, cd does not produce any output, so piping it to anything is not going to do much good.



        Thirdly, ls -l will produce a listing of entries (names) in the current directory. You are using wc -l to count the number of lines in the output of ls -l. This number likely be at least one more than the real number of directory entries in any directory since ls -l outputs a sort of header (BSD ls doesn't do this for empty directories, but GNU ls always does). ls -l will not list hidden filenames, and it will (when piped to wc -l) produce multiple lines for files that have newlines embedded in their filenames.



        Don't use ls for anything other than for looking at directory listings (with your eyes), and only ever use wc -l to count lines. There are smarter and quicker ways to count files.



        Related:




        • Why *not* parse `ls` (and what to do instead)?




        To list the number of directory entries (names) inside all 2nd level subdirectories using bash, use something like



        shopt -s nullglob dotglob

        for dirpath in /*/*/; do
        set -- "$dirpath"/*
        printf '%dt%sn' "$#" "$dirpath"
        done


        This loops over all 2nd level directories. For each directory, it expands the * globbing pattern in the directory and sets the positional parameters to the resulting names. After doing so, the special variable $# will contain the number of positional parameters (names in the directory). We then print the directory pathname together with that count.



        The nullglob and dotglob shell options are first set so that we correctly count hidden names and so that we get the correct count (0) for empty directories.



        Would you want to have a recursive count of each subdirectory at level 2:



        shopt -s nullglob dotglob globstar

        for dirpath in /*/*/; do
        set -- "$dirpath"/**/*
        printf '%dt%sn' "$#" "$dirpath"
        done


        The globstar shell option in bash enables the use of **, which is a pattern that matches "recursively" down into subdirectories.



        Would you want to only list the ones with 20 or more entries in them (here using the second variation from above, which counts names recursively in each subdirectory):



        shopt -s nullglob dotglob globstar

        for dirpath in /*/*/; do
        set -- "$dirpath"/**/*
        if [[ $# -ge 20 ]]; then
        printf '%dt%sn' "$#" "$dirpath"
        fi
        done





        share|improve this answer
















        Your command is nonsensical. First of all, cd /../.. is the same as cd /, i.e. it will just change the current working directory to the top-most directory in the directory hierarchy. Secondly, cd does not produce any output, so piping it to anything is not going to do much good.



        Thirdly, ls -l will produce a listing of entries (names) in the current directory. You are using wc -l to count the number of lines in the output of ls -l. This number likely be at least one more than the real number of directory entries in any directory since ls -l outputs a sort of header (BSD ls doesn't do this for empty directories, but GNU ls always does). ls -l will not list hidden filenames, and it will (when piped to wc -l) produce multiple lines for files that have newlines embedded in their filenames.



        Don't use ls for anything other than for looking at directory listings (with your eyes), and only ever use wc -l to count lines. There are smarter and quicker ways to count files.



        Related:




        • Why *not* parse `ls` (and what to do instead)?




        To list the number of directory entries (names) inside all 2nd level subdirectories using bash, use something like



        shopt -s nullglob dotglob

        for dirpath in /*/*/; do
        set -- "$dirpath"/*
        printf '%dt%sn' "$#" "$dirpath"
        done


        This loops over all 2nd level directories. For each directory, it expands the * globbing pattern in the directory and sets the positional parameters to the resulting names. After doing so, the special variable $# will contain the number of positional parameters (names in the directory). We then print the directory pathname together with that count.



        The nullglob and dotglob shell options are first set so that we correctly count hidden names and so that we get the correct count (0) for empty directories.



        Would you want to have a recursive count of each subdirectory at level 2:



        shopt -s nullglob dotglob globstar

        for dirpath in /*/*/; do
        set -- "$dirpath"/**/*
        printf '%dt%sn' "$#" "$dirpath"
        done


        The globstar shell option in bash enables the use of **, which is a pattern that matches "recursively" down into subdirectories.



        Would you want to only list the ones with 20 or more entries in them (here using the second variation from above, which counts names recursively in each subdirectory):



        shopt -s nullglob dotglob globstar

        for dirpath in /*/*/; do
        set -- "$dirpath"/**/*
        if [[ $# -ge 20 ]]; then
        printf '%dt%sn' "$#" "$dirpath"
        fi
        done






        share|improve this answer















        share|improve this answer




        share|improve this answer








        edited 8 mins ago

























        answered 38 mins ago









        KusalanandaKusalananda

        168k20 gold badges326 silver badges522 bronze badges




        168k20 gold badges326 silver badges522 bronze badges


























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










            draft saved

            draft discarded

















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













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












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
















            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f546628%2faccess-nth-subdirectories-depth-and-count-files%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

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...