DEV Community

yash sugandh
yash sugandh

Posted on

Deleting Files and Directories in Linux System

In the previous post, we explored how to create single, multiple, even thousands of files and directories with the use of a single touch command, mkdir command, and the use of brace expansion.

Now let us look at how to delete the files and directories.

1.rm command

The rm command stands for "remove" is used to delete or remove files and directories.

The syntax for rm command

rm syntax

For deleting a file we only need to call rm command along with the file-name.

rm file single

In the above example, we first use the ls command to check all the files present in the current directory and then used the rm file2.txt to delete a single file.

Let’s take a few scenarios for better understanding

How to

  • delete multiple files at once

rm multiple files

In the above example, we deleted files file1.txt, file3.txt, and file5.txt at one go.

Okay, so we mention all the file_names we want to delete after rm.

Okay, we now know how to delete multiple files at once but what if they are present in different directories?

  • delete files from different directories at once

rm files multiple directories

In the above example, we first use the ls command to list the files present in the current directory(represented by the "." after ls) and in the logs directory. After we found the files we then used rm command to remove those files.

Now we know how to delete multiple files at once but is there a way we can delete all the text files at once?

Yes, there is. Do you guys remember we discussed Wildcards this is one of the situations where they will come in handy?

  • remove all the text files(.txt) from the current working directory

So basically if we want to remove files from multiple directories at once what we need to do is to provide the path to those files to the rm command.

rm file star wildcard

In the above example, we first used ls command to list out all the files from the current working directory then we use the command rm *.txt to remove all the .txt files from the directory.

Let's break down the command
rm represents the remove command
* represents a wildcard which matches any character
.txt represents the text files.

Similarly, you can try using other wildcards to practice the deletion of files.

In the last post, we learned bracket expansion. So let's try to use the bracket expansion to delete the required files

rm file bracket

In the above example, we first listed all the files using ls command and then used the rm file{1..10} to remove file1 to file10 i.e. 10 files using a single command.

These are the ways we can delete a file. Now let's have a look at how to delete a directory.

Before we start deleting directories we first need to know the difference between deleting a file and deleting a directory.

When deleting a directory we need to know Whether this directory is empty or not if it's not empty then what's inside this directory?

Let's take this step by step

1.Deleting an empty directory

Deleting an empty directory is easy we already no since the directory is empty we will not be impacting anything.

But How do we do it?

Do you remember in the last post we used mkdir to create a directory similarly there is a command which can be used to delete empty directories, Can you guess what can that be?

Yeah. You guessed it right!

rmdir command

The Syntax for rmdir

rm-dir-syntax

The rmdir stands for remove directory is used to remove empty directories.

rm dir single

For removing a directory we only need to call rmdir command along with the directory_name.

  • remove multiple directories

rm multiple dir

In the above example, we deleted the directory logs1, logs2, and logs3 at one go.

Okay, so we mention all the directory_names we want to delete after rmdir.

  • remove all the directories that start with "logs" and have exactly one character after that

rm-dir-question

In the above example, we used the wildcard ? and formed the command rmdir logs?

Let's break down the command
rmdir represents remove directory
logs represents the text that directory_name should start with
? represents exactly one character after the text "logs"

We can do the same using the brace expansion as follows:

rmdir-bracket

Now we have a good idea of how we can delete the directories. But what about Directories with data

2.Deleting Directories with some Data

  • remove a directory with data

rm dir -r

To remove a directory with data we use rm -r <directory_name>.

Let's break down the command rm -r logs
rm represents remove command
-r represents the option recursively
logs directory to delete

The main problem that we can have if we don't use this command carefully is that we can even delete those files that are important without notice.

Is there any way we can prevent this?

Yes, there is a way we can prevent this. Let's check it out

  • remove a directory with data and prompt

rm-dir -ri

To remove a directory with data and prompt we use rm -ri <directory_name>.

Let's break down the command rm -r logs
rm represents remove command
-r represents the option recursively
i represents interactive(prompts)
logs directory to delete

This option allows us to confirm every file before deletion and when we decide to keep a few files in the directory then it does not allow us to delete the directory.

Along with the -ri option we can use all the things that we have learned i.e. wildcards and brace expansion.

Okay, so that’s all the commands we need to know for Deleting Files and Directories in Linux System.

I hope you understood the basics of file manipulation in Linux. Please, let me know if there are any questions.

Top comments (5)

Collapse
 
bananado profile image
Noam Silvy

Maybe add a quick discussion on the fact that the files are deleted permanently but it's easy write a "garbage" script that you can use instead of rm

Collapse
 
yashsugandh profile image
yash sugandh

Hey Noam Silvy,

Thank you so much for your suggestion.

Since the post is addressed to all the users from beginner level and we have not reached the scripting part yet I did not add the "garbage script" in the post but I will surely add a link in the discussion soon for everyone who is interested to look into.

Collapse
 
yashsugandh profile image
yash sugandh • Edited

Added the post for trash-cli please have a look

dev.to/yashsugandh/command-line-re...

Collapse
 
sfjuocekr profile image
Sjoer van der Ploeg

Please add [] bracket explanation as well?

rm file[123]

Collapse
 
yashsugandh profile image
yash sugandh

Hey Sjoer van der Ploeg,

Let's take an example

  • remove all the files that start with "file" and has exactly one character after that 1, 2 or 3

rm-file-wildcard-bracket

In the above example, we used wildcard [] and created command rm file[123] where

rm represents remove command
file represents the text "file" in the filename
[123] represents exactly one character out of values 1, 2 or 3

If you still have any queries related to wildcards please look at the previous post on Wildcards(*,?,[],[[:class:]]) for better understanding.