Easily Delete Files Using Terminal 9

Terminal

Share
Easily Delete Files Using Terminal

Hey

I like terminal, it is so powerful and useful. You can do so much with it. This little tip will show you how you can delete a lot of files, and certain types of files really quickly using Terminal. This tip carries a warning of “Insert Brain Before Doing Anything”, because if you type something incorrectly you can delete your entire hard drive, I am serious one little line and zip no more files. Anyway, I am going to use a bit of caution in this tutorial and you should to.

To being, open up Terminal. This this tip will show you how to delete every file and folder in a certain directory. First navigate to the folder you want using the cd command. You can then type the following. Bewarned if you type this straight into Terminal it will delete your entire user area. If you are in the root of your hard drive, it will delete your entire hard drive.

rm -rf ./*

That will delete everything in that folder. The -f option will not prompt you about file permissions and delete the files anyway, -r will delete the folders and then and files and folders in the folder and so on and so forth. Don’t do this on the root or your home directory because it will be unforgiving and will delete everything. It doesn’t move it the files to the trash. The slash dot at the beginning will make sure it will delete everything forward of the current folder and not anything else, a small security measure. The star is the wild card operator. You can delete certain files by inserting their names separated by spaces.

Another trick which you can use is to delete files by file type using the wild card operator. For example the following will delete everything that is a png file.

rm -rf ./*.png

You can also use this the other way around.

rm -rf ./test.*

That will delete everything called test not matter what the application extension it uses. You can make the wild card more open by adding the following.

rm -rf ./r*.p*

That will delete any application that any file that begins with r and the file extension that begins with p (.png, .psd etc).

The uses of this are endless. For example you can quickly clear out a whole folder in a couple of characters. How long do you think it would take to delete everything out of a folder that contains starts with the letter “t” and is a png file, this folder contains 1000 files and is mixed in with a lot of different files. You could do it within a couple of seconds with Terminal but it would take you a lot longer in Finder.

Have fun with rm, just make sure you don’t type rm -rf /* otherwise bye bye hard drive.

If you want to take your skills with Terminal a bit further I recommend you check out the Terminal Category on this site. If you fancy reading a book there is a couple on Amazon that I regularly see mentioned and recommend, O’reilly Unix Geeks and Unix Under the Hood both are designed for Mac OS X and take Terminal further.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips