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:
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:
As we can see, file1 is successfully removed. Besides rm you can also use unlink command:
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
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:
If you want to remove files with same extension, you only need to move asterisk on the other side:
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:
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:
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
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
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.