Learning the Command Line – File System

Last time we learned how to list files, now let’s see where those files are.
All your files reside on your hard drive, either internal, external or SSD. These drives have files and folders scattered throughout the drive but most are in certain places and are required for the system to function. Moving or deleting certain files or folders can cause your system to malfunction or even not boot. Best practice is if you don’t know what it is, leave it.

The beginning of your drive, called the root, is what you see if you click on the drive name in Finder. You will see a list of folders similar to what is listed below:
snow

You probably won’t have all the same folders as here. You will find however that you’re not seeing all the folders that are there. Some folders are hidden from Finder. Apple did this so users can not go into system folders without knowing what they are doing. It’s a safety measure. If you open Terminal and type

cd /
ls -l

you will see all the folders at the root of the drive.

craig@zapper:/$ ls -l
total 16141
drwxrwxr-x+ 99 root  admin     3366 Oct 10 05:16 Applications
drwxr-xr-x+ 69 root  wheel     2346 Sep 13 08:37 Library
drwxr-xr-x@  2 root  wheel       68 Aug 16  2012 Network
drwxr-xr-x+  4 root  wheel      136 Mar  1  2013 System
drwxr-xr-x   7 root  admin      238 Mar  1  2013 Users
drwxrwxrwt@  3 root  admin      102 Oct 14 04:47 Volumes
drwxr-xr-x@ 39 root  wheel     1326 Jun 15 07:15 bin
drwxrwxr-t@  2 root  admin       68 Aug 16  2012 cores
dr-xr-xr-x   3 root  wheel     4318 Oct 14 04:43 dev
lrwxr-xr-x@  1 root  wheel       11 Mar  1  2013 etc -> private/etc
dr-xr-xr-x   2 root  wheel        1 Oct 14 04:43 home
dr-xr-xr-x   2 root  wheel        1 Oct 14 04:43 net
drwxr-xr-x   4 root  wheel      136 Apr 14  2013 opt
drwxr-xr-x@  6 root  wheel      204 Mar  1  2013 private
drwxr-xr-x@ 62 root  wheel     2108 Sep 13 08:37 sbin
lrwxr-xr-x@  1 root  wheel       11 Mar  1  2013 tmp -> private/tmp
drwxr-xr-x@ 13 root  wheel      442 Apr 14  2013 usr
lrwxr-xr-x@  1 root  wheel       11 Mar  1  2013 var -> private/var
craig@zapper:/$

The first command cd / means change directory (cd) to the root of the drive (/). And of course ls -l gives a long list. You could have also done it like

ls -l /

Which is the same thing however you didn’t change to the root directory, you just listed it. / not only means the root but it also is the beginning of a path. If you know the path you can do all sorts of things without moving from where you are. If you are in root, to go back to your home directory you could type

cd /Users/yourname/

Where yourname is your home directory name (usually your name). There are a couple of shortcuts which do the same thing (there usually are). For example typing cd ~ will take you back to your home directory. ~ is short for your home dir. Also just typing cd without anything else will take you home.

Now lets make some folders (directories). Make sure you’re in your home dir (cd ~). Now type:

mkdir test

Remember about shortening commands? mkdir is “makedirectory” without the vowels, and a couple of other letters. What you just did is make a directory named “test”. Now let’s go inside that directory. Type cd test. Do a list, (ls). Nothing there. Well not actually true. If you do a ls -la, you will see a dot and two more dots.

craig@zapper:~/test$ ls -la
total 0
drwxr-xr-x   2 staff    68B Oct 14 16:12 ./
drwxr-xr-x+ 78 staff   2.6K Oct 14 16:12 ../
craig@zapper:~/test$

This is system shorthand for this directory (the dot) and the directory it’s in (dot dot). If you wanted to back out of this directory to your home dir you could type

cd ..

This will take you back to the enclosing dir. Notice the space between cd and the dots. The only place this will not work is if you’re at the root of the drive. There is no enclosing dir, you will just stay in the same place. Now make sure you’re in test. We want to create a file here. There are several ways to create a file. You could use TextEdit or nano or one of several apps to create a file. However, for our purposes we want to create an empty file so just type

touch myfile

Among other things, touch will create an empty file with default permissions (in other words, you own it. We’ll get to permissions later). You just made a file named myfile. We did this so I could introduce another command, rm. rm is remove, and now we’re getting to the part where you can really get into trouble if you’re not careful. If you remove the wrong file or directory, bad things can happen. If you don’t know what something is, don’t mess with it. I can’t stress this strongly enough. We’re talking “Oh my, what happened?! Now I have to reinstall EVERYTHING!!!” kind of bad. Chances are, if you made it, you can delete it.

Back out of the test dir by “cd ..”. Do a list to make sure you’re in the right place. Now type

rm test

What did you get?  test: is a directory
It’s still there. rm by itself won’t delete a directory, especially if it has something in it. Remember myfile? If you want to delete a directory and everything in it you need a switch. Type

rm -R test

In this particular case you could have used a lower case r also, either would work. It means recursive, or delete everything inside first, then delete the dir. “test” should now be gone.
Next we’ll learn about permissions and file attributes, or “Why cant I change that file?”.

1 Comments: 

  1. TattooedMac's Avatar
    Nice write up vansmith. Its good to see someone giving a insight to the command line, so much more, so people don't get scared of it. It is such a powerful tool in Mac OS X . . . Im starting to learn it, and i got this great little book called Take Control of the Mac Command Line with Terminal and its a great little reference and help. Thanks for bringing this too light.
Leave a Comment