Overview

Hi fellow Linux users! I hope you are you’re ready for some new lessons about Linux. This time we’ll turn to dark side right away, because this article will be devoted to destruction. We’ll learn how to destroy… I meant, how to delete files and folders in Linux.

How to delete files in Linux

Deleting files in Linux terminal is fairly easy. You’ve been already introduced to command in last article when we needed to delete symbolic links. It’s rm (shortly for remove, logicaly) and it command looks like this:

rm filename

We’ll use files we created in an article “How to create multiple files in Linux”. In case you missed that article, you can read it right here »>LINK«< .

Using rm command would look on one of these files would look like this:

rm file1

delete files and folders in linux

As we can see, file1 is successfully removed. Besides rm you can also use unlink command:

unlink file2

Which command you’ll use is up to you. I personally have a habbit of using unlink for symbolic links and rm for removing files, but like I said it is up to you to choose. In the end both will get the job done, so it’s just the matter of preference. Moving on, no time to waste. Surely, by now there are some among you that wonders is there a way to delete more files simultaneously, not just one by one. Of course there is and the most simple one is continuation of rm command:

rm filename1 filename2 filename3 (etc.) – by etc. I mean more filenames, not as part of command.

So, in our case removing the rest of files named file# (file3, file4 and file5) would go like this:

rm file3 file4 file5

delete files and folders in linux

Done! But what to do in cases where filenames are longer and it’s troublesome to type every single one of them? In case filenames share a same name, you can do a little trick with asterisk (*), like this:

rm newfile*

delete files in linux

If you want to remove files with same extension, you only need to move asterisk on the other side:

rm *.txt

delete files and folders in linux

Yup, all .txt files are gone. How about you want to delete some of the files that have same name? No problem at all, just add –i (interactive mode) to command, so it would like this:

rm –i somephp*.php

Bear in mind that if you, by mistake, use –I (capital i), system will ask you only once to remove files, but only if you’re deleting more than 3 of them at once.

If you’re a fan of watching what is going on “behind the curtains”, just add –v (verbose) option to your command and enjoy the show.

rm –v index*.html

Of course, if you just want to get the job done fast and clean with no interruptions, you can always use –f (force), which will delete even write protected files.

How to delete folders in linux

Just like with files, with folders you have two options how to remove them. Either you’ll use

rm –d foldername(where –d is for emtpy folders)

or

rmdir foldername

Let’s test it.

rm –d test1

Works like a charm. Now the second one:

rmdir test2

Yet another command successfully run. Ok, so what about our testdir folder which happens to be non-empty? Let’s try with these commands we just used:

delete files and folders in linux

System notifies us that folder is not empty and cannot delete them. If you’re thinking about using force here, let me just say it’s futile attempt but feel free to try it out.

But don’t be discouraged, there is always a way. All you have to do is add –r (recursive) and show who’s in charge here 

rm –r testdir

delete folders in linux

And testdir full of JavaScript and Python files went to oblivion. If, by any chance, folder had a write protected file and you didn’t want to be bothered with confirmation of deletion, then you would be advised to add –f to command:

rm –rf foldername

Of course, deleting multiple folder at once works just like in case with files:

rm –r foldername1 foldername2 foldername3, or to be specific in our case:

rm –r test1 test2 test3

Summary

So, we covered how to delete files and folders in Linux and that’s one of the most dangerous commands/tools you could know in Linux. I must point out that you must be careful when you’re using these commands. If you are not careful, you could lose data and these commands do NOT send files to trash, they delete it from your hard drive right away.

That’s pretty much everything there is to say about deleting files and folders in Linux. Until next tutorial article stay motivated to learn and practice some new stuff and feel free to ask questions in comments section if you experienced some troubles.