{"id":22082,"date":"2023-10-01T16:07:46","date_gmt":"2023-10-01T16:07:46","guid":{"rendered":"https:\/\/linuxways.net\/?p=22082"},"modified":"2023-10-01T16:08:09","modified_gmt":"2023-10-01T16:08:09","slug":"list-users-with-awk-command-in-ubuntu","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/list-users-with-awk-command-in-ubuntu\/","title":{"rendered":"List Users with awk Command in Ubuntu"},"content":{"rendered":"<p>For administrators, in Ubuntu, listing users is a crucial task for User Management as It helps in improving security by checking for unauthorized users. User Management can also be used to troubleshoot problems in Ubuntu. The \u201cawk\u201d command is a useful way to display Users in Ubuntu by listing them in the Terminal. The \u201cawk\u201d Command is used as a search pattern. It is used to find patterns in a file or filter a text file according to specified criteria.<\/p>\n<p>This article explains the different methodologies in which the \u201cawk\u201d command can be used to list users in Ubuntu.<\/p>\n<h2><strong>List Users With the \u201cawk\u201d Command in Ubuntu<\/strong><\/h2>\n<p>As the \u201cawk\u201d command is used to filter search, it can be used with the \u201cgetent passwd\u201d command. The \u201cgetent passwd\u201d is used to retrieve all users. The \u201cawk\u201d can be used with it to filter based on a query.<\/p>\n<p>To list the details of users, the \u201cgetent passwd\u201d command can be used:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">getent<\/span> <span class=\"kw2\">passwd<\/span><\/div><\/div>\n<p>This will list all the users and their complete details including the UID as well:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1005\" height=\"443\" class=\"wp-image-22092\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-1.png 1005w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-1-300x132.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-1-768x339.png 768w\" sizes=\"auto, (max-width: 1005px) 100vw, 1005px\" \/><\/p>\n<p>To display only the names of the users, the \u201cawk\u201d command is used with the \u201cgetent passwd\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">getent<\/span> <span class=\"kw2\">passwd<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> -F: <span class=\"st_h\">'{print $1}'<\/span><\/div><\/div>\n<p>This will display all the user names only in the terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"404\" class=\"wp-image-22095\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-2.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-2-300x167.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<h2><strong>List User UID with the \u201cawk\u201d Command in Ubuntu<\/strong><\/h2>\n<p>The \u201cgetent passwd\u201d Command returns the list of users and additional details of the users as well. The UID of the users is also included and can be returned or listed using the \u201c-F print{}\u201d command with awk specifying to return only the specific column. To return only the User ID, the third column of the list is included in the \u201cawk\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">getent<\/span> <span class=\"kw2\">passwd<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> -F: <span class=\"st_h\">'{print $3}'<\/span><\/div><\/div>\n<p>The \u201cprint $3\u201d command ensures to display the third column of the Users List, i.e. the UIDs of users:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"397\" class=\"wp-image-22100\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-3.png 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-3-300x164.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<h2><strong>List Filtered User UID With \u201cawk\u201d Command in Ubuntu<\/strong><\/h2>\n<p>Conditions can also be passed to filter users. To display the users having a UID less than 100, the command used is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">getent<\/span> <span class=\"kw2\">passwd<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> -F: <span class=\"st_h\">'{if($3&lt;100) print $1}'<\/span><\/div><\/div>\n<p>The \u201c($3&lt;100)\u201d ensures to return the users having UID less than 100:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"348\" class=\"wp-image-22104\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-4.png 730w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22082-4-300x143.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>The \u201cawk\u201d command is useful to filter the search. It is used with the \u201cgetend passwd\u201d to return users of the specific types according to your specific needs. This article explained different ways in which you can use the \u201cawk\u201d command to list users.<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>The \u201cawk\u201d command is useful to filter the search and is used with the \u201cgetend passwd\u201d to return users of our own choice according to specific needs.<\/p>","protected":false},"author":110,"featured_media":22192,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1049,2],"tags":[],"class_list":["post-22082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-commands","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22082"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22082\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22192"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}