Loop that lists folders with spaces

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












Approach



I have a directory with named folders, randomly, with blank spaces and periods.



I created a small loop (in a script) with the intention of renaming these folders.
On the basis that these are my directories (test and empty):



$ ls -la
./
../
35._JK_io/
'43A. io kl_ -'/
'Mi.Nombre.es Adios'/


Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.



Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:



for archives in `sh -c 'ls -q */' sh`

do

echo "$archives"
done


Issue



The problem is that when I run the script I get:



35._JK_io/:
43A.
io
kl_
-/:
Mi.Nombre.es
Adios/:


Question




How do I prevent this from happening to me and to appear with spaces and as only three files?




Observation



When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not



Thank you!!







share|improve this question



























    up vote
    1
    down vote

    favorite
    1












    Approach



    I have a directory with named folders, randomly, with blank spaces and periods.



    I created a small loop (in a script) with the intention of renaming these folders.
    On the basis that these are my directories (test and empty):



    $ ls -la
    ./
    ../
    35._JK_io/
    '43A. io kl_ -'/
    'Mi.Nombre.es Adios'/


    Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.



    Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:



    for archives in `sh -c 'ls -q */' sh`

    do

    echo "$archives"
    done


    Issue



    The problem is that when I run the script I get:



    35._JK_io/:
    43A.
    io
    kl_
    -/:
    Mi.Nombre.es
    Adios/:


    Question




    How do I prevent this from happening to me and to appear with spaces and as only three files?




    Observation



    When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not



    Thank you!!







    share|improve this question























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      Approach



      I have a directory with named folders, randomly, with blank spaces and periods.



      I created a small loop (in a script) with the intention of renaming these folders.
      On the basis that these are my directories (test and empty):



      $ ls -la
      ./
      ../
      35._JK_io/
      '43A. io kl_ -'/
      'Mi.Nombre.es Adios'/


      Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.



      Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:



      for archives in `sh -c 'ls -q */' sh`

      do

      echo "$archives"
      done


      Issue



      The problem is that when I run the script I get:



      35._JK_io/:
      43A.
      io
      kl_
      -/:
      Mi.Nombre.es
      Adios/:


      Question




      How do I prevent this from happening to me and to appear with spaces and as only three files?




      Observation



      When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not



      Thank you!!







      share|improve this question













      Approach



      I have a directory with named folders, randomly, with blank spaces and periods.



      I created a small loop (in a script) with the intention of renaming these folders.
      On the basis that these are my directories (test and empty):



      $ ls -la
      ./
      ../
      35._JK_io/
      '43A. io kl_ -'/
      'Mi.Nombre.es Adios'/


      Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.



      Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:



      for archives in `sh -c 'ls -q */' sh`

      do

      echo "$archives"
      done


      Issue



      The problem is that when I run the script I get:



      35._JK_io/:
      43A.
      io
      kl_
      -/:
      Mi.Nombre.es
      Adios/:


      Question




      How do I prevent this from happening to me and to appear with spaces and as only three files?




      Observation



      When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not



      Thank you!!









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 20 at 14:36









      Gilles

      502k1169881514




      502k1169881514









      asked Jul 20 at 11:03









      Nicolás Alarcón R.

      1165




      1165




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().



          Instead



          for dirname in ./*/; do
          printf 'Directory name is "%s"n' "$dirname"
          done


          The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.



          It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.



          Related:



          • Why *not* parse `ls`?

          • Why does my shell script choke on whitespace or other special characters?

          • When is double-quoting necessary?





          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457416%2floop-that-lists-folders-with-spaces%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().



            Instead



            for dirname in ./*/; do
            printf 'Directory name is "%s"n' "$dirname"
            done


            The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.



            It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.



            Related:



            • Why *not* parse `ls`?

            • Why does my shell script choke on whitespace or other special characters?

            • When is double-quoting necessary?





            share|improve this answer



























              up vote
              5
              down vote



              accepted










              When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().



              Instead



              for dirname in ./*/; do
              printf 'Directory name is "%s"n' "$dirname"
              done


              The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.



              It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.



              Related:



              • Why *not* parse `ls`?

              • Why does my shell script choke on whitespace or other special characters?

              • When is double-quoting necessary?





              share|improve this answer

























                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().



                Instead



                for dirname in ./*/; do
                printf 'Directory name is "%s"n' "$dirname"
                done


                The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.



                It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.



                Related:



                • Why *not* parse `ls`?

                • Why does my shell script choke on whitespace or other special characters?

                • When is double-quoting necessary?





                share|improve this answer















                When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().



                Instead



                for dirname in ./*/; do
                printf 'Directory name is "%s"n' "$dirname"
                done


                The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.



                It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.



                Related:



                • Why *not* parse `ls`?

                • Why does my shell script choke on whitespace or other special characters?

                • When is double-quoting necessary?






                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jul 20 at 11:19


























                answered Jul 20 at 11:09









                Kusalananda

                101k13199311




                101k13199311






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457416%2floop-that-lists-folders-with-spaces%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)