How to Delete a File or Directory on Linux

In this guide, we will show you how to delete a file or directory on a Linux system.

Linux Remove file or directory

Removing files and folders on Linux is a fairly straightforward task, thanks to the inbuilt tools.

A few different tools are built into the core of Linux that you can use to delete files or directories.

The commands that we can use to delete files and directories on Linux are the following, rm, unlink, and rmdir.

Like learning about how the Linux permission system works, deleting files is also a critical aspect of using the operating system.

How to Delete Files on Linux

The first thing we will show you is how you can delete files on your Linux-based system.

There are two commands that we can use to remove files. The first being the unlink command. The other one we can use is the rm command.

If you would like to learn more about these commands, be sure to check out our guides.

Delete a Single File

Let us start with the most basic thing you can do: delete a single file On Linux.

All you need to do is use either rm or unlink, followed by the name of the file you want to remove.

rm FILENAME
unlink FILENAME

Both of these commands have the same syntax for deleting a single file and behave in the same way so that you can use them interchangeably.

They will remove the link to the file in the system so that it will eventually be overwritten and replaced.

Removing a Write Protected File

When you use rm to delete a file on Linux that the current user does not have write permissions, you will be greeted with a message.

Please note that if you use unlink, you will not receive any warning.

That message would be the one shown below. This gives you a chance to back out of deleting the file.

rm: remove write-protected regular file 'pimylifeup.txt'?

If you want to remove this file, type in Y, then press the ENTER key.

Remove Multiple Files

You can also delete multiple files on your Linux system. However, you will no longer be able to use unlink as that can only handle removing one file at a time.

There are two ways you can use rm to delete multiple files from your device.

The first way is to specify the name of each of the files you want to remove individually.

rm filename1 filename2 filename3...

You can specify as many files as you want to delete using this command.

Alternatively, you can also use the wildcard symbol (*) to select multiple files at once.

For example, if you want to delete all pdf files from a directory, you could use the following command.

rm *.pdf

How to Remove a Directory in Linux

Deleting a directory in Linux is a little different from removing files on Linux.

The two main commands you can use to remove a directory on Linux are rm and rmdir commands.

Removing an Empty Directory

Removing an empty directory is the simplest thing you can do. For this situation, you can use both commands.

The rmdir command does not require any particular arguments. We only need to specify the empty directory.

rmdir EMPTDYIRECTORY

However, to use rm to delete an empty directory, we need to use the “-d” option.

rm -d EMPTDYIRECTORY

If you try to delete a directory containing files, you will receive an error message, as we have shown below.

rm: cannot remove 'test': Directory not empty

Delete a Directory that has Files

If you want to remove a directory that contains files on Linux, you will need to use the rm command.

For this to work, we will need to use the rm command alongside the recursive option (-r).

Using this option with the rm command will empty the directory before eventually removing it.

rm -r DIRECTORYNAME

If any files are write-protected within the directory, you will be prompted to confirm their removal.

Removing Multiple Directories

It is also possible to remove multiple directories from your Linux system at once.

To specify multiple directories, you can use the name for each of them, separating each of the directories by a space.

rm -r DIRECTORYNAME1 DIRECTORYNAME2 ...

If you are using a wildcard, it will also remove directories that match the pattern. You should be careful when using the wildcard.

Conclusion

Hopefully, by this point, you will now have an idea of how you can delete a file or directory on your Linux-based system.

We have shown you some of the simple ways you can use several commands on your system to achieve this.

If you have had any issues with removing a file or directory, please leave a comment below.

Be sure to check out some of our other Linux guides so you can better use the system.

Leave a Reply

Your email address will not be published.