Deleting Files and Directories in Linux System
Tags: ubuntu, linux, beginners, computerscience
Published: 2020-05-16 13:30:40

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](https://dev-to-uploads.s3.amazonaws.com/i/9ehxhrvgh24hnmpz99vm.png)

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

![rm file single](https://dev-to-uploads.s3.amazonaws.com/i/qhzibm6wpr6lo2jz28up.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ecwg0wzeulr9j4mxbb7a.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/0pb3lyo4jsl0tw6gen4p.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/qv8b72kw0iou1d8yauv8.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/uns3ho6eht9h81o7r1b3.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/831bqzpxcbln3cmokkqo.png)

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

![rm dir single](https://dev-to-uploads.s3.amazonaws.com/i/u7ljbhyg41wvdcrihkyt.png)

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

- remove multiple directories

![rm multiple dir](https://dev-to-uploads.s3.amazonaws.com/i/gbgvt3gtaoldq77u40gk.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/mrromc0xtggw62kea76f.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/wrpipwrbqf2lo1rkl8yt.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/hdtp1q6k68636h1k0gt1.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ghdfwlucq0uh7lwh5mi4.png)

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.