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 to Delete Files and Directories in Linux

by Editorial Staff
May 12, 2019
in Linux
Reading Time: 3 mins read

You may, in some cases, need to delete directories and files that you do not need to free up space on your hard drive. In this brief tutorial, you will learn how to delete files and directories in Linux.

To remove files, the rm command is used. A word of caution, using the rm command to delete files and directories in Linux is irreversible. Therefore, extra caution should be taken.

Deleting a file

To delete a single file using the rm command, use the syntax below

$ rm filename

For example, we will create a text file file1.txt using the touch command. To delete the file run

$ rm file1.txt

Output

removing a single file with rm command in Linux

Deleting multiple files

To remove many files in a single command using the rm command, use the syntax

$ rm file1 file2 file3

Once again,  we will create 3 files – file1.txt file2.txt file3.txt using the touch command. To remove 3 files –  – in one command run

$ rm file1.txt file2.txt file3.txt

Output

remove several files with a single command

This result can also be achieved by making use of the wildcard symbol (*). In the example below, The wildcard matches and deletes all files with the extension .txt

$ rm *.txt

Remove files interactively

If you wish to confirm whether to remove files before deleting them, use the -i flag as shown

$ rm -i file1.txt

Output

remove a file interactively

Force remove files

To remove files forcefully, especially ones that have been write-protected, use the -f flag as shown below

$ rm -f file1.txt

Output

force remove a file using rm -f command

Removing empty directories

If you want to remove an empty directory, use the -d flag with the rm command

$ rm -d directory_name

Let’s create an empty directory and call it data

To remove the directory run

$ rm -d data

Output

remove empty directory in Linux

The same result can be achieved using the rmdir command. This is short for remove directory. The syntax is as shown below.

$ rmdir directory_name

In our second example, we are going to create a new directory and call it linux . We will then remove it using the rmdir command.

Output
remove directories in Linux using rmdir command

Deleting non-empty directories

If you want to remove non-empty directories together will all files, use the -r flag as shown

$ rm -r directory_name

In this example, we have a directory called distros containing 5 files namely centos, debian, fedora and mint.

To remove the entire directory alongside its contents run

$ rm -r distros

Deleting multiple directories recursively

If you wish to delete or remove multiple directories at once use the syntax below

$ rm -r directory_1 directory_2 directory_3

For instance, to remove 3 directories namely data1 data2 & data3 run

$ rm -r data1 data2 data3

Output
remove multiple directories recursively

Forcefully and recursively remove directories

If you want to recursively and forcefully remove directories without being prompted, , use the combination of  -rfflag as shown below

$ rm -rf directory_name

Wrapping up

Using the examples in this article, you now know how to delete a file in Linux safely. We value your feedback. Drop us a comment in the comment section. Keep it locked for more informative and insightful articles.

Related Posts

Top 5 MySQL Performance Tuning Tips
Linux

Top 5 MySQL Performance Tuning Tips

January 25, 2023
Top Commands to Check the Running Processes in Linux
Linux

Top Commands to Check the Running Processes in Linux

May 27, 2022
4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error
Linux

4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error

March 30, 2022

Leave a Reply Cancel reply

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

Recent Articles

  • Top 26 Best AnimeRebel Alternatives To Watch Anime Free
  • The Benefits of Shooting in Raw: Why and How to Convert
  • High-Definition Monitoring: Introducing the Noorio B200
  • 11 Sites Like Netskope In 2023
  • EDI vs. APIs? Why Not Both!
  • 10 Apps Like Paired In 2023
  • 7 Money-Making Business Schemes for Techies
  • 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.