# How to Remove Files and Directories in Linux

> URL: https://www.atlantic.net/dedicated-server-hosting/how-to-remove-files-and-directories-in-linux/ | Published: 2022-04-26 | Updated: 2023-11-15 | Author: Hitesh Jethva

If you are working on Linux, then you may need to remove certain files and directories based on a set of predetermined requirements. Linux provides several tools to remove files and directories. Among them, the **rm** and **rmdir** are the most widely used commands to remove files and directories on Linux, macOS, and Unix-based operating systems.

In this post, we will show you how to remove files and directories in Linux.

## Remove a Single File

The rm command allows you to remove one or more files in a one-liner command.

To remove a single file, run the following command:

```
rm file1.txt
```

The above command will delete a file without prompting you to confirm file deletion.

If you want to make the **rm** command always prompt before deleting a file, use the **-i** flag:

```
rm -i file1.txt
```

You can use the -v flag to display what the **rm** command is actually doing:

```
rm -v file1.txt
```

If you want to remove a write-protected file without prompting for confirmation, use the **-f** flag:

```
rm -f file1.txt
```

Also Read

[How to Install LFTP to Download and Upload Files in Linux](https://www.atlantic.net/dedicated-server-hosting/how-to-install-lftp-to-download-and-upload-files-in-linux/)

## Remove Multiple Files

The rm command also allows you to remove a set of files by specifying all file names in a single command.

To remove multiple files, run the following command:

```
rm file1.txt file2.txt file3.txt
```

You can also remove multiple files using regular expressions. For example, to remove all files whose names start with a word **notebook**, run the following command:

```
rm notebook*
```

If you want to remove all files with a **png** extension, run the following command:

```
rm *.png
```

Also Read

[How to Install and Use Pigz to Compress File in Linux](https://www.atlantic.net/dedicated-server-hosting/how-to-install-and-use-pigz-to-compress-file-in-linux/)

## Remove Directory in Linux

You can use the **rm** and **rmdir** commands to remove a directory in Linux.

If you want to remove an empty directory, use the **rm -d** or **rmdir** command:

```
rm -d dir1
```

Or

```
rmdir dir1
```

If you want to remove a non-empty directory including all sub-directories and all the files within them, run the following command:

```
rm -r dir1
```

If you want to remove a write-protected directory including all files within them, run the following command:

```
rm -rf dir1
```

To remove multiple directories at once, run the following command:

```
rm -r dir1 dir2 dir3
```

## Find and Remove Directory

You can also use the find command with different options to remove files and directories based on the matching patterns.

To find and remove all empty directories within a given path, run the following command:

```
find /mnt -type d -empty -delete
```

This will find and remove all **empty** directories located inside the /mnt directory.

To find and remove all **empty** files within a given path, run the following command:

```
find /mnt -type f -empty -delete
```

If you want to find and delete all **pdf** files located inside the /etc directory, run the following command:

```
find /etc -type f -name "*.pdf" -exec rm -f {} \;
```

If you want to find and delete all files **owned** by a specific user, run the following command:

```
find /opt -mindepth 1 -user user1 -delete
```

This command will find and remove all files owned by **user1** located inside /opt directory.

If you want to find and remove all files older than **x** days, run the following command:

```
find /mnt -type f -mtime +30 -delete
```

This will find and remove all files inside /mnt directory that is older than **30** days.

## Conclusion

In this post, we explained how to remove files and directories using the **rm** and **rmdir** commands. We also explained how to use the find command to perform some advanced file deletion tasks. You can now easily remove files and directories from the Linux system. Try it on [bare metal servers](https://www.atlantic.net/dedicated-server-hosting/bare-metal-servers/) from Atlantic.Net!
