{"id":11446,"date":"2019-03-14T07:48:36","date_gmt":"2019-03-14T07:48:36","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11446"},"modified":"2019-03-14T07:48:36","modified_gmt":"2019-03-14T07:48:36","slug":"learn-how-to-use-dir-command-with-different-options-and-arguments-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/learn-how-to-use-dir-command-with-different-options-and-arguments-in-linux\/","title":{"rendered":"Learn How to Use \u2018dir\u2019 Command with Different Options and Arguments in Linux"},"content":{"rendered":"<p>This article shows some examples of using the\u00a0<strong>dir<\/strong>\u00a0command to list the contents of a directory. The\u00a0<strong>dir<\/strong>command is not a commonly used command in Linux. Though it works more less like the\u00a0<a href=\"https:\/\/www.tecmint.com\/15-basic-ls-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>\u00a0which most Linux users prefer to use. We\u2019ll be discussing the\u00a0<strong>dir<\/strong>\u00a0command where we shall look at how to use different options and arguments.<\/p>\n<div id=\"attachment_15368\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Linux-dir-Command-Usage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15368\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Linux-dir-Command-Usage-620x297.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Linux-dir-Command-Usage-620x297.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Linux-dir-Command-Usage.png 717w\" alt=\"dir Command Usage in Linux\" width=\"620\" height=\"297\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">dir Command Usage in Linux<\/p>\n<\/div>\n<p>The general syntax of the\u00a0<strong>dir<\/strong>\u00a0command is as follows.<\/p>\n<pre># dir [OPTION] [FILE]\r\n<\/pre>\n<div id=\"attachment_15347\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/dir-command-syntax.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15347\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/dir-command-syntax.png\" alt=\"dir Command Syntax\" width=\"383\" height=\"27\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">dir Command Syntax<\/p>\n<\/div>\n<h3>dir Command Usage with Examples<\/h3>\n<h4>Simple output of the dir command<\/h4>\n<pre># dir \/\r\n<\/pre>\n<div id=\"attachment_15348\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/dir-Command-Output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15348\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/dir-Command-Output.png\" alt=\"dir Command Output\" width=\"491\" height=\"63\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">dir Command Output<\/p>\n<\/div>\n<p>Output of the\u00a0<strong>dir<\/strong>\u00a0command with the\u00a0<code>\/etc<\/code>\u00a0directory file is as follows. As you can see from the output not all files in the\u00a0<strong>\/etc<\/strong>\u00a0directory are listed.<\/p>\n<pre># dir \/etc\r\n<\/pre>\n<div id=\"attachment_15349\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Etc-directory-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15349\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Etc-directory-Files.png\" alt=\"List \/etc Directory\" width=\"459\" height=\"199\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List \/etc Directory<\/p>\n<\/div>\n<p>To list one file per line use\u00a0<strong>-1<\/strong>\u00a0option as follows.<\/p>\n<pre># dir\r\n# dir -1\r\n<\/pre>\n<div id=\"attachment_15350\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-per-Line.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15350\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-per-Line.png\" alt=\"List Files per Line\" width=\"438\" height=\"137\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Files per Line<\/p>\n<\/div>\n<h4>View all files in a directory including hidden files<\/h4>\n<p>To list all files in a directory including\u00a0<code>.<\/code>\u00a0(hidden) files, use the\u00a0<strong>-a<\/strong>\u00a0option. You can include the\u00a0<strong>-l<\/strong>\u00a0option to format output as a list.<\/p>\n<pre># dir -a\r\n# dir -al\r\n<\/pre>\n<div id=\"attachment_15351\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Hidden-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15351\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Hidden-Files.png\" alt=\"List Hidden Files\" width=\"577\" height=\"168\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Hidden Files<\/p>\n<\/div>\n<div id=\"attachment_15352\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Long-List-Hidden-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15352\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Long-List-Hidden-Files.png\" alt=\"Long List Hidden Files\" width=\"403\" height=\"245\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Long List Hidden Files<\/p>\n<\/div>\n<h4>View directory entries instead of content<\/h4>\n<p>When you need to list only directory entries instead of directory content, you can use the\u00a0<strong>-d<\/strong>\u00a0option. In the output below, the option\u00a0<strong>-d<\/strong>\u00a0lists entries for the\u00a0<strong>\/etc<\/strong>\u00a0directory.<\/p>\n<p>When you use\u00a0<strong>-dl<\/strong>, it shows a long listing of the directory including owner, group owner, permissions.<\/p>\n<pre># dir -d \/etc\r\n# dir -dl \/etc\r\n<\/pre>\n<div id=\"attachment_15353\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Long-List-Etc-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15353\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Long-List-Etc-Directory.png\" alt=\"Long List \/etc Directory\" width=\"360\" height=\"82\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Long List \/etc Directory<\/p>\n<\/div>\n<h4>View index number of files<\/h4>\n<p>In case you want to view the index number of each file, use the option\u00a0<strong>-i<\/strong>. From the output below, you can see that first column shows numbers. These numbers are called\u00a0<strong>inodes<\/strong>\u00a0which are sometimes referred to as index nodes or index numbers.<\/p>\n<p>An\u00a0<strong>inode<\/strong>\u00a0in Linux systems is a data storage on a filesystem that stores information about a file except the\u00a0<strong>filename<\/strong>\u00a0and its actual data.<\/p>\n<pre># dir -il\r\n<\/pre>\n<div id=\"attachment_15354\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Index-Number-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15354\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Index-Number-Files.png\" alt=\"List Index Number of Files\" width=\"554\" height=\"257\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Index Number of Files<\/p>\n<\/div>\n<h4>List files and their allocated sizes in blocks<\/h4>\n<p>You can view files sizes using the\u00a0<strong>-s<\/strong>\u00a0option. If you need to sort the files according to size, then use the\u00a0<strong>-S<\/strong>\u00a0option.<\/p>\n<p>In this case you need to also use the\u00a0<strong>-h<\/strong>\u00a0option to view the files sizes in a human-readable format.<\/p>\n<pre># dir -shl\r\n<\/pre>\n<div id=\"attachment_15355\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-with-Sizes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15355\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-with-Sizes.png\" alt=\"List Files with Sizes\" width=\"509\" height=\"261\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Files with Sizes<\/p>\n<\/div>\n<p>In the output above, the first column shows the size of files in\u00a0<strong>Kilobytes<\/strong>. The output below shows a sorted list of files according to their sizes by using the\u00a0<strong>-S<\/strong>\u00a0option.<\/p>\n<pre># dir -ashlS \/home\/kone\r\n<\/pre>\n<div id=\"attachment_15356\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Sort-Files-with-Sizes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15356\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Sort-Files-with-Sizes.png\" alt=\"Sort Files with Sizes\" width=\"488\" height=\"121\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Sort Files with Sizes<\/p>\n<\/div>\n<p>You can also sort by modification time, with the file that has recently been modified appearing first on the list. This can be done using the\u00a0<strong>-t<\/strong>\u00a0option.<\/p>\n<pre># dir -ashlt \/home\/kone\r\n<\/pre>\n<div id=\"attachment_15357\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Sort-Files-with-Modification-Time.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15357\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/Sort-Files-with-Modification-Time.png\" alt=\"Sort Files by Modification Time\" width=\"465\" height=\"123\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Sort Files by Modification Time<\/p>\n<\/div>\n<h4>List files without owner or group owner<\/h4>\n<p>To list files without their owners, you have to use\u00a0<strong>-g<\/strong>\u00a0option which works like the\u00a0<strong>-l<\/strong>\u00a0option only that it does not print out the file owner. And to list files without group owner use the\u00a0<strong>-G<\/strong>\u00a0option as follows.<\/p>\n<pre># dir -ahgG \/home\/kone\r\n<\/pre>\n<div id=\"attachment_15358\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-without-Owner.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15358\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-without-Owner.png\" alt=\"List Files without Owner\" width=\"388\" height=\"122\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Files without Owner<\/p>\n<\/div>\n<p>As you can notice from the output above that the name of the file owner and the group owner are not printed. You can as well view the author of a file by using the\u00a0<strong>\u2013author<\/strong>\u00a0flag as follows.<\/p>\n<pre># dir -al --author \/home\/kone\r\n<\/pre>\n<div id=\"attachment_15359\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/View-Author-of-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15359\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/View-Author-of-Files.png\" alt=\"View Author of Files\" width=\"472\" height=\"124\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">View Author of Files<\/p>\n<\/div>\n<p>In the output above, the fifth column shows the name of the author of a file. The\u00a0<strong>examples.desktop<\/strong>\u00a0files is owned by user\u00a0<strong>kone<\/strong>, belongs to group\u00a0<strong>kili<\/strong>\u00a0and it was authored by user\u00a0<strong>kone<\/strong>.<\/p>\n<h4>List directories before other files<\/h4>\n<p>You may wish to view directories before all other files and this can be done by using the\u00a0<strong>\u2013group-directories-first<\/strong>flag as follows.<\/p>\n<pre># dir -l --group-directories-first\r\n<\/pre>\n<div id=\"attachment_15360\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Directory-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15360\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Directory-Files.png\" alt=\"List Group Directory Files\" width=\"535\" height=\"120\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Group Directory Files<\/p>\n<\/div>\n<p>When you observe the output above, you can see that all the directories are listed before the regular files. The letter\u00a0<code>d<\/code>\u00a0before the permissions indicates a directory and\u00a0<code>a<\/code>\u00a0indicates a regular file.<\/p>\n<p>You can also view subdirectories recursively, meaning that you can list all other subdirectories in a directory using the\u00a0<strong>-R<\/strong>\u00a0option as follows.<\/p>\n<pre># dir -R\r\n<\/pre>\n<div id=\"attachment_15361\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Directories-Recursively.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15361\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Directories-Recursively.png\" alt=\"List Directories Recursively\" width=\"476\" height=\"301\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Directories Recursively<\/p>\n<\/div>\n<p>In the above output, the\u00a0<code>(.)<\/code>\u00a0sign means the current directory and home directory of user\u00a0<strong>Kone<\/strong>\u00a0has three subdirectories that is\u00a0<strong>Backup<\/strong>,\u00a0<strong>dir<\/strong>\u00a0and\u00a0<strong>Docs<\/strong>.<\/p>\n<p>The\u00a0<strong>Backup<\/strong>\u00a0subdirectory has two other subdirectories that is\u00a0<strong>mariadb<\/strong>\u00a0and\u00a0<strong>mysql<\/strong>\u00a0which have no subdirectories.<\/p>\n<p>The\u00a0<strong>dir<\/strong>\u00a0subdirectory does not have any subdirectory. And the Docs subdirectory has two subdirectories namely\u00a0<strong>Books<\/strong>\u00a0and\u00a0<strong>Tuts<\/strong>\u00a0which do not have subdirectories.<\/p>\n<h4>View user and group IDs instead of names<\/h4>\n<p>To view user and group\u00a0<strong>IDs<\/strong>, you need to use\u00a0<strong>-n<\/strong>\u00a0option. Let us observe the difference between the next two outputs.<\/p>\n<p>Output without\u00a0<strong>-n<\/strong>\u00a0option.<\/p>\n<pre># dir -l --author\r\n<\/pre>\n<div id=\"attachment_15362\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-Without-IDs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15362\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-Without-IDs.png\" alt=\"List Files Without ID's\" width=\"453\" height=\"122\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Files Without ID\u2019s<\/p>\n<\/div>\n<p>Output with\u00a0<strong>-n<\/strong>\u00a0option.<\/p>\n<pre># dir -nl --author\r\n<\/pre>\n<div id=\"attachment_15363\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-with-IDs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15363\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Files-with-IDs.png\" alt=\"List Files with ID's\" width=\"453\" height=\"122\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Files with ID\u2019s<\/p>\n<\/div>\n<h4>View entries separated by commas<\/h4>\n<p>This can be archived by using\u00a0<strong>-m<\/strong>\u00a0option.<\/p>\n<pre># dir -am\r\n<\/pre>\n<div id=\"attachment_15364\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Entries-by-Comma.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15364\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/08\/List-Entries-by-Comma.png\" alt=\"List Entries by Comma\" width=\"544\" height=\"48\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Entries by Comma<\/p>\n<\/div>\n<p>To find help in using the dir command use\u00a0<strong>\u2013help<\/strong>\u00a0flag and to view version details of dir use\u00a0<strong>\u2013version<\/strong>.<\/p>\n<h3>Conclusion<\/h3>\n<p>These are just examples of basic usage of the\u00a0<strong>dir<\/strong>\u00a0command, to use many other options see the manual entry for\u00a0<strong>dir<\/strong>\u00a0command on your system.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-dir-command-usage-with-examples\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article shows some examples of using the\u00a0dir\u00a0command to list the contents of a directory. The\u00a0dircommand is not a commonly used command in Linux. Though it works more less like the\u00a0ls command\u00a0which most Linux users prefer to use. We\u2019ll be discussing the\u00a0dir\u00a0command where we shall look at how to use different options and arguments. dir &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/learn-how-to-use-dir-command-with-different-options-and-arguments-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn How to Use \u2018dir\u2019 Command with Different Options and Arguments in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11446","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11446"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11446\/revisions"}],"predecessor-version":[{"id":11447,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11446\/revisions\/11447"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}