{"id":4966,"date":"2019-08-03T15:16:28","date_gmt":"2019-08-03T09:46:28","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4966"},"modified":"2026-11-02T14:37:36","modified_gmt":"2026-11-02T09:07:36","slug":"sort-du-output-based-on-file-size","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/sort-du-output-based-on-file-size\/","title":{"rendered":"How to Sort Files and Directories by Size Using the du Command in Linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Post Date:<\/strong> July 31, 2019<br><strong>Last Updated:<\/strong> October 28, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When managing a Linux server, identifying large files and directories is essential for disk space monitoring and troubleshooting. The <code>du<\/code> (disk usage) command helps you check how much disk space files and directories consume.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we explain how to use the <code>du<\/code> command with <code>sort<\/code> to display files and directories in ascending order by size.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, ensure that you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Access to a Linux server.<\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-ssh-remote-server-via-terminal\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/blog\/how-to-ssh-remote-server-via-terminal\/\">A terminal or SSH session.<\/a><\/li>\n\n\n\n<li>Permission to access the directory you want to analyze.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Navigate to the Directory<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open a terminal or connect to your Linux server through SSH. Navigate to the directory you want to check. For example, to inspect <code>\/var\/log<\/code>, run:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd \/var\/log<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Sort Files and Directories by Size<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Execute the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">du -sh -- * | sort -h<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Command explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>du<\/code> \u2014 Displays disk usage.<\/li>\n\n\n\n<li><code>-s<\/code> \u2014 Shows a summary for each file or directory instead of listing every item inside directories.<\/li>\n\n\n\n<li><code>-h<\/code> \u2014 Displays sizes in human-readable units, such as KB, MB, and GB.<\/li>\n\n\n\n<li><code>--<\/code> \u2014 Marks the end of command options.<\/li>\n\n\n\n<li><code>*<\/code> \u2014 Matches non-hidden files and directories in the current directory.<\/li>\n\n\n\n<li><code>sort -h<\/code> \u2014 Sorts the output by human-readable size in ascending order.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Review the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The command displays each file or directory with its disk usage, sorted from smallest to largest.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Sample output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">4.0K    alternatives\n8.0K    apt\n16K     private\n1.2M    unattended-upgrades\n12M     journal\n85M     apache2<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Note: The output above is illustrative. Actual names and sizes will vary depending on your server.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2026-10-28-at-5.58.06-pm-1.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"584\" height=\"992\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2026-10-28-at-5.58.06-pm-1.png\" alt=\"\" class=\"wp-image-12607\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2026-10-28-at-5.58.06-pm-1.png 584w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/08\/Screenshot-2026-10-28-at-5.58.06-pm-1-177x300.png 177w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Optional: Sort from Largest to Smallest<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To list the largest files and directories first, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">du -sh -- * | sort -hr<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>-r<\/code> option reverses the sort order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Optional: Include Hidden Files and Directories<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>*<\/code> wildcard does not include hidden entries. To include hidden files and directories, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">du -sh -- * .[^.]* 2>\/dev\/null | sort -h<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command suppresses errors that may occur when no matching hidden entries exist.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>du -sh -- * | sort -h<\/code> command provides a quick way to identify disk usage across files and directories in a Linux directory. Use <code>sort -hr<\/code> when you need to identify the largest items first, making it easier to investigate disk space issues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. How can I display the largest directories first?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">du -sh -- * | sort -hr<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Does <code>du -sh -- *<\/code> include hidden files?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. The <code>*<\/code> wildcard does not match hidden files and directories by default. Use the optional hidden-file command above to include them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the difference between <code>du<\/code> and <code>df<\/code>?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>du<\/code> reports disk usage for files and directories, while <code>df<\/code> reports filesystem-level space usage, including used and available capacity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. How can I check disk usage for a specific directory?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Specify the directory path directly. For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">du -sh \/var\/log<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-start-the-ssh-service-if-it-fails-after-rebooting-the-server\/\">How to start the SSH service if it fails after rebooting the server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-disable-strict-host-key-checking-in-ssh\/\">How to Disable SSH Strict Host Key Checking in Linux<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Need Assistance?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Need help with Linux server administration, disk space analysis, or troubleshooting? Contact the <a href=\"https:\/\/pheonixsolutions.com\/devops-cloud\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/devops-cloud\/\">Pheonix Solutions <\/a>team for assistance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/contact\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/contact\/\">Contact Pheonix Solutions Support<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: July 31, 2019Last Updated: October 28, 2026 Introduction When managing a Linux server, identifying large files and directories [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[460,703,261],"class_list":["post-4966","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-commands","tag-du","tag-linux","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1i6","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4966"}],"version-history":[{"count":4,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4966\/revisions"}],"predecessor-version":[{"id":12746,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4966\/revisions\/12746"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}