Linux File System: Part 1

September 5, 2009

You just moved over from Window and are now working with Ubuntu.  The only problem is that you have no idea how the file system is laid out.  In Windows you know that there are partitions D:\ (usually the recovery partition/drive) and C:\ (which acts like the main partition/drive).  Even further, these partitions are visible when you boot up your computer so you don’t have to worry about mounting them.

Now as for a Linux system, EVERYTHING IS A FILE!!! (and if it isn’t a file it’s a process).  Directories like /home (similar to C:\Users\[user name] in Windows) is just a special kind of file, that contains “pointers” do it descendants.  At this point I should mention that in Linux the file system is set up like a tree, with root node /.  Those of you that are familiar with working in DOS will have to get use to using / to navigate between directories and not \.  Really it’s not that hard to do, but does take some getting used to.  And if you are going to say “Why does Linux not adhere to the standard?” Just remember that Linux is based off of Unix which has been around much longer than Windows.  Additionally Windows doesn’t have a great track record when it comes to adhering to standards (IE for example).  Something else to keep in mind is that file names are case sensitive.  That means that THIS.txt is different from tHiS.txt or This.txt and even this.txt.  In Windows this isn’t the case (Thanks Kevin telling me this.  I always entered file names as if they were case sensitive when working with Windows.)

Remember earlier when I said that under Windows you don’t have to mount drives/partitions once you boot up the system?  Well under Linux you don’t have this convenience.  However, there are some advantages to this set up, so you shouldn’t cry too much.  For instance the /usr (the file that contains most of the system executables, I’ll talk about this more later) can be mounted from another computer on your network and everything will work just fine.  Just try moving the folder C:\WINDOWS to another location (have fun fixing your registry errors if you actually do this by the way).  The reason this will work under Linux is because it will still see /usr as a local directory that’s part of the local directory structure.

It is finally time to talk about the different parts of the Linux file system. To see what the directories under / are just go to that directory and enter ls -p. The entries with a / after them are directories, everything else is just a regular file.
On my system the list of directories is:

/bin /boot /dev /etc /home /initrd /lib /lib32 /lost+found /media /mnt /opt /proc /root /sbin /srv /sys /tmp /usr /var

The /lib32 directory seems to be special to 64-bit machines so I won’t be talking about it. However, I figure that it is similar to /lib, but provides support for 64-bit systems.

In part 2-and part 3, if it is needed, I will discuess each of these directories.  Nothing too detailed but it should provide you with a good starting point.


Change Mode

August 16, 2009

So you have written your first program in Perl and it probably looks something like this


#!/usr/local/bin/perl

# We will call the program hello_world
print “Hello world\n”

Now you try to run it with the command ./hello_world, only to find that it won’t run.  Well the reason is obvious, well to experienced users anyway.  The problem is that your program doesn’t have permission to be executed.  Instead you first need to run chmod u+x hello_world, then ./hello_world will run as expected.
The function of the chmod command is to change the mode of a files.  The modes are permissions and special modes.  I will only touch on permissions in this post.  The format for this command is as follows

chmod [references] [operator] [modes] file1...

references: who you want to apply the changes to
operator: how the changes are to be made
modes: which changes are to be made

The references are; u, o, g, and a u is the owner of the file.  g is users who are members of the file’s group.  o is outsider, members who are not specified by either u or g.  Finally a is all, this is equivalent to uog.

The operators are; +, , and =.  As can be expected you use + to add permissions and to remove them.  The = is a little harder to explain but is made perfectly clear with an example (which is later in this post).

The modes are; r, w, x.  They stand for read, write, and executable respectively.

Well now I suppose you would like to see some examples of this in use.  For all these examples we will assume that hello_world has the following initial modes: -rwxr-xr-x, this means that the owner (the first grouping of rwx) has the ability to read, write, and execute.  However, group members (the second grouping) and outsiders (the last grouping) can only read and execute

Example 1: Make it so that the owner only has executable permission.  After using the following command, the file’s modes will look like —xr-xr-x

chmod u=x hello_world

Example 2: Make is so that the owner can only read and execute.  After using the following command, the file’s modes will look like -r-xr-xr-x

chmod u-w hello_world

Example 3: Make it so that group users and outsiders  can read and write only. While not affecting the owner’s permissions.  After using the following command the file’s modes will look like -rwxrw-rw-

chmod og=rw hello_world

Example 4: Make it so that no one has execute ability.  The file’s modes will look like -rw-r–r– after using the following command.

chmod a-x hello_world

Example 5: Make is so that people that aren’t group members or the owner have write permission.  The file’s modes will look like -rwxr-xrwx after using the following command.

chmod o+w hello_world

So that wasn’t too bad, and hopefully you noticed how the = operator works.  Finally, it is worth mentioning that in order to see a file’s modes you only need to use ls -l file.  For more information on chmod check out this Wikipedia entry, and for examples follow this link.


On my First Script

July 15, 2009

Well as promised it is time to explain parts of my script. Well I shall start at the beginning.  The !#/bin/bash tell the OS where the interpretor for the script can be found. The following line is just a place where most of the work will be done.  This should actually be changed to cd ~ if I were planning on sharning this script (which I am).

Now for the more “interesting” aspects.

ls -a | grep “^\.” > temp

This line will first list all the fils found in a directory (ls -a). Then it will find those that begin with a ‘.’ (dot) and print them to a file called temp.  This is done through the use of grep “^\.” grep works with regx, and in the one used here it will match ‘phrases’ that begin with a ‘.’ (dot).  Pretty much grep can do a lot of things (or so I have read) but they rely on regx so needless to say I am currently working on learning regx.  Anyway back to the code. After this I do a search to find a file called .myPw, this is where the password will be stored.  So if the file doesn’t exist we shall create it, along with a password to place into it.

After this I basically use some basic loops to test the user’s input to see if it matches the password.  I won’t talk about the loop structure since 1: this isn’t a tutorial on bash, and 2: it is easy enough to figure out from just reading the code.  I won’t explain what echo does, if you don’t know just click on the man link at the top of this blog and then click on echo.  It’s actually a pretty “simple” command.

One last thing, I would like to point out that after making the file temp I deleted it once I no longer needed it. This turns out to be somewhat optinal (I’ll explain in a moment), but I think it’s just “polite” to delete temporary files you put on someone else’s system.  Now for why this is optional.  The way the code is set up, if there already exist a file called temp it will overwrite it.  If however I had used

ls -a | grep “^\.” >> temp

It would append the output to the end of the file (in which case who knows what was in there before and how much you will have to seach through while looking for .myPw).  So there is a BIG difference between > and >>, both direct output to a file but in differnt fashions.