Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Linux

How do I force delete a directory in Linux?

by Editorial Staff
July 4, 2019
in Linux
Reading Time: 4 mins read

 

I am trying to delete a directory in Linux using the rmdir dir1 command. But, I am getting an error that read as follows:
rmdir: failed to remove `dir1': Directory not empty

A directory is nothing but a location for storing files on the Linux operating system in a hierarchical format. For example, /bin/ directory would store all executable binary files. This page shows how to remove a directory forcefully when it is not empty using the bash shell command prompt.

 

Table of Contents
Adblock detected 😱
How to force delete a directory in Linux
How to remove a directory that contains other files or directories
How do I remove a full directory in Linux with verbose output?
Delete a non-empty directory in Linux terminal
Learn more about rm command
Conclusion
Posted by: Vivek Gite
Adblock detected 😱
My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

 

How to force delete a directory in Linux

  1. Open the terminal application on Linux.
  2. The rmdir command removes empty directories only. Hence you need to use the rm command to remove files on Linux.
  3. Type the command rm -rf dirname to delete a directory forcefully.
  4. Verify it with the help of ls command on Linux.

How to remove a directory that contains other files or directories

The syntax is:
rm -rf dirName
Say you have a directory named /tmp/data/ that contains two files and one directory as follows:
ls -l /tmp/data/
If you run the rmdir command, you will get an error as follows:
rmdir /tmp/data/
As explained earlier rmdir only delete the DIRECTORIES, if they are empty. Therefore you must use the rm command to remove a full directory in Linux:
rm -rf /tmp/data/
Verify it:
ls -l /tmp/data/

Force delete a directory in Linux using rm command
Use rm command to delete a non-empty directory in Linux terminal

How do I remove a full directory in Linux with verbose output?

Pass the -v option to the rm command as follows:
rm -rfv dirname
For example, delete a full directory named /tmp/bar in Linux and note down the output on the screen:
rm -rfv /tmp/bar/
Remove a full directory in Linux
Where,

  • -r : Recursive delete
  • -f : Force delete a directory
  • -v : Verbose output

Delete a non-empty directory in Linux terminal

In case if you don’t have the permission to delete the folder run rm command as a root user. Otherwise, you will see permission denied message on the screen. Therefore to avoid that prefix sudo at the beginning of the rm command :
sudo rm -rf dirName
OR
sudo rm -rf /somedir/
To remove a folder whose name starts with a ‘-‘, for example ‘-backups’, use one of these commands:
rm -rfv -- -backups/
OR
rm -rfv ./-bacups/

Learn more about rm command

Run:
rm --help
Sample outputs:

  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively; less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i); without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat '/' specially
      --preserve-root[=all]  do not remove '/' (default);
                              with 'all', reject any command line argument
                              on a separate device from its parent
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done
      --help     display this help and exit
      --version  output version information and exit

-f, –force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or
when removing recursively; less intrusive than -i,
while still giving protection against most mistakes
–interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
–one-file-system when removing a hierarchy recursively, skip any
directory that is on a file system different from
that of the corresponding command line argument
–no-preserve-root do not treat ‘/’ specially
–preserve-root[=all] do not remove ‘/’ (default);
with ‘all’, reject any command line argument
on a separate device from its parent
-r, -R, –recursive remove directories and their contents recursively
-d, –dir remove empty directories
-v, –verbose explain what is being done
–help display this help and exit
–version output version information and exit

Conclusion

This page explained how to use the rm command to remove a non-empty directory in Linux. The rmdir command only deletes non-empty directories, so we need to use the rm command.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

[ad_2]

Related Posts

Apache, MySQL Performance

How to Optimize Apache, MySQL Performance for 1GB RAM VPS Centos/RHEL

April 7, 2024
Top 5 MySQL Performance Tuning Tips

Top 5 MySQL Performance Tuning Tips

January 25, 2023

Top Commands to Check the Running Processes in Linux

May 27, 2022

4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error

March 30, 2022

How Common Signals are Used in Kubernetes Pod Management

November 20, 2021

How to Transfer Windows 10 to SSD?

April 14, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Articles

  • Biometric Banking: Why Your Face, Fingerprint, and Voice Are the New PIN
  • The Revolution in Telecommunications: How technology is changing connections
  • Understanding the Differences: BACS vs CHAPS Payment Systems Explained
  • Why CSPM Is the Unsung Hero of Enterprise Cloud Security
  • The New Face of Antivirus: Smarter, Faster, and Built for 2025 Threats
  • What are The Responsibilities of your tax preparer?
  • Best 10 Cryptocurrency Exchange Development Companies 2025

Related Posts

None found

  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.