May I sort the /etc/group and /etc/passwd files?












33















My /etc/group has grown by adding new users as well as installing programs that have added their own user and/or group. The same is true for /etc/passwd. Editing has now become a little cumbersome due to the lack of structure.



May I sort these files (e.g. by numerical id or alphabetical by name) without negative effect on the system and/or package managers?



I would guess that is does not matter but just to be sure I would like to get a 2nd opinion. Maybe root needs to be the 1st line or within the first 1k lines or something?



The same goes for /etc/*shadow.










share|improve this question


















  • 7





    "Editing has now become a little cumbersome due to the lack of structure" Why are you editing those files by hand?

    – Kevin
    Feb 19 '18 at 23:50











  • How does sorting the file help with editing? Is it because you want to group related accounts together, and then do similar changes in a range of rows? But will related account be adjacent if you sort by uid or name?

    – Barmar
    Feb 21 '18 at 20:51











  • @Barmar It has helped mainly because user accounts are grouped by ranges and separate from system accounts (when sorting by UID). Therefore it is easier e.g. to spot the correct line to examine or change when editing with vi.

    – Ned64
    Mar 13 '18 at 23:15
















33















My /etc/group has grown by adding new users as well as installing programs that have added their own user and/or group. The same is true for /etc/passwd. Editing has now become a little cumbersome due to the lack of structure.



May I sort these files (e.g. by numerical id or alphabetical by name) without negative effect on the system and/or package managers?



I would guess that is does not matter but just to be sure I would like to get a 2nd opinion. Maybe root needs to be the 1st line or within the first 1k lines or something?



The same goes for /etc/*shadow.










share|improve this question


















  • 7





    "Editing has now become a little cumbersome due to the lack of structure" Why are you editing those files by hand?

    – Kevin
    Feb 19 '18 at 23:50











  • How does sorting the file help with editing? Is it because you want to group related accounts together, and then do similar changes in a range of rows? But will related account be adjacent if you sort by uid or name?

    – Barmar
    Feb 21 '18 at 20:51











  • @Barmar It has helped mainly because user accounts are grouped by ranges and separate from system accounts (when sorting by UID). Therefore it is easier e.g. to spot the correct line to examine or change when editing with vi.

    – Ned64
    Mar 13 '18 at 23:15














33












33








33


6






My /etc/group has grown by adding new users as well as installing programs that have added their own user and/or group. The same is true for /etc/passwd. Editing has now become a little cumbersome due to the lack of structure.



May I sort these files (e.g. by numerical id or alphabetical by name) without negative effect on the system and/or package managers?



I would guess that is does not matter but just to be sure I would like to get a 2nd opinion. Maybe root needs to be the 1st line or within the first 1k lines or something?



The same goes for /etc/*shadow.










share|improve this question














My /etc/group has grown by adding new users as well as installing programs that have added their own user and/or group. The same is true for /etc/passwd. Editing has now become a little cumbersome due to the lack of structure.



May I sort these files (e.g. by numerical id or alphabetical by name) without negative effect on the system and/or package managers?



I would guess that is does not matter but just to be sure I would like to get a 2nd opinion. Maybe root needs to be the 1st line or within the first 1k lines or something?



The same goes for /etc/*shadow.







group passwd






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 18 '18 at 13:52









Ned64Ned64

2,61411335




2,61411335








  • 7





    "Editing has now become a little cumbersome due to the lack of structure" Why are you editing those files by hand?

    – Kevin
    Feb 19 '18 at 23:50











  • How does sorting the file help with editing? Is it because you want to group related accounts together, and then do similar changes in a range of rows? But will related account be adjacent if you sort by uid or name?

    – Barmar
    Feb 21 '18 at 20:51











  • @Barmar It has helped mainly because user accounts are grouped by ranges and separate from system accounts (when sorting by UID). Therefore it is easier e.g. to spot the correct line to examine or change when editing with vi.

    – Ned64
    Mar 13 '18 at 23:15














  • 7





    "Editing has now become a little cumbersome due to the lack of structure" Why are you editing those files by hand?

    – Kevin
    Feb 19 '18 at 23:50











  • How does sorting the file help with editing? Is it because you want to group related accounts together, and then do similar changes in a range of rows? But will related account be adjacent if you sort by uid or name?

    – Barmar
    Feb 21 '18 at 20:51











  • @Barmar It has helped mainly because user accounts are grouped by ranges and separate from system accounts (when sorting by UID). Therefore it is easier e.g. to spot the correct line to examine or change when editing with vi.

    – Ned64
    Mar 13 '18 at 23:15








7




7





"Editing has now become a little cumbersome due to the lack of structure" Why are you editing those files by hand?

– Kevin
Feb 19 '18 at 23:50





"Editing has now become a little cumbersome due to the lack of structure" Why are you editing those files by hand?

– Kevin
Feb 19 '18 at 23:50













How does sorting the file help with editing? Is it because you want to group related accounts together, and then do similar changes in a range of rows? But will related account be adjacent if you sort by uid or name?

– Barmar
Feb 21 '18 at 20:51





How does sorting the file help with editing? Is it because you want to group related accounts together, and then do similar changes in a range of rows? But will related account be adjacent if you sort by uid or name?

– Barmar
Feb 21 '18 at 20:51













@Barmar It has helped mainly because user accounts are grouped by ranges and separate from system accounts (when sorting by UID). Therefore it is easier e.g. to spot the correct line to examine or change when editing with vi.

– Ned64
Mar 13 '18 at 23:15





@Barmar It has helped mainly because user accounts are grouped by ranges and separate from system accounts (when sorting by UID). Therefore it is easier e.g. to spot the correct line to examine or change when editing with vi.

– Ned64
Mar 13 '18 at 23:15










4 Answers
4






active

oldest

votes


















49














You should be OK doing this: in fact, according to the article and reading the documentation, you can sort /etc/passwd and /etc/group by UID/GID with pwck -s and grpck -s, respectively.






share|improve this answer



















  • 3





    @Menasheh This site's colours don't make them stand out as much as on other sites, but "OK doing this" in this answer is a hyperlink.

    – hvd
    Feb 18 '18 at 22:59






  • 2





    OK, fine, but... In general, are there valid reasons to manually edit /etc/passwd and similar files? Isn't it considered better to access these via the tools that are designed to create and modify them?

    – mickeyf
    Feb 19 '18 at 14:05











  • @mickeyf I've seen people manually edit /etc/passwd when they're making batch changes, like changing the GECOS field for all users due to moving/restructuring (global room or phone number changes, etc.) It's not common anymore, but there are specific reasons that crop up from time to time.

    – ErikF
    Feb 20 '18 at 21:21



















43














Although ErikF is correct that this should generally be okay, I do want to point out one potential issue:



You're allowed to map different usernames to the same UID. If you make use of this, tools that map a UID back to a username will generally pick the first username they find for that UID in /etc/passwd. Sorting may cause a different username to appear first. For display purposes (e.g. ls -l output), either username should work, but it's possible that you've configured some program to accept requests from username A, where it will deny those requests if it sees them coming from username B, even if A and B are the same user.






share|improve this answer































    2














    Having root at first line has been a long time de facto "standard" and is very convenient if you ever have to fix their shell or delete the password, when dealing with problems or recovering systems.



    Likewise I prefer to have daemons/utils users in the middle and standard users at the end of both passwd and shadow.



    hvd answer is also very good about disturbing the users order, especially in systems with many users maintained by hand.



    If you somewhat manage to sort the files, for instance, only for standard users, it would be more sensible than changing the order of all users, imo.






    share|improve this answer





















    • 1





      If you sort numerically by UID, you should get your preferred order. Root is always 0, and daemons conventionally have UIDs under 100.

      – Barmar
      Feb 21 '18 at 20:13











    • @Barmar If sorting by UID and not by name, indeed, thanks for remembering.

      – Rui F Ribeiro
      Feb 21 '18 at 20:16





















    0














    Here is a bit of somewhat hairy bash code to similarly sort the shadow/gshadow files safely (using vipw -s/vigr -s) and in-place from the command line:



    EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/passwd ^V^J
    :wq" >/dev/null 2>&1" vipw -s

    EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/group ^V^J
    :wq" >/dev/null 2>&1" vigr -s


    NOTE:





    1. ^V^J is CtrlVCtrlJ which generates a new line - this new line is needed to separate the vim command lines.


    2. vi -c passes the command to the vim editor which is assigned to be the editor for vipw/vigr by the EDITOR variable

    3. The awk code sorts shadow by passwd (and gshadow by group)

    4. The backslashes are just the multiple levels of escape needed corresponding to the quotation levels.





    share










    New contributor




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




















      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/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%2funix.stackexchange.com%2fquestions%2f424967%2fmay-i-sort-the-etc-group-and-etc-passwd-files%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      49














      You should be OK doing this: in fact, according to the article and reading the documentation, you can sort /etc/passwd and /etc/group by UID/GID with pwck -s and grpck -s, respectively.






      share|improve this answer



















      • 3





        @Menasheh This site's colours don't make them stand out as much as on other sites, but "OK doing this" in this answer is a hyperlink.

        – hvd
        Feb 18 '18 at 22:59






      • 2





        OK, fine, but... In general, are there valid reasons to manually edit /etc/passwd and similar files? Isn't it considered better to access these via the tools that are designed to create and modify them?

        – mickeyf
        Feb 19 '18 at 14:05











      • @mickeyf I've seen people manually edit /etc/passwd when they're making batch changes, like changing the GECOS field for all users due to moving/restructuring (global room or phone number changes, etc.) It's not common anymore, but there are specific reasons that crop up from time to time.

        – ErikF
        Feb 20 '18 at 21:21
















      49














      You should be OK doing this: in fact, according to the article and reading the documentation, you can sort /etc/passwd and /etc/group by UID/GID with pwck -s and grpck -s, respectively.






      share|improve this answer



















      • 3





        @Menasheh This site's colours don't make them stand out as much as on other sites, but "OK doing this" in this answer is a hyperlink.

        – hvd
        Feb 18 '18 at 22:59






      • 2





        OK, fine, but... In general, are there valid reasons to manually edit /etc/passwd and similar files? Isn't it considered better to access these via the tools that are designed to create and modify them?

        – mickeyf
        Feb 19 '18 at 14:05











      • @mickeyf I've seen people manually edit /etc/passwd when they're making batch changes, like changing the GECOS field for all users due to moving/restructuring (global room or phone number changes, etc.) It's not common anymore, but there are specific reasons that crop up from time to time.

        – ErikF
        Feb 20 '18 at 21:21














      49












      49








      49







      You should be OK doing this: in fact, according to the article and reading the documentation, you can sort /etc/passwd and /etc/group by UID/GID with pwck -s and grpck -s, respectively.






      share|improve this answer













      You should be OK doing this: in fact, according to the article and reading the documentation, you can sort /etc/passwd and /etc/group by UID/GID with pwck -s and grpck -s, respectively.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Feb 18 '18 at 14:12









      ErikFErikF

      2,9111513




      2,9111513








      • 3





        @Menasheh This site's colours don't make them stand out as much as on other sites, but "OK doing this" in this answer is a hyperlink.

        – hvd
        Feb 18 '18 at 22:59






      • 2





        OK, fine, but... In general, are there valid reasons to manually edit /etc/passwd and similar files? Isn't it considered better to access these via the tools that are designed to create and modify them?

        – mickeyf
        Feb 19 '18 at 14:05











      • @mickeyf I've seen people manually edit /etc/passwd when they're making batch changes, like changing the GECOS field for all users due to moving/restructuring (global room or phone number changes, etc.) It's not common anymore, but there are specific reasons that crop up from time to time.

        – ErikF
        Feb 20 '18 at 21:21














      • 3





        @Menasheh This site's colours don't make them stand out as much as on other sites, but "OK doing this" in this answer is a hyperlink.

        – hvd
        Feb 18 '18 at 22:59






      • 2





        OK, fine, but... In general, are there valid reasons to manually edit /etc/passwd and similar files? Isn't it considered better to access these via the tools that are designed to create and modify them?

        – mickeyf
        Feb 19 '18 at 14:05











      • @mickeyf I've seen people manually edit /etc/passwd when they're making batch changes, like changing the GECOS field for all users due to moving/restructuring (global room or phone number changes, etc.) It's not common anymore, but there are specific reasons that crop up from time to time.

        – ErikF
        Feb 20 '18 at 21:21








      3




      3





      @Menasheh This site's colours don't make them stand out as much as on other sites, but "OK doing this" in this answer is a hyperlink.

      – hvd
      Feb 18 '18 at 22:59





      @Menasheh This site's colours don't make them stand out as much as on other sites, but "OK doing this" in this answer is a hyperlink.

      – hvd
      Feb 18 '18 at 22:59




      2




      2





      OK, fine, but... In general, are there valid reasons to manually edit /etc/passwd and similar files? Isn't it considered better to access these via the tools that are designed to create and modify them?

      – mickeyf
      Feb 19 '18 at 14:05





      OK, fine, but... In general, are there valid reasons to manually edit /etc/passwd and similar files? Isn't it considered better to access these via the tools that are designed to create and modify them?

      – mickeyf
      Feb 19 '18 at 14:05













      @mickeyf I've seen people manually edit /etc/passwd when they're making batch changes, like changing the GECOS field for all users due to moving/restructuring (global room or phone number changes, etc.) It's not common anymore, but there are specific reasons that crop up from time to time.

      – ErikF
      Feb 20 '18 at 21:21





      @mickeyf I've seen people manually edit /etc/passwd when they're making batch changes, like changing the GECOS field for all users due to moving/restructuring (global room or phone number changes, etc.) It's not common anymore, but there are specific reasons that crop up from time to time.

      – ErikF
      Feb 20 '18 at 21:21













      43














      Although ErikF is correct that this should generally be okay, I do want to point out one potential issue:



      You're allowed to map different usernames to the same UID. If you make use of this, tools that map a UID back to a username will generally pick the first username they find for that UID in /etc/passwd. Sorting may cause a different username to appear first. For display purposes (e.g. ls -l output), either username should work, but it's possible that you've configured some program to accept requests from username A, where it will deny those requests if it sees them coming from username B, even if A and B are the same user.






      share|improve this answer




























        43














        Although ErikF is correct that this should generally be okay, I do want to point out one potential issue:



        You're allowed to map different usernames to the same UID. If you make use of this, tools that map a UID back to a username will generally pick the first username they find for that UID in /etc/passwd. Sorting may cause a different username to appear first. For display purposes (e.g. ls -l output), either username should work, but it's possible that you've configured some program to accept requests from username A, where it will deny those requests if it sees them coming from username B, even if A and B are the same user.






        share|improve this answer


























          43












          43








          43







          Although ErikF is correct that this should generally be okay, I do want to point out one potential issue:



          You're allowed to map different usernames to the same UID. If you make use of this, tools that map a UID back to a username will generally pick the first username they find for that UID in /etc/passwd. Sorting may cause a different username to appear first. For display purposes (e.g. ls -l output), either username should work, but it's possible that you've configured some program to accept requests from username A, where it will deny those requests if it sees them coming from username B, even if A and B are the same user.






          share|improve this answer













          Although ErikF is correct that this should generally be okay, I do want to point out one potential issue:



          You're allowed to map different usernames to the same UID. If you make use of this, tools that map a UID back to a username will generally pick the first username they find for that UID in /etc/passwd. Sorting may cause a different username to appear first. For display purposes (e.g. ls -l output), either username should work, but it's possible that you've configured some program to accept requests from username A, where it will deny those requests if it sees them coming from username B, even if A and B are the same user.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 18 '18 at 17:28









          hvdhvd

          1,410815




          1,410815























              2














              Having root at first line has been a long time de facto "standard" and is very convenient if you ever have to fix their shell or delete the password, when dealing with problems or recovering systems.



              Likewise I prefer to have daemons/utils users in the middle and standard users at the end of both passwd and shadow.



              hvd answer is also very good about disturbing the users order, especially in systems with many users maintained by hand.



              If you somewhat manage to sort the files, for instance, only for standard users, it would be more sensible than changing the order of all users, imo.






              share|improve this answer





















              • 1





                If you sort numerically by UID, you should get your preferred order. Root is always 0, and daemons conventionally have UIDs under 100.

                – Barmar
                Feb 21 '18 at 20:13











              • @Barmar If sorting by UID and not by name, indeed, thanks for remembering.

                – Rui F Ribeiro
                Feb 21 '18 at 20:16


















              2














              Having root at first line has been a long time de facto "standard" and is very convenient if you ever have to fix their shell or delete the password, when dealing with problems or recovering systems.



              Likewise I prefer to have daemons/utils users in the middle and standard users at the end of both passwd and shadow.



              hvd answer is also very good about disturbing the users order, especially in systems with many users maintained by hand.



              If you somewhat manage to sort the files, for instance, only for standard users, it would be more sensible than changing the order of all users, imo.






              share|improve this answer





















              • 1





                If you sort numerically by UID, you should get your preferred order. Root is always 0, and daemons conventionally have UIDs under 100.

                – Barmar
                Feb 21 '18 at 20:13











              • @Barmar If sorting by UID and not by name, indeed, thanks for remembering.

                – Rui F Ribeiro
                Feb 21 '18 at 20:16
















              2












              2








              2







              Having root at first line has been a long time de facto "standard" and is very convenient if you ever have to fix their shell or delete the password, when dealing with problems or recovering systems.



              Likewise I prefer to have daemons/utils users in the middle and standard users at the end of both passwd and shadow.



              hvd answer is also very good about disturbing the users order, especially in systems with many users maintained by hand.



              If you somewhat manage to sort the files, for instance, only for standard users, it would be more sensible than changing the order of all users, imo.






              share|improve this answer















              Having root at first line has been a long time de facto "standard" and is very convenient if you ever have to fix their shell or delete the password, when dealing with problems or recovering systems.



              Likewise I prefer to have daemons/utils users in the middle and standard users at the end of both passwd and shadow.



              hvd answer is also very good about disturbing the users order, especially in systems with many users maintained by hand.



              If you somewhat manage to sort the files, for instance, only for standard users, it would be more sensible than changing the order of all users, imo.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 20 '18 at 13:08

























              answered Feb 19 '18 at 17:53









              Rui F RibeiroRui F Ribeiro

              39.7k1479132




              39.7k1479132








              • 1





                If you sort numerically by UID, you should get your preferred order. Root is always 0, and daemons conventionally have UIDs under 100.

                – Barmar
                Feb 21 '18 at 20:13











              • @Barmar If sorting by UID and not by name, indeed, thanks for remembering.

                – Rui F Ribeiro
                Feb 21 '18 at 20:16
















              • 1





                If you sort numerically by UID, you should get your preferred order. Root is always 0, and daemons conventionally have UIDs under 100.

                – Barmar
                Feb 21 '18 at 20:13











              • @Barmar If sorting by UID and not by name, indeed, thanks for remembering.

                – Rui F Ribeiro
                Feb 21 '18 at 20:16










              1




              1





              If you sort numerically by UID, you should get your preferred order. Root is always 0, and daemons conventionally have UIDs under 100.

              – Barmar
              Feb 21 '18 at 20:13





              If you sort numerically by UID, you should get your preferred order. Root is always 0, and daemons conventionally have UIDs under 100.

              – Barmar
              Feb 21 '18 at 20:13













              @Barmar If sorting by UID and not by name, indeed, thanks for remembering.

              – Rui F Ribeiro
              Feb 21 '18 at 20:16







              @Barmar If sorting by UID and not by name, indeed, thanks for remembering.

              – Rui F Ribeiro
              Feb 21 '18 at 20:16













              0














              Here is a bit of somewhat hairy bash code to similarly sort the shadow/gshadow files safely (using vipw -s/vigr -s) and in-place from the command line:



              EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/passwd ^V^J
              :wq" >/dev/null 2>&1" vipw -s

              EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/group ^V^J
              :wq" >/dev/null 2>&1" vigr -s


              NOTE:





              1. ^V^J is CtrlVCtrlJ which generates a new line - this new line is needed to separate the vim command lines.


              2. vi -c passes the command to the vim editor which is assigned to be the editor for vipw/vigr by the EDITOR variable

              3. The awk code sorts shadow by passwd (and gshadow by group)

              4. The backslashes are just the multiple levels of escape needed corresponding to the quotation levels.





              share










              New contributor




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

























                0














                Here is a bit of somewhat hairy bash code to similarly sort the shadow/gshadow files safely (using vipw -s/vigr -s) and in-place from the command line:



                EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/passwd ^V^J
                :wq" >/dev/null 2>&1" vipw -s

                EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/group ^V^J
                :wq" >/dev/null 2>&1" vigr -s


                NOTE:





                1. ^V^J is CtrlVCtrlJ which generates a new line - this new line is needed to separate the vim command lines.


                2. vi -c passes the command to the vim editor which is assigned to be the editor for vipw/vigr by the EDITOR variable

                3. The awk code sorts shadow by passwd (and gshadow by group)

                4. The backslashes are just the multiple levels of escape needed corresponding to the quotation levels.





                share










                New contributor




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























                  0












                  0








                  0







                  Here is a bit of somewhat hairy bash code to similarly sort the shadow/gshadow files safely (using vipw -s/vigr -s) and in-place from the command line:



                  EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/passwd ^V^J
                  :wq" >/dev/null 2>&1" vipw -s

                  EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/group ^V^J
                  :wq" >/dev/null 2>&1" vigr -s


                  NOTE:





                  1. ^V^J is CtrlVCtrlJ which generates a new line - this new line is needed to separate the vim command lines.


                  2. vi -c passes the command to the vim editor which is assigned to be the editor for vipw/vigr by the EDITOR variable

                  3. The awk code sorts shadow by passwd (and gshadow by group)

                  4. The backslashes are just the multiple levels of escape needed corresponding to the quotation levels.





                  share










                  New contributor




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










                  Here is a bit of somewhat hairy bash code to similarly sort the shadow/gshadow files safely (using vipw -s/vigr -s) and in-place from the command line:



                  EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/passwd ^V^J
                  :wq" >/dev/null 2>&1" vipw -s

                  EDITOR="/usr/bin/vi -c "1,\$!awk 'BEGIN {FS = \":\"} FNR==NR {x2[\$1] = \$0; next} \$1 in x2 {print x2 [\$1]}' - /etc/group ^V^J
                  :wq" >/dev/null 2>&1" vigr -s


                  NOTE:





                  1. ^V^J is CtrlVCtrlJ which generates a new line - this new line is needed to separate the vim command lines.


                  2. vi -c passes the command to the vim editor which is assigned to be the editor for vipw/vigr by the EDITOR variable

                  3. The awk code sorts shadow by passwd (and gshadow by group)

                  4. The backslashes are just the multiple levels of escape needed corresponding to the quotation levels.






                  share










                  New contributor




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








                  share


                  share








                  edited 49 secs ago









                  Olorin

                  3,2441417




                  3,2441417






                  New contributor




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









                  answered 9 mins ago









                  puterboyputerboy

                  11




                  11




                  New contributor




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





                  New contributor





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






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






























                      draft saved

                      draft discarded




















































                      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%2f424967%2fmay-i-sort-the-etc-group-and-etc-passwd-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

                      サソリ

                      広島県道265号伴広島線

                      Accessing regular linux commands in Huawei's Dopra Linux