Wednesday, April 27, 2011

Linux #2: More Basics - Removing Directories

-------- How to Remove Directories and the Files/Folders within Them  --------

++++++++++++++ The Reason It Can Be Tricky +++++++++++++++++

Last time I taught you how to do some simple commands via the terminal in Ubuntu, including the ability to create directories. This time I will teach you how to remove directories. Removing directories is a fairly simple task... most of the time it can be done graphically via the GUI by simply right clicking a folder and choosing "Move To Trash". However, on occasion you will find yourself unable to do this because the folder has an icon of a lock next to it, signifying that You do not have permission to delete the folder or its contents. This is because as you go about normal activities in Ubuntu, you do Not have administrative/root privileges by default. The only way to gain root privileges is to type "sudo" into the terminal before any command you may want to execute. This is true for any and just about every command you will run... if it is of any importance, it will require you to type "sudo" before hand, and then prompt you for your account password.

+++++++++++  Removal Solely Via the Terminal  ++++++++++++++++

$>sudo rmdir /path/to/dir/to/be/removed

If there are other subdomains, AND they are empty, find
furthest subdomain, NAVIGATE TO IT (cd /path/ as explained earlier), and type:

$>sudo rmdir -p /removed/deepest/sub/dir/

This removes each parent directory, starting from the deepest directory. Which is subsequently equivalent to typing: [again, assuming All directories are Empty]
$>rmdir /removed/deepest/sub/dir/
$>rmdir /removed/deepest/sub/
$>rmdir /removed/deepest/
$>rmdir /removed/

However, the Easiest way to do this is to
use the "Recursively Remove Files" flag, -rf
This will recursively remove all folders and files within the domain...
ultimately doing what we wanted to do all along, minus all the bullshit...

sudo rmdir -rf /path/to/dir/to/be/removed/

This ^^^^^^^^^^^^^^^ is what you should take home from this post.

+++++++++++++  Getting All GOOEY To Do It  +++++++++++++++++

This still must start via the terminal, because typically everything does.
ProTip! Cool People use the terminal!
You are going to launch the program (nautilus in Ubuntu) which
manages file navigation graphically, as root, by typing:

$>sudo nautilus

ProTip! (for realzies this time)... toss an ampersand after your command in order to break it free of still having to run inside the terminal and being virtually connected to the terminal as such:
$>sudo nautilus &

If no Ampersand[&] is placed at the end of your command, the GUI will open for you to navigate through files and folders with root access, but you cannot use that terminal because it will be tied up spitting out text output from the Nautilus program, and more importantly, if you decide to close the terminal, your window showing you whatever folder with whatever files will be closed immediately without warning (though the terminal will remind you it is running a program before it lets you close it completely. Though usually it does not specify what.)

Sorry I haven't posted in a while. Been busy doing this and that. Will post about this and that later in the future. Also, I think I am going to start posting MUCH more Advanced stuff really really soon, skipping the basics and just going back to them every now and then... Ultimately I want this blog to be something I can go back and reference when I need help with something soooo, there will be some good stuff in the soon to near future I promise!


  1. Thanks for all the information. Great blog.

  2. I am using rm -Rf nameOfDirectory the most.