Recursively create empty file in empty sub-directories

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











up vote
2
down vote

favorite












I have created the directory structure for my Maven project.



$ tree -a -I .git
.
├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
│   ├── java
│   └── resources
└── test
├── java
└── resources

7 directories, 2 files


Now I'd like to persist the structure to .git, which requires creating dummy files in sub-directories. How can I (recursively) add empty .gitkeep files to all empty sub-directories?




Following questions already discuss (recursive) creation of empty files in sub-directories, but I'd like the files to be created only in leaf directories and not in any intermediate directories



  • Creating empty files in all subfolders

  • Recursively add a file to all sub-directories






share|improve this question
























    up vote
    2
    down vote

    favorite












    I have created the directory structure for my Maven project.



    $ tree -a -I .git
    .
    ├── .gitignore
    ├── README.md
    ├── pom.xml
    └── src
    ├── main
    │   ├── java
    │   └── resources
    └── test
    ├── java
    └── resources

    7 directories, 2 files


    Now I'd like to persist the structure to .git, which requires creating dummy files in sub-directories. How can I (recursively) add empty .gitkeep files to all empty sub-directories?




    Following questions already discuss (recursive) creation of empty files in sub-directories, but I'd like the files to be created only in leaf directories and not in any intermediate directories



    • Creating empty files in all subfolders

    • Recursively add a file to all sub-directories






    share|improve this question






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have created the directory structure for my Maven project.



      $ tree -a -I .git
      .
      ├── .gitignore
      ├── README.md
      ├── pom.xml
      └── src
      ├── main
      │   ├── java
      │   └── resources
      └── test
      ├── java
      └── resources

      7 directories, 2 files


      Now I'd like to persist the structure to .git, which requires creating dummy files in sub-directories. How can I (recursively) add empty .gitkeep files to all empty sub-directories?




      Following questions already discuss (recursive) creation of empty files in sub-directories, but I'd like the files to be created only in leaf directories and not in any intermediate directories



      • Creating empty files in all subfolders

      • Recursively add a file to all sub-directories






      share|improve this question












      I have created the directory structure for my Maven project.



      $ tree -a -I .git
      .
      ├── .gitignore
      ├── README.md
      ├── pom.xml
      └── src
      ├── main
      │   ├── java
      │   └── resources
      └── test
      ├── java
      └── resources

      7 directories, 2 files


      Now I'd like to persist the structure to .git, which requires creating dummy files in sub-directories. How can I (recursively) add empty .gitkeep files to all empty sub-directories?




      Following questions already discuss (recursive) creation of empty files in sub-directories, but I'd like the files to be created only in leaf directories and not in any intermediate directories



      • Creating empty files in all subfolders

      • Recursively add a file to all sub-directories








      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 16 at 18:42









      y2k-shubham

      1615




      1615




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote













          From Ryan Armstrong's blog, here's how you do it



          find . -type d -empty -not -path "./.git/*" -exec touch /.gitkeep ;



          • find . -type d (recursively) looks for directories under current path


          • -empty filters out directories that already contain something


          • -not -path "./.git/*" ensures no files are created inside .git directory


          • -exec touch /.gitkeep ; creates empty .gitkeep file in each directory matching above criteria


          The resulting structure looks like



          $ tree -a -I .git
          .
          ├── .gitignore
          ├── README.md
          ├── pom.xml
          └── src
          ├── main
          │   ├── java
          │   │   └── .gitkeep
          │   └── resources
          │   └── .gitkeep
          └── test
          ├── java
          │   └── .gitkeep
          └── resources
          └── .gitkeep

          7 directories, 7 files





          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%2f463044%2frecursively-create-empty-file-in-empty-sub-directories%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













            From Ryan Armstrong's blog, here's how you do it



            find . -type d -empty -not -path "./.git/*" -exec touch /.gitkeep ;



            • find . -type d (recursively) looks for directories under current path


            • -empty filters out directories that already contain something


            • -not -path "./.git/*" ensures no files are created inside .git directory


            • -exec touch /.gitkeep ; creates empty .gitkeep file in each directory matching above criteria


            The resulting structure looks like



            $ tree -a -I .git
            .
            ├── .gitignore
            ├── README.md
            ├── pom.xml
            └── src
            ├── main
            │   ├── java
            │   │   └── .gitkeep
            │   └── resources
            │   └── .gitkeep
            └── test
            ├── java
            │   └── .gitkeep
            └── resources
            └── .gitkeep

            7 directories, 7 files





            share|improve this answer


























              up vote
              5
              down vote













              From Ryan Armstrong's blog, here's how you do it



              find . -type d -empty -not -path "./.git/*" -exec touch /.gitkeep ;



              • find . -type d (recursively) looks for directories under current path


              • -empty filters out directories that already contain something


              • -not -path "./.git/*" ensures no files are created inside .git directory


              • -exec touch /.gitkeep ; creates empty .gitkeep file in each directory matching above criteria


              The resulting structure looks like



              $ tree -a -I .git
              .
              ├── .gitignore
              ├── README.md
              ├── pom.xml
              └── src
              ├── main
              │   ├── java
              │   │   └── .gitkeep
              │   └── resources
              │   └── .gitkeep
              └── test
              ├── java
              │   └── .gitkeep
              └── resources
              └── .gitkeep

              7 directories, 7 files





              share|improve this answer
























                up vote
                5
                down vote










                up vote
                5
                down vote









                From Ryan Armstrong's blog, here's how you do it



                find . -type d -empty -not -path "./.git/*" -exec touch /.gitkeep ;



                • find . -type d (recursively) looks for directories under current path


                • -empty filters out directories that already contain something


                • -not -path "./.git/*" ensures no files are created inside .git directory


                • -exec touch /.gitkeep ; creates empty .gitkeep file in each directory matching above criteria


                The resulting structure looks like



                $ tree -a -I .git
                .
                ├── .gitignore
                ├── README.md
                ├── pom.xml
                └── src
                ├── main
                │   ├── java
                │   │   └── .gitkeep
                │   └── resources
                │   └── .gitkeep
                └── test
                ├── java
                │   └── .gitkeep
                └── resources
                └── .gitkeep

                7 directories, 7 files





                share|improve this answer














                From Ryan Armstrong's blog, here's how you do it



                find . -type d -empty -not -path "./.git/*" -exec touch /.gitkeep ;



                • find . -type d (recursively) looks for directories under current path


                • -empty filters out directories that already contain something


                • -not -path "./.git/*" ensures no files are created inside .git directory


                • -exec touch /.gitkeep ; creates empty .gitkeep file in each directory matching above criteria


                The resulting structure looks like



                $ tree -a -I .git
                .
                ├── .gitignore
                ├── README.md
                ├── pom.xml
                └── src
                ├── main
                │   ├── java
                │   │   └── .gitkeep
                │   └── resources
                │   └── .gitkeep
                └── test
                ├── java
                │   └── .gitkeep
                └── resources
                └── .gitkeep

                7 directories, 7 files






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 16 at 18:43









                ilkkachu

                50.3k677138




                50.3k677138










                answered Aug 16 at 18:42









                y2k-shubham

                1615




                1615



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463044%2frecursively-create-empty-file-in-empty-sub-directories%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    One-line joke