{"id":2296,"date":"2025-01-13T13:57:04","date_gmt":"2025-01-13T13:57:04","guid":{"rendered":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/?p=2296"},"modified":"2025-01-13T13:57:42","modified_gmt":"2025-01-13T13:57:42","slug":"remove-directory-in-linux","status":"publish","type":"post","link":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html","title":{"rendered":"How To Remove Directory in Linux With Command?"},"content":{"rendered":"\n<p>Every Linux user should know how to manage directories. Removing directories is a common task, whether cleaning up old files or organising your workspace. It might seem daunting for new users, but it\u2019s quite straightforward once you understand the commands.<\/p>\n\n\n\n<p>In this article, we will explore how to remove directories using two primary commands: rmdir and rm.<\/p>\n\n\n\n<p>This guide will show you how to remove directories in Linux using two primary commands: rmdir and rm. Don\u2019t worry if you\u2019re a beginner; we will walk you through step by step!<\/p>\n\n\n\n<p>Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding the Commands<\/strong><\/h2>\n\n\n\n<p>Before we dive into the steps, it\u2019s important to understand the commands you must use to remove the Linux directory.<\/p>\n\n\n\n<p><strong>\u27a2<\/strong> <strong>rmdir<\/strong>: This command removes empty directories. It will not work if your directory contains files or other directories.<\/p>\n\n\n\n<p><strong>\u27a2<\/strong> <strong>rm:<\/strong> This command is more powerful and can remove directories and their contents. Please note that the -r (recursive) option is required to delete non-empty directories.<\/p>\n\n\n\n<hr><p><strong>Also Read: <\/strong><a href=\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/423\/login-ssh-using-password-authentication.html\"><strong>How to Log into SSH using Password Authentication?<\/strong><\/a><\/p><hr>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What You Need to Know Before Removing a Directory<\/strong><\/h2>\n\n\n\n<p>Before we dive in, here are a few things to keep in mind:<\/p>\n\n\n\n<p><strong>\u27a2<\/strong> <strong>Permissions:<\/strong> It is required to have permission to delete a directory. If you don\u2019t own the directory, you might need <strong>sudo<\/strong> to proceed.<\/p>\n\n\n\n<p><strong><strong>\u27a2<\/strong><\/strong> <strong>Empty vs. Non-Empty Directories:<\/strong> As discussed above, Linux treats empty and non-empty directories differently when it comes to deletion. Both have different commands for the process.\u00a0<\/p>\n\n\n\n<hr><p><strong>Also Read: <\/strong><a href=\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/417\/how-to-change-server-time-in-linux.html\"><strong>How to Change Server Time in Linux?<\/strong><\/a><\/p><hr>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps For Removing an Empty Directory<\/strong><\/h2>\n\n\n\n<p>As mentioned above, to remove an empty directory, use the <strong style=\"font-family: Consolas, monospace !important\">rmdir<\/strong> command.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\" ><tbody><tr><td class=\"kb_firewall\"><strong>rmdir &lt;directory_name><\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Let\u2019s say you have an empty directory named <strong style=\"font-family: Consolas, monospace !important\">test_folder<\/strong>.<\/p>\n\n\n\n<p>\u2794 Open your terminal.<\/p>\n\n\n\n<p>\u2794 Navigate to the parent directory where <strong style=\"font-family: Consolas, monospace !important\">test_folder<\/strong> is located using <strong style=\"font-family: Consolas, monospace !important\">cd<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>cd \/path\/to\/parent_directory<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u2794 Before removing it, you need to ensure that the directory is indeed empty by listing its contents:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>Ls test_folder<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>If the directory contains files or subdirectories, they will be listed. If it\u2019s empty, the command will return nothing.<\/p>\n\n\n\n<p>\u2794 Use the <strong style=\"font-family: Consolas, monospace !important\">rmdir<\/strong> command after ensuring that the directory is empty.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>rmdir test_folder<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>If the directory is empty, it will be removed without any issues.<\/p>\n\n\n\n<p>In case you didn\u2019t check before whether the file contains content, you will get an error message like this:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>rmdir: failed to remove &#8216;test_folder&#8217;: Directory not empty<\/strong><br><br><strong>## Command to Remove a Non-Empty Directory<\/strong><br><br><strong>To remove a directory containing files or subdirectories, use the `rm` command with the `-r` (recursive) option.<\/strong><br><br><strong>### Syntax:<\/strong><br><strong>&#8220;`bash<\/strong><br><strong>rm -r &lt;directory_name><\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr><p><strong>Also Read: <\/strong><a href=\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/412\/access-linux-server-using-putty-terminal.html\"><strong>How To Access Linux Server Using PuTTY SSH Terminal?<\/strong><\/a><\/p><hr>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps to Remove a Non-Empty Directory<\/strong><\/h2>\n\n\n\n<p>If you need to delete a directory that contains files or other directories, follow these steps:<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Suppose <strong style=\"font-family: Consolas, monospace !important\">test_folder<\/strong> contains files or subdirectories.<\/p>\n\n\n\n<p>\u2794 Navigate to the parent directory by running the following command.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>cd \/path\/to\/parent_directory<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u2794 Use the <strong style=\"font-family: Consolas, monospace !important\">rm<\/strong> command with the <strong style=\"font-family: Consolas, monospace !important\">-r<\/strong> option:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>rm -r test_folder<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This will remove the directory along with all its contents.<\/p>\n\n\n\n<p><strong>Be Careful:<\/strong><\/p>\n\n\n\n<p>\u2794 Always double-check the directory name before running the <strong style=\"font-family: Consolas, monospace !important\">rm -r<\/strong> command.<\/p>\n\n\n\n<p>\u2794 If you want a confirmation prompt for each file being deleted, use the <strong style=\"font-family: Consolas, monospace !important\">-i<\/strong> option:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>rm -ri test_folder<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr><p><strong>Also Read: <\/strong><a href=\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/343\/Install-and-Configure-a-CSF-Firewall-in-a-CenTOS-Server.html\"><strong>Install &amp; Configure a CSF Firewall in a CenTOS Server<\/strong><\/a><\/p><hr>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps to Remove a Directory with sudo<\/strong><\/h2>\n\n\n\n<p>As discussed above, If you don\u2019t have permission to delete a directory, you can use <strong style=\"font-family: Consolas, monospace !important\">sudo<\/strong> to execute the command with administrative rights.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>sudo rm -r &lt;directory_name><\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>\u2794 Let\u2019s say you want to delete a directory named <strong style=\"font-family: Consolas, monospace !important\">restricted_folder<\/strong> that requires administrative rights. To do so, run the following command.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"kb_firewall\"><strong>sudo rm -r restricted_folder<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>You will be prompted to enter your password. After entering it, the directory will be removed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Removing directories in Linux is straightforward once you understand the commands. Whether it\u2019s an empty directory or one filled with files, you now have the commands to clean up your Linux environment confidently.<\/p>\n\n\n\n<p>By following these steps, even new users can confidently navigate their file systems and perform deletions safely. Practice these commands and remember to handle deletions carefully to avoid accidental data loss.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every Linux user should know how to manage directories. Removing directories is a common task, whether cleaning up old files or organising your workspace. It might seem daunting for new users, but it\u2019s quite straightforward once you understand the commands. In this article, we will explore how to remove directories using two primary commands: rmdir [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-2296","post","type-post","status-publish","format-standard","hentry","category-vps"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Remove Directory in Linux With Command?<\/title>\n<meta name=\"description\" content=\"Learn how to remove directories in Linux with our detailed guide that covers essential commands like rmdir and rm for deleting empty and non-empty directories.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Remove Directory in Linux With Command?\" \/>\n<meta property=\"og:description\" content=\"Learn how to remove directories in Linux with our detailed guide that covers essential commands like rmdir and rm for deleting empty and non-empty directories.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html\" \/>\n<meta property=\"og:site_name\" content=\"Host IT Smart Knowledge base\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-13T13:57:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-13T13:57:42+00:00\" \/>\n<meta name=\"author\" content=\"Admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html\",\"url\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html\",\"name\":\"How To Remove Directory in Linux With Command?\",\"isPartOf\":{\"@id\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#website\"},\"datePublished\":\"2025-01-13T13:57:04+00:00\",\"dateModified\":\"2025-01-13T13:57:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#\/schema\/person\/317c14a877385667f19d3b7496bd5108\"},\"description\":\"Learn how to remove directories in Linux with our detailed guide that covers essential commands like rmdir and rm for deleting empty and non-empty directories.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#website\",\"url\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/\",\"name\":\"Host IT Smart Knowledge base\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#\/schema\/person\/317c14a877385667f19d3b7496bd5108\",\"name\":\"Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d4827c56a6721070dde339640d47ff44ba0d0e515c7e577cf29305ab72383fe3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d4827c56a6721070dde339640d47ff44ba0d0e515c7e577cf29305ab72383fe3?s=96&d=mm&r=g\",\"caption\":\"Admin\"},\"sameAs\":[\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/\"],\"url\":\"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Remove Directory in Linux With Command?","description":"Learn how to remove directories in Linux with our detailed guide that covers essential commands like rmdir and rm for deleting empty and non-empty directories.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html","og_locale":"en_US","og_type":"article","og_title":"How To Remove Directory in Linux With Command?","og_description":"Learn how to remove directories in Linux with our detailed guide that covers essential commands like rmdir and rm for deleting empty and non-empty directories.","og_url":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html","og_site_name":"Host IT Smart Knowledge base","article_published_time":"2025-01-13T13:57:04+00:00","article_modified_time":"2025-01-13T13:57:42+00:00","author":"Admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html","url":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html","name":"How To Remove Directory in Linux With Command?","isPartOf":{"@id":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#website"},"datePublished":"2025-01-13T13:57:04+00:00","dateModified":"2025-01-13T13:57:42+00:00","author":{"@id":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#\/schema\/person\/317c14a877385667f19d3b7496bd5108"},"description":"Learn how to remove directories in Linux with our detailed guide that covers essential commands like rmdir and rm for deleting empty and non-empty directories.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/425\/remove-directory-in-linux.html"]}]},{"@type":"WebSite","@id":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#website","url":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/","name":"Host IT Smart Knowledge base","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#\/schema\/person\/317c14a877385667f19d3b7496bd5108","name":"Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d4827c56a6721070dde339640d47ff44ba0d0e515c7e577cf29305ab72383fe3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d4827c56a6721070dde339640d47ff44ba0d0e515c7e577cf29305ab72383fe3?s=96&d=mm&r=g","caption":"Admin"},"sameAs":["https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/"],"url":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/posts\/2296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/comments?post=2296"}],"version-history":[{"count":9,"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/posts\/2296\/revisions"}],"predecessor-version":[{"id":2306,"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/posts\/2296\/revisions\/2306"}],"wp:attachment":[{"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/media?parent=2296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/categories?post=2296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostitsmart.com\/manage\/knowledgebase\/wp-json\/wp\/v2\/tags?post=2296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}