{"id":29379,"date":"2026-03-18T08:03:17","date_gmt":"2026-03-18T07:03:17","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=29379"},"modified":"2026-05-12T15:56:05","modified_gmt":"2026-05-12T13:56:05","slug":"how-to-remove-directory-linux","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/how-to-remove-directory-linux\/","title":{"rendered":"Delete Directories in Linux: rm and rmdir Explained"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN.webp\" alt=\"Remove a Directory in Linux\" class=\"wp-image-29800\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN.webp 1200w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN-600x315.webp 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN-768x403.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p>Two commands do the heavy lifting when you need to remove a directory in Linux: <strong>rm<\/strong> and <strong>rmdir<\/strong>. They look similar at first glance, but they behave very differently. Getting the wrong one on the wrong directory can delete a lot more than you intended &#8211; and Linux doesn&#8217;t have a recycle bin.<\/p>\n\n\n\n<p>This guide covers both commands, every useful option, and the situations where each one belongs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-linux-rm-and-rmdir-command-overview\">Linux rm and rmdir: Command Overview<\/h2>\n\n\n\n<p>Before running any deletion command, make sure you know where you are in the filesystem. The <code>pwd <\/code>command shows your current working directory; <code>ls<\/code> lists what&#8217;s in it. Both are worth running before you delete anything.<\/p>\n\n\n\n<p>Here&#8217;s the quick reference for which command does what:<\/p>\n\n\n\n<p><strong>rm options at a glance:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>rm -d<\/code> &#8211; remove an empty directory<\/li>\n\n\n\n<li><code>rm -r<\/code> &#8211; recursively remove a directory and all its contents<\/li>\n\n\n\n<li><code>rm -f<\/code> &#8211; skip confirmation prompts when deleting write-protected files<\/li>\n\n\n\n<li><code>rm -rf<\/code> &#8211; recursive deletion, no prompts, write-protected or not<\/li>\n\n\n\n<li><code>rm -i<\/code> &#8211; prompt before every single deletion<\/li>\n\n\n\n<li><code>rm -I<\/code> &#8211; single prompt if more than 3 files are being deleted<\/li>\n\n\n\n<li><code>rm *<\/code> &#8211; wildcard matching multiple characters<\/li>\n\n\n\n<li><code>rm ?<\/code> &#8211; wildcard matching exactly one character<\/li>\n<\/ul>\n\n\n\n<p><strong>rmdir options:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>rmdir -p<\/code> &#8211; remove an empty subdirectory and its parent<\/li>\n\n\n\n<li><code>rmdir -v<\/code> &#8211; print confirmation that the directory was removed<\/li>\n<\/ul>\n\n\n\n<p>The rule of thumb: use <strong>rmdir<\/strong> when you&#8217;re sure the directory is empty and want protection against accidental deletion of files. Use <strong>rm -r<\/strong> when you need to delete directory contents as well.<\/p>\n\n\n\n<p>Before running any deletion command in Linux, always have a current backup. These commands are permanent. There&#8217;s no undo.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-delete-empty-directories-with-rmdir\">Delete Empty Directories with rmdir<\/h2>\n\n\n\n<p><strong>rmdir<\/strong> is the safer choice for removing empty directories. It simply refuses to work on a directory that still contains anything &#8211; files, subdirectories, symlinks &#8211; and returns this error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rmdir: failed to remove 'Directory': Directory not empty<\/code><\/pre>\n\n\n\n<p>That behavior is intentional. It prevents you from accidentally nuking a directory tree when you thought it was already clean.<\/p>\n\n\n\n<p>Basic syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rmdir DirectoryName<\/code><\/pre>\n\n\n\n<p>To remove multiple empty directories in one command, list them as separate arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rmdir Directory_1 Directory_2 Directory_3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-remove-a-subdirectory-and-its-parent\">Remove a Subdirectory and Its Parent<\/h3>\n\n\n\n<p>The -p option removes a subdirectory and walks up the path, deleting each parent directory if it&#8217;s also empty after the child is gone:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rmdir -p \/Directory\/SubDirectory<\/code><\/pre>\n\n\n\n<p>This removes SubDirectory first, then Directory &#8211; but only if Directory contains nothing else at that point.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-confirm-deletion-with-v\">Confirm Deletion with -v<\/h3>\n\n\n\n<p>The <code>-v<\/code> flag prints a message for each directory removed. Useful when removing multiple directories and you want to verify what actually got deleted:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rmdir -v Simple-Directory<br><br>rmdir: removing directory, 'Simple-Directory'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-remove-non-empty-directories-with-rm-r\">Remove Non-Empty Directories with rm -r<\/h2>\n\n\n\n<p><strong>rm<\/strong> is a file deletion command, but combined with <code>-r<\/code> (recursive), it becomes the standard tool for deleting directory trees. The recursive flag tells it to descend into subdirectories and remove everything &#8211; files, folders, the lot.<\/p>\n\n\n\n<p>Basic recursive removal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -r Simple-Directory<\/code><\/pre>\n\n\n\n<p><strong>Warning: <\/strong><em>This deletes the directory and everything inside it permanently. Recovery requires a backup.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-skip-write-protection-prompts-with-rf\">Skip Write-Protection Prompts with -rf<\/h3>\n\n\n\n<p>When a directory or its contents are write-protected, rm will pause and ask for confirmation before each file. If you&#8217;re confident about what you&#8217;re deleting, <code>-rf<\/code> skips all prompts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -rf Simple-Directory<\/code><\/pre>\n\n\n\n<p>Use this carefully. The combination of recursive and force is the most destructive variant of the command. Double-check the path before running it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-delete-multiple-directories-at-once\">Delete Multiple Directories at Once<\/h3>\n\n\n\n<p>You can pass multiple directory names as arguments to remove them all in a single linux delete directory operation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -r Directory_1 Directory_2 Directory_3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-delete-files-in-linux-using-rm-command\">Delete Files in Linux Using rm Command<\/h2>\n\n\n\n<p>Sometimes you want to remove specific files from a directory without touching the directory structure itself. <strong>rm<\/strong> handles that too.<\/p>\n\n\n\n<p>Remove a single file from your current working directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm file.txt<\/code><\/pre>\n\n\n\n<p>Remove multiple files at once:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm file1.txt file2.txt file3.txt<\/code><\/pre>\n\n\n\n<p>Remove a file in a different directory by specifying its path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm dir\/subdir\/file.txt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-safe-deletion-with-confirmation-flags\">Safe Deletion with Confirmation Flags<\/h3>\n\n\n\n<p>The<code> -i<\/code> flag prompts before every deletion. Slower, but safe when you&#8217;re removing files you haven&#8217;t checked recently:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -i file1.txt file2.txt file3.txt<\/code><\/pre>\n\n\n\n<p>Terminal will ask for a Y\/N on each file. If you&#8217;re deleting a large batch and only want one confirmation, use <code>-I<\/code> instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -I file1.txt file2.txt file3.txt<\/code><\/pre>\n\n\n\n<p>To skip all confirmation, including for write-protected files, use <code>-f<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -f file.txt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-wildcards-to-delete-multiple-files\">Using Wildcards to Delete Multiple Files<\/h3>\n\n\n\n<p>Wildcards let you match groups of files with a single command rather than listing them individually. The asterisk matches any number of characters; the question mark matches exactly one.<\/p>\n\n\n\n<p>Delete all .txt files in the current directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm *.txt<\/code><\/pre>\n\n\n\n<p>Delete all files beginning with the letter &#8216;a&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm a*<\/code><\/pre>\n\n\n\n<p>Delete files with single-character extensions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm *.?<\/code><\/pre>\n\n\n\n<p><strong>Warning: <\/strong><em>Run ls first to see exactly which files a wildcard pattern matches before using it with rm. Wildcards don&#8217;t care about file importance.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-vps-file-management-via-ssh-terminal\">VPS File Management via SSH Terminal<\/h2>\n\n\n\n<p>On a <a href=\"https:\/\/contabo.com\/en\/vps\/\">VPS <\/a>or <a href=\"https:\/\/contabo.com\/en\/dedicated-servers\/\">dedicated server<\/a>, you&#8217;ll run these commands over SSH. Connect from a terminal application or SSH client &#8211; on Linux and macOS that&#8217;s the built-in Terminal; on Windows, PuTTY or Windows Terminal work fine.<\/p>\n\n\n\n<p>If you&#8217;d rather not manage files through the command line, most VPS providers let you install a control panel. CyberPanel and <a href=\"https:\/\/contabo.com\/en\/cpanel-servers\/\">cPanel <\/a>both offer a file manager with a graphical interface &#8211; useful for one-off tasks, though slower than the command line for anything repetitive.<\/p>\n\n\n\n<p>For quick access without installing additional software, many providers now include a browser-based terminal directly in the VPS dashboard. Open it, authenticate, and you&#8217;re running bash commands from any browser without a local SSH client.<\/p>\n\n\n\n<p>Whatever access method you use, the linux rm command and rmdir behave identically. The only thing that changes is how you get to the prompt.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-quick-reference-rm-vs-rmdir\">Quick Reference: rm vs rmdir<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>rmdir<\/code> &#8211; empty directories only, safe by default, returns an error if directory has content<\/li>\n\n\n\n<li><code>rm -d<\/code> &#8211; rm&#8217;s equivalent for empty directories<\/li>\n\n\n\n<li><code>rm -r<\/code> &#8211; removes non-empty directories recursively; cannot be undone<\/li>\n\n\n\n<li><code>rm -rf<\/code> &#8211; recursive, no prompts; use only when you&#8217;re certain of what you&#8217;re targeting<\/li>\n\n\n\n<li><code>rm -i<\/code> &#8211; interactive mode; confirmation before each deletion<\/li>\n\n\n\n<li><code>rm * \/ rm ?<\/code> &#8211; wildcards for batch deletion; verify with <code>ls<\/code> before running<\/li>\n<\/ul>\n\n\n\n<p>The linux delete directory operation that gets admins in trouble is almost always <code>rm -rf<\/code> pointed at the wrong path. Slow down, verify the path with <code>pwd<\/code> and <code>ls<\/code>, then execute. That extra five seconds prevents a very bad afternoon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Delete directories in Linux safely with rm and rmdir. Includes empty and non-empty directories, recursive deletion, force flags, confirmation prompts, wildcards, SSH usage, and when to use each command.<\/p>\n","protected":false},"author":44,"featured_media":29800,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","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":"","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":"set","ast-page-background-enabled":"default","ast-page-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":"","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-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":"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":""},"mobile":{"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":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[3402],"class_list":["post-29379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"uagb_featured_image_src":{"full":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN.webp",1200,630,false],"thumbnail":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN-150x150.webp",150,150,true],"medium":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN-600x315.webp",600,315,true],"medium_large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN-768x403.webp",768,403,true],"large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN.webp",1200,630,false],"1536x1536":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN.webp",1200,630,false],"2048x2048":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/04\/blog-head_how2-remove-a-directory-in-linux-rm-and-rmdir-guide_EN.webp",1200,630,false]},"uagb_author_info":{"display_name":"Milan Ivanovic","author_link":"https:\/\/contabo.com\/blog\/author\/milan\/"},"uagb_comment_info":0,"uagb_excerpt":"Delete directories in Linux safely with rm and rmdir. Includes empty and non-empty directories, recursive deletion, force flags, confirmation prompts, wildcards, SSH usage, and when to use each command.","authors":[{"term_id":3402,"user_id":0,"is_guest":1,"slug":"contabro","display_name":"ContaBro","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/29379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=29379"}],"version-history":[{"count":7,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/29379\/revisions"}],"predecessor-version":[{"id":30566,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/29379\/revisions\/30566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media\/29800"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=29379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=29379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=29379"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=29379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}