batch rename folders with value from json value in package.json

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











up vote
1
down vote

favorite
1












This is a bit of a crazy one, but I am wondering if it is even possible.



I have maybe 300 folders in my /var/www folder that were all renamed to the same name [Name](Num) so kindred (19) for example.



Almost every single one of these folders has a file called package.json which all have a name key value pair which then has the name of the project in side that.




"name": "a useful project name",
......
"main": "src/index.js",



I would like the folder that contains the package.json to be renamed to whatever the value of "name" is inside the package.json file.










share|improve this question

























    up vote
    1
    down vote

    favorite
    1












    This is a bit of a crazy one, but I am wondering if it is even possible.



    I have maybe 300 folders in my /var/www folder that were all renamed to the same name [Name](Num) so kindred (19) for example.



    Almost every single one of these folders has a file called package.json which all have a name key value pair which then has the name of the project in side that.




    "name": "a useful project name",
    ......
    "main": "src/index.js",



    I would like the folder that contains the package.json to be renamed to whatever the value of "name" is inside the package.json file.










    share|improve this question























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      This is a bit of a crazy one, but I am wondering if it is even possible.



      I have maybe 300 folders in my /var/www folder that were all renamed to the same name [Name](Num) so kindred (19) for example.



      Almost every single one of these folders has a file called package.json which all have a name key value pair which then has the name of the project in side that.




      "name": "a useful project name",
      ......
      "main": "src/index.js",



      I would like the folder that contains the package.json to be renamed to whatever the value of "name" is inside the package.json file.










      share|improve this question













      This is a bit of a crazy one, but I am wondering if it is even possible.



      I have maybe 300 folders in my /var/www folder that were all renamed to the same name [Name](Num) so kindred (19) for example.



      Almost every single one of these folders has a file called package.json which all have a name key value pair which then has the name of the project in side that.




      "name": "a useful project name",
      ......
      "main": "src/index.js",



      I would like the folder that contains the package.json to be renamed to whatever the value of "name" is inside the package.json file.







      grep terminal regular-expression rename batch-jobs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 26 at 11:51









      Jamie Hutber

      1001213




      1001213




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          One of the better tools to use when you need to parse json files is jq. That makes it easy to extract the name field from the package.json file in a directory. Performing the rename is a simple bit of shell scripting:



          $ cd /var/www
          $ for d in */; do # *1
          > if [ -f "$dpackage.json" ]; then # *2
          > new_name=$(jq -e -M -r .name "$dpackage.json") # *3
          > if [ $? -eq 0 ] && ! [ -e "$new_name" ]; then # *4
          > mv "$d" "$new_name" # *5
          > fi
          > fi
          > done


          Some notes:



          *1: */ expands to all the directories in the current directory. Each directory name will include a / on the end, so we do not put one in later at *2 and *3.



          *2: Only process directories that have a package.json file.



          *3: Invoke jq to extract the name field from package.json. We invoke jq with -r to output raw strings (i.e. leave off the double quotes), with -M to not have colored output, and -e to have jq exit with an error if there is no name field.



          *4: Check that jq ran successfully (there was a name field) and that the new name for the directory does not already exist. You may want to split these up and add an else if you want to output an error message for the two cases where you're skipping the rename.



          *5: Rename the directory.



          For a test run, I'd put echo in front of the mv command at *5 and check the output to see that the renaming looks right. I haven't tested this myself as I don't have a bunch of directories with package.json files.






          share|improve this answer






















          • I changed ` $? -ne 0` to ` $? -ne 1` and it worked a treat :D
            – Jamie Hutber
            Aug 26 at 19:32






          • 1




            @JamieHutber Yeah, sorry. Stupid typo. I've changed it from -ne to -eq which is better than checking -ne 1 as jq could return a different error code.
            – camh
            Aug 27 at 2:27











          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%2f464906%2fbatch-rename-folders-with-value-from-json-value-in-package-json%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
          1
          down vote



          accepted










          One of the better tools to use when you need to parse json files is jq. That makes it easy to extract the name field from the package.json file in a directory. Performing the rename is a simple bit of shell scripting:



          $ cd /var/www
          $ for d in */; do # *1
          > if [ -f "$dpackage.json" ]; then # *2
          > new_name=$(jq -e -M -r .name "$dpackage.json") # *3
          > if [ $? -eq 0 ] && ! [ -e "$new_name" ]; then # *4
          > mv "$d" "$new_name" # *5
          > fi
          > fi
          > done


          Some notes:



          *1: */ expands to all the directories in the current directory. Each directory name will include a / on the end, so we do not put one in later at *2 and *3.



          *2: Only process directories that have a package.json file.



          *3: Invoke jq to extract the name field from package.json. We invoke jq with -r to output raw strings (i.e. leave off the double quotes), with -M to not have colored output, and -e to have jq exit with an error if there is no name field.



          *4: Check that jq ran successfully (there was a name field) and that the new name for the directory does not already exist. You may want to split these up and add an else if you want to output an error message for the two cases where you're skipping the rename.



          *5: Rename the directory.



          For a test run, I'd put echo in front of the mv command at *5 and check the output to see that the renaming looks right. I haven't tested this myself as I don't have a bunch of directories with package.json files.






          share|improve this answer






















          • I changed ` $? -ne 0` to ` $? -ne 1` and it worked a treat :D
            – Jamie Hutber
            Aug 26 at 19:32






          • 1




            @JamieHutber Yeah, sorry. Stupid typo. I've changed it from -ne to -eq which is better than checking -ne 1 as jq could return a different error code.
            – camh
            Aug 27 at 2:27















          up vote
          1
          down vote



          accepted










          One of the better tools to use when you need to parse json files is jq. That makes it easy to extract the name field from the package.json file in a directory. Performing the rename is a simple bit of shell scripting:



          $ cd /var/www
          $ for d in */; do # *1
          > if [ -f "$dpackage.json" ]; then # *2
          > new_name=$(jq -e -M -r .name "$dpackage.json") # *3
          > if [ $? -eq 0 ] && ! [ -e "$new_name" ]; then # *4
          > mv "$d" "$new_name" # *5
          > fi
          > fi
          > done


          Some notes:



          *1: */ expands to all the directories in the current directory. Each directory name will include a / on the end, so we do not put one in later at *2 and *3.



          *2: Only process directories that have a package.json file.



          *3: Invoke jq to extract the name field from package.json. We invoke jq with -r to output raw strings (i.e. leave off the double quotes), with -M to not have colored output, and -e to have jq exit with an error if there is no name field.



          *4: Check that jq ran successfully (there was a name field) and that the new name for the directory does not already exist. You may want to split these up and add an else if you want to output an error message for the two cases where you're skipping the rename.



          *5: Rename the directory.



          For a test run, I'd put echo in front of the mv command at *5 and check the output to see that the renaming looks right. I haven't tested this myself as I don't have a bunch of directories with package.json files.






          share|improve this answer






















          • I changed ` $? -ne 0` to ` $? -ne 1` and it worked a treat :D
            – Jamie Hutber
            Aug 26 at 19:32






          • 1




            @JamieHutber Yeah, sorry. Stupid typo. I've changed it from -ne to -eq which is better than checking -ne 1 as jq could return a different error code.
            – camh
            Aug 27 at 2:27













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          One of the better tools to use when you need to parse json files is jq. That makes it easy to extract the name field from the package.json file in a directory. Performing the rename is a simple bit of shell scripting:



          $ cd /var/www
          $ for d in */; do # *1
          > if [ -f "$dpackage.json" ]; then # *2
          > new_name=$(jq -e -M -r .name "$dpackage.json") # *3
          > if [ $? -eq 0 ] && ! [ -e "$new_name" ]; then # *4
          > mv "$d" "$new_name" # *5
          > fi
          > fi
          > done


          Some notes:



          *1: */ expands to all the directories in the current directory. Each directory name will include a / on the end, so we do not put one in later at *2 and *3.



          *2: Only process directories that have a package.json file.



          *3: Invoke jq to extract the name field from package.json. We invoke jq with -r to output raw strings (i.e. leave off the double quotes), with -M to not have colored output, and -e to have jq exit with an error if there is no name field.



          *4: Check that jq ran successfully (there was a name field) and that the new name for the directory does not already exist. You may want to split these up and add an else if you want to output an error message for the two cases where you're skipping the rename.



          *5: Rename the directory.



          For a test run, I'd put echo in front of the mv command at *5 and check the output to see that the renaming looks right. I haven't tested this myself as I don't have a bunch of directories with package.json files.






          share|improve this answer














          One of the better tools to use when you need to parse json files is jq. That makes it easy to extract the name field from the package.json file in a directory. Performing the rename is a simple bit of shell scripting:



          $ cd /var/www
          $ for d in */; do # *1
          > if [ -f "$dpackage.json" ]; then # *2
          > new_name=$(jq -e -M -r .name "$dpackage.json") # *3
          > if [ $? -eq 0 ] && ! [ -e "$new_name" ]; then # *4
          > mv "$d" "$new_name" # *5
          > fi
          > fi
          > done


          Some notes:



          *1: */ expands to all the directories in the current directory. Each directory name will include a / on the end, so we do not put one in later at *2 and *3.



          *2: Only process directories that have a package.json file.



          *3: Invoke jq to extract the name field from package.json. We invoke jq with -r to output raw strings (i.e. leave off the double quotes), with -M to not have colored output, and -e to have jq exit with an error if there is no name field.



          *4: Check that jq ran successfully (there was a name field) and that the new name for the directory does not already exist. You may want to split these up and add an else if you want to output an error message for the two cases where you're skipping the rename.



          *5: Rename the directory.



          For a test run, I'd put echo in front of the mv command at *5 and check the output to see that the renaming looks right. I haven't tested this myself as I don't have a bunch of directories with package.json files.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 27 at 2:27

























          answered Aug 26 at 12:26









          camh

          23.6k66050




          23.6k66050











          • I changed ` $? -ne 0` to ` $? -ne 1` and it worked a treat :D
            – Jamie Hutber
            Aug 26 at 19:32






          • 1




            @JamieHutber Yeah, sorry. Stupid typo. I've changed it from -ne to -eq which is better than checking -ne 1 as jq could return a different error code.
            – camh
            Aug 27 at 2:27

















          • I changed ` $? -ne 0` to ` $? -ne 1` and it worked a treat :D
            – Jamie Hutber
            Aug 26 at 19:32






          • 1




            @JamieHutber Yeah, sorry. Stupid typo. I've changed it from -ne to -eq which is better than checking -ne 1 as jq could return a different error code.
            – camh
            Aug 27 at 2:27
















          I changed ` $? -ne 0` to ` $? -ne 1` and it worked a treat :D
          – Jamie Hutber
          Aug 26 at 19:32




          I changed ` $? -ne 0` to ` $? -ne 1` and it worked a treat :D
          – Jamie Hutber
          Aug 26 at 19:32




          1




          1




          @JamieHutber Yeah, sorry. Stupid typo. I've changed it from -ne to -eq which is better than checking -ne 1 as jq could return a different error code.
          – camh
          Aug 27 at 2:27





          @JamieHutber Yeah, sorry. Stupid typo. I've changed it from -ne to -eq which is better than checking -ne 1 as jq could return a different error code.
          – camh
          Aug 27 at 2:27


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464906%2fbatch-rename-folders-with-value-from-json-value-in-package-json%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?