How to Remove Files and Directories in Linux

In a modern operating system, the handling of files and directories is basic and elementary to manipulate them fluently. One of these tasks is to add and create them, but also to delete them. So today you will see different methods to learn how to remove files and directories on Linux.

For Unix-based systems, directory is the same as folder, so don’t worry. You know well that a file is the structure that contains user data. Standard files are made up of a series of characters or stream of information in the form of bytes.

In the case of directories, or folders, they are structures that serve to organize the hierarchy of files in the system. In short, they allow adding files to those folders to have a better access to them.

The names of files and directories on Linux can accept any character, except the following:

! # & ( ) ' " ; | < > @ $ { } * ? \ / TAB

And to create them, just use commands or the graphical interface. Likewise, you can delete them using both ways. Today I will explain how to do it.

Method 1: Remove files and directories on Linux using the graphical interface

A functional desktop operating system could not be functional without a modern and intuitive graphical interface to help both novice and expert users with its operation.

For this example, I will take Linux Mint as an example because it is one of the main distributions. However, the process will be very similar for every file browser on Linux.

The first thing to do is to open the file browser and navigate to the folder or file you want to delete. In this case, I have created the example folder with five text files and a folder with another text file. Thus, the image with the working directory.

Files and Directories on Linux
Files and Directories on Linux

To delete a file or folder using the GUI on Linux, just click on it and when it is selected, press the right mouse button to display the context menu.

Remove files and directories on Linux
Remove files and directories on Linux

There you will see the option to move to the Rubbish Bin.

This option does not permanently delete the file but sends it to a special directory from where you will have a time limit to retrieve it.

So open the recycle trash can from the file browser favorites.

You will see the deleted files, from there you can either recover them or delete them forever. In this case, you can select the file you want to delete permanently and right-click to find the Delete permanently option.

Rubbinsh Bin
Rubbinsh Bin

If there are several of them, then you can select them with the mouse or press the option to empty the trash.

Now the file is permanently deleted. There is no option to recover it.

This same process can be done with directories, obtaining the same results.

Note: In some file managers, it is possible to permanently delete files by selecting the file or folder and pressing SHIFT + DELETE.

Method 2: Using the terminal to remove files and directories on Linux

In the case of many Linux servers, the only possible option to handle it is to use the terminal. This way you can delete files and directories using commands.

Although it may seem difficult, the reality is that it is not and this method is quite flexible because it can be used on any Linux distribution either server or desktop, independent of the desktop environment used.

So, starting from the same structure created above, you can begin.

To delete files, you use the rm command with its different options.

For example, if you want to delete a file within the same directory, you can run

rm [file]

Replace [file] with the full file name.

Moreover, you can specify several files

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

Furthermore, you can specify the full path, in case it is not in the same directory.

rm ~/Pictures/file1.txt /home/user/sample.txt

And so on.

Thanks to the rm command options, you can gain much more flexibility. For example, if you want it to ask before deleting, then you can add -i to it.

rm -i file1.txt

Sample output:

rm: remove regular file 'file1.txt'?

Pressing y and enter will then delete the file.

Another widely used option is -f which does not ask you if you want to remove the file even if it is write-protected.

rm -f file1.txt

The rm command also accepts wildcards. This makes it much easier for automated scripts or to increase productivity.

An example of this, is that to delete all txt files, you can use

rm *.txt

As you can see, it is not as complex as it may seem.

Deleting directories from the terminal

To delete directories, we can use the rm command together with the -r option if it is not empty or the rmdir command. The operation of both is quite similar.

To delete directories, just run

rm -r directory_name

Or with the rmdir command

rmdir directory_name

You can also delete several directories at the same time by running

rm -r directory_name directory2_name

And using absolute paths.

rm -r /home/user/folder

Unlike the graphical interface, here there is no recycle garbage can or trash can and every time you delete files or directories with the rm command they will be permanently deleted.

Conclusion

Deleting files and directories on Linux is easy and allows you to manipulate the system normally. In this post, you learned two methods to do it plus a few other tricks.

I hope you liked this post and help our site grow by sharing it with your friends.

More info: rm command, rmdir command

Share This:

Leave a Comment

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