Files, Directories and the Linux Filing System

Tell me about /usr, /dev, /etc, /home and friends

You may have wondered what goes where on your Linux system, and what all of the directories or folders in the / (root) folder on your system are doing. This table lists a typical root partition on a standard Linux system. Yours may differ slightly, but the main ones (/bin, /usr, /lib, /var and /etc will always exist).

Folder Name Contents
/bin The rudimentary minimum set of Linux programs are held here. These programs are essential to the operation of your Linux system. It’s like a system folder, except it only holds binary programs (as the name /bin suggests). Don’t delete anything in here or your system will most likely become broken!
/usr /usr is a place where binary programs are installed into. It is also where your kernel source is held. There are various folders within /usr, such as bin (where installed software goes), src (your Linux Kernel source is here), X11R6 or X11, (The X Window system is here), share (things like icons and pictures, wallpapers and fonts are held here — so that they are shared between your systems users), doc (where the programs on your system hold their documentation or manuals), lib (where all the libraries, such as Qt or an OpenGL driver would be held) , etc (global configurations for installed software) and finally local (local binaries and programs to your system are located here, just another place for bin really).
/dev Large directory which holds files that link to real hardware devices attached to your system, ie: /dev/fd0 is the file for the first floppy disk drive (fd) on your PC. /dev/scd1 is the second SCSI cdrom drive attached to your PC and /dev/ttyS0 is the equivalent of COM1 in DOS. Due to this handy architecture, you can point the output of textfiles or running programs straight to a hardware device, without actually knowing how the hardware works. The kernel knows that. Ie, doing a cat /home/my_modem_at_instructions.txt > /dev/ttyS0 would send the contents of the file my_modem_at_instructions.txt to the modem on COM1 (or ttyS0).
/etc An important directory that you should try not too much to fool with (although some times, you’ve gotta go there to change things). This directory can be basically described as a configuration directory for system-wide applications and resources. For example, the file /etc/lilo.conf holds the configuration for the LInuxLOader program — it boots Linux from your hard drive for you. If you edit this file, this will allow you to change the way that your system boots. The file /etc/X11/XF86Config holds the basic configuration for the X server. Don’t delete this folder. Your system will crumble!
Note: individual users program profiles are stored in their home directories, ie: a single users profile for the bash shell is stored in ~/.bashrc (a file with a dot infront of it is a hidden file btw).
/home This is where all users home directories should be. For example, if your username is jdoe then you will probably have a home directory called /home/jdoe or /home/users/jdoe. All of jdoe’s personal files (excluding applications generally), profiles and directories will be stored in here.
/root The root user’s (system administrator) home directory. Root and ONLY root should be able to access this folder.
/lib Where standard system libraries are stored, such as the pam (Pluggable Authentication Module) library.
/mnt On a UNIX system, all drives are mounted before and after use, instead of just being A:, C: and D:. This means that a drive can just be part of your filesystem (as a directory). Most of the time, people like to put their mounted drives (such as a CDROM or Floppy) in the /mnt (short for mount) folder. Typically, you might have the following folders within a /mnt directory: cdrom, floppy, cdrw and possibly a windows drive: win_c. Whatever you have in the /mnt directory, you will find that the file /etc/fstab (file system table) has an entry relating the /mnt directory to the physical drive. Ie: /mnt/cdrom usually points to /dev/cdrom in the fstab, and /mnt/floppy usually points to /dev/fd0.
/proc If you have a good look at the proc folder, you will find that it’s not actually a real folder at all, it’s a virtual folder – it’s 0 bytes big. Inside /proc, you will find handles for various devices and also, some informative files such as meminfo (if you type cat /proc/meminfo it will tell you how much memory you have and are using). /proc/pci holds all the information about your PCI bus and cards attached to it. Have a look, you can’t edit any of these files.
/tmp Temporary files usually created by running software are held here, sometimes these are called sockets.
/sbin Where system programs are placed. Much like /usr/local/bin and /usr/bin, however, these binaries are usually ran as root, for system maintenance reasons. For example, RedHat/Fedora systems store the ifconfig tool here (which will allow you to alter network settings)
/var Holds files that are generally server oriented, such as mail spools (where mail is held for a user until they pick it up). Lock files are held in /var/run. These files stop multiple instances of one program running by one user if necessary. Red Hat 7 and above also puts the web server in this directory, although Apache’s default directory is /usr/local/apache.
/lost+found Damaged data that has been found on a drive when e2fsck (equivalent of a Scandisk in Windows) runs is put in this folder for personal examination by root.
/opt Placed on your system generally when KDE is installed. Some KDE and Qt Based programs like to install themselves in the folder /opt/kde/bin for some reason.

 

Keeping your files nice and tidy

It’s important that you keep your files tidy on your Linux system, mainly because you’ll find them easier, but also because you want to keep your system secure. Place all of your own personal files in your own folder, and make sure that the permissions are set that only you can access them. For example, if you have lots of documents, consider making a folder in your home directory called docs. In docs you can put all of your documents.

Making folders

To make a folder, you can usually enter your file manager in Gnome or KDE and right click on a white space in the folder that you want to create the folder, then select Create Directory or New Folder.
However, to do it at the terminal, change to the directory that you want to make the folder in (cd /name_of_folder_you_want_to/go_to). Then type:

mkdir new_folder

Operating on files: Executing (running), copying, moving, renaming and deleting files

Binary Execution
To execute a binary program, at the terminal, you can do the following:

/folder_of_program/name_of_binary_file
So, if I wanted to execute the file myscript.pl in the current directory, I could issue:

./myscript.pl
Some programs such as LokiGames software (www.lokigames.com), Sun’s StarOffice and Corel’s WordPerfect Office have their own dedicated binary installers. Most of the time, these sort of binary installers are initially executed from the terminal, by using the example above.

Note that sometimes installer packages come without permission to execute (run), so you will need to provide it permission, try chmod 755 [filename] before you run the program.

Copying files

To copy a file at the terminal, the cp command is used, with the following syntax:

cp source_dir/source.file destination_dir/destination.file

For example, I want to copy myfile.pl from the current directory to /home/bob/perl:

cp myfile.pl /home/bob/perl

Moving files

To move files at the terminal, the format is very similar to the cp command. The move command is mv.
Here is the standard syntax:

mv /source_dir/source.file dest_dir/source.file

So, as a typical example:

mv myscript.pl /home/bob

The mv command has many other arguments (options you can pass to it at run time), such as -R, lets investigate the -R switch:

mv /home/jane/docs/* /home/bob/docs -R

This would copy the entire contents of the folder /home/jane/docs (because we gave it the wildcard *), to the folder /home/bob/docs. Additionaly, as we passed the -R argument, if there were any further directories inside /home/jane/docs (ie: folders called CV, letters and notes could exist inside the docs folder), the -R argument moves the CV, letters and notes folders as well as the docs folder.

Renaming files

Unlike DOS, which had a seperate command to rename files with (ren), Unix simply uses the move (mv) command to do it. Take this example:

mv oldname newname

That would rename the file the file in the current directory called oldname to newname.

Deleting files

Deleting files can be done simply with the rm command. Deletion of emtpy directories can be done with rmdir command. Here are some examples:

Deletion of a single file:
rm myfile.txt

Deletion of every file in current directory:
rm *

Deletion of every file in current directory and all directories within it (recursive deletion):
rm * -Rvf

Note: if you want to delete a directory and all of it’s contents in one swoop, instead of using rm *, then cd .., then rmdir dirname, you can simply do a rm dirname -Rvf.

Removal of an empty directory:
rmdir mydir

Security and files: Permissions and Ownership

In Unix, all files have permissions. Enabling files to have permissions allows security over and above simple password protection to a system. Imagine the situation: You have a Linux server which is used for 5 people from your companies Research and Development department. The five people are:

  • bob
  • jane
  • mary
  • mark
  • simon

Consider for a moment that Bob wanted to keep his data (which is stored on the linux server over the network) private from jane, mary, mark and simon. Without the use of permissions, it would be impossible to do this. So for this reason we have the following permission system in Unix:

/home/bob         bob    users         drwxr-xr-x
What you see above is an edited example of a directory listing on a unix system. You can achieve the same result as what you see above by using the ls command with the long listing function (ls -l). Consider that the above result was an abstract of the output of ls -l /home on the linux server.

The part that reads rwxr-xr-x is the permissions bit for the file (or directory).
The bit that says d, can also read -, s, l or c. – means normal file, s means sticky, l means symbolic link (like a shortcut, but but better) or c is a block device (you’ll find these in the /dev folder). The d bit that is on the listing that we have is because the listing is of a directory.
So, from the above listing already we know that it is a directory, it has some permissions set, and that the directory is called bob and it is in the /home folder.

The Permissions for /home/bob
But what do the permissions rwxr-xr-x actually mean, I hear you cry?! Have a look at this:

UNIX Permissions

Figure 1: Explanation of permissions on a Linux system: UGO = Users, Groups, Others.

As you can see from the diagram, the first rwx is mapped to the owner of the file. This means that the owner (which is presumably bob), has read (r), write (w) and execution (x) access for that folder.

The second permission, r-x is mapped to the group of the directory. Not only can an induvidual user own a file, but a whole group (if desired) can access a file. This example illustrates that everyone in the group users can read the files in the directory, and that they can execute programs within it, or change into that directory.
The third permission, r-x, is for others (commonly referred to as world), which means everyone else. This is the most dangerous permission bit. Our example allows anyone on the server to be able (like the users group) to read anything within the bob directory, and execute files in the directory.

Ideally, what bob should really do, is set the following permissions, for total privacy:
/home/bob         bob    users         drwx------

Using the chmod command to set permissions

When you want to change the permissions of a file, you can use the ch commands: chmod (which changes the rwx bits of a file), chown (which changes the owner of a file or directory) and chgrp (which changes the group which accesses a file or directory).
This part deals with chmod.
Imagine a file called cv.doc with the following permissions User: read, write, execute. Group: read. Others: none, ie:
cv.doc rwx/r–/—
Let’s imagine that we wanted to change the permissions to: rwxrwx—, so user and group can read, write and execute the file cv.doc.
This command would let us change the desired permissions:
chmod ug=rwx,o= cv.doc
So, what we did there, is we ran the chmod command and told it to set the ‘user’ and ‘group’ permissions to read, write and execute. We set the ‘other’ (everyone else) permissions to nothing. Finally, we specified the file that we wanted to change the permissions for, cv.doc. Take this other example, as with Unix, there is always more than one way to do something:
chmod 0770 cv.doc
You will notice the only difference to the above command is the 0770 against the ugo bit. The 0770 (or just 770 would do) means exactly the same thing as ug=rwx,o= . 0770 is the numerical representation for the permissions, octal is a base 8 number system, and the permissions of files are based on this. Ie:
1 =    execute only
2 =    write only
4 =    read only

You will notice, there are 8 (as in octet) combinations of numbers (0 through 7).
If you add 1, 2 and 4 together, you get 7, which is read, write and execute. By taking any combination of all of the numbers 1, 2 or 4, you get your desired permission that you require. For example, 1+2 = 3, and chmod 300 would give you write and execute permissions.

It really dosen’t matter which way you choose to use chmod (either ie: u=rwx,g=rwx,o=rx) or (775), just use what you feel most comfortable with, although it is handy to know how each way works.

Using the chown command to set the owner of files

The syntax of the chown command is relatively simple:
chown bob mydoc.txt
changes the owner from whomever the current owner of mydoc.txt to bob. Note that only the person(s) with write permissions to this file can perform this action.
Quick Tip: chown user.group will change the owner and group of the file

Using the chgrp command to set the group owner of files

As you saw in the previous hint, user.group will quickly change the group of a file or directory, and I personally prefer using chown user.group, but the original and proper way to change the group ownership of a file or a directory, can be done with the chgrp command, as follows:

chgrp users mydoc.txt
…which changes the group ownership of mydoc.txt to users.

Even further in the depths of network file management: NFS

Some of you will have a brief idea of what NFS is, and some of you will have never before heard of NFS, and because of this, I will describe NFS from the top. If you know completely what it is, and just want to know how to set it up, feel free to scroll down a bit until we get to the section ‘Using NFS’.

What Exactly is NFS?

NFS is a method that you can easily and very efficiently share files accross a network in a Unix environment. Almost all types of Unix support NFS, and although not natively, Windows NT can be told to read NFS volumes.

A distinct advantage that NFS takes over the ‘Windows Shares’ mechanism (the system used to share files in Windows), is that NFS does not require to assign a seperate drive on your computer for the use of shared files. Imagine the following scenario:

NFS Example

NFS file sharing, side by side Windows File Sharing (CIFS / SMB).

The image shows that directories of files or drives themselves, under Windows, must be exported to a drive letter (of which only a possible 24 are available). With Linux and Unix, you can export any directory or drive, and it can be viewed under any name on the client.

Imagine the scenario: You have the directory /shared/home on your server (let’s call it alpha). You want your other client (desktop) machines, bravo and charlie to be able to see this folder, and everything inside it. Your two client systems are to have their home directories inside this share. You use NFS for this purpose.
Percieve the users mary (In this example, Mary will use the Linux PC called bravo), and bob (uses the desktop charlie). Mary’s current login directory on bravo is /home/mary, and bob’s login directory on charlie is /home/bob. Both of these directories are local to bravo or charlie and cannot be seen on other machines.
You make two folders inside /shared/home on Alpha, one called mary and one called bob.
You move everything inside mary on her computer (bravo) into her folder on alpha, and you do the same for bob.
You remove the /home directory and all it’s contents on bravo and charlie — as it’s now redundant.
You then export the directory /shared/home to bravo and charlie (and call it /home)

The net result of all of this is that mary and bob (to them anyway, nothing has changed), but to the administrator of the network on alpha, all of the files are stored on the alpha server, and any files they now request from their home directories is now taken from the folder /shared/home on alpha.

Using NFS

Taking the three servers, alpha, beta and charlie a little further, this is a demonstration of how you can actually set it up:

NFS works on the basis of one of the rpc (Remote Procedure Call) tools called nfsd. All good Linux distributions should have rpc tools already with them (although they may be disabled in the sysv init startup), make sure this is installed and ready to go, and make sure that you have installed nfsd on the system that you wish to export your directories or drives (in our case, this is alpha).

Edit the file /etc/exports which is where all your exported volumes are listed. On alpha the format of the file would look like this:
/shared/home      bravo (rw,no_root_squash)      charlie (rw,no_root_squash) 
This is what the above line means to you and I:
1) Take the directory /shared/home 2) Allow the computer bravo to read and write (rw) to it, and allow root to be treated as root over exported filesystems (no_root_squash)
3) Allow the computer charlie to read and write (rw) to it, and allow root to be treated as root over exported filesystems (no_root_squash)

Obviously, alpha must know, either by the file /etc/hosts or by using DNS / NIS how to resolve the hostnames bravo or charlie. If you don’t think you can manage this at your current stage of Linux networking skills, then I recommend simply replacing the hostname (ie: bravo) with the IP address of that particular host (ie: 10.0.0.2).

You may or may not know about the infamous /etc/fstab, but this file holds the key for configuring bravo and charlie with the information they need to see the ‘share’ that alpha has now been set up to provide.
If you investigate /etc/fstab, you will notice that it has all the points of where all your drives are mounted, and this file is read by Linux when it starts up, to mount each drive. NFS Volumes are no different from physical disks, they too get added to this list, assuming that you want the volume to be mounted every time you use the computer.

Adding this line the the /etc/fstab on bravo and charlie would then kick in the mounting of the nfs volume on boot each time:

alpha:/shared/home       /home        rsize=8192,wsize=8192 
And in english:
1) Take the export or ‘share’ on the system alpha (/shared/home) (alpha:/shared/home).
2) Mount it on this computer (either bravo or charlie) as /home (ps: an empty directory with the name /home must exist to put this in). (/home)
3) Make the amount of retrieved bytes per block 8K / 8192 bytes (rsize=8192)
4) Make the amount of sent bytes ber block 8K / 8192 (wsize=8192)

Parts 3 and 4 of the statement are not necessary, but they help to improve efficiency over the network.
Also note that again, if you haven’t set up an /etc/hosts.txt or DNS to resolve the name alpha, use it’s IP address in the place of the ‘alpha:‘, to read something like ‘10.0.0.1:‘.

Once you are sure that nfsd is running correctly, you can do this by typing ps ax | grep “nfsd” at the shell If you get a process called nfsd appearing (don’t confuse this with grep nfsd, which is what you just typed), then you know that nfsd is running, and it sounds like it’s configured correctly.

Now all you need to do is either start nfs manually on alpha, or reboot it, so that the startup script brings it up. You could reboot bravo and charlie, but preferrably, just typing ‘mount -a’ at the root prompt on both of the systems is enough for Linux to re-read it’s /etc/fstab file and cotton on to the fact that it has a new entry. If you get any errors when it re-reads this configuration, make sure to double check all of your settings.

Further Reading

The Linux Documentation Project should have the definitive guide to using NFS, although it’s possibly not too newbie-friendly.

Typing man nfsdman exports and man fstab will all help you, and mainly man exports.

2 Comments on "Files, Directories and the Linux Filing System"

  1. In describing the /bin folder (top of page), you say, “The standard set of GNU programs are held here.” As this website is aimed at Linux newbies, I think it would be good practice to define any term or acronym — like GNU — the first time it’s introduced. Otherwise people like myself might start to unconsciously skim over anything they don’t understand, which is not the way to learn. I could look it up elsewhere, but I’d prefer that a beginner’s guide really start at the beginning.

    On another subject, this page behaves very strangely for me in the Opera browser (version 12.16) on Windows XP Home Edition, SP3. I’ve had to scroll to the bottom at least a dozen times to compose this message because the page keeps scrolling upward whenever I move the mouse! Very strange and annoying. I just move the cursor around the screen and the page jumps up, just far enough to hide this message box, over and over again.

    • Hi Richard,Thanks for the comments. The terms GNU are described on other pages on the web page, but you are right. I wrote those words specifically back in 2001, and I think it holds less relevance now, so I changed the wording to be “The rudimentary minimum set of Linux programs are held here. These programs are essential to the operation of your Linux system. It’s like a system folder, except it only holds binary programs (as the name /bin suggests). Don’t delete anything in here or your system will most likely become broken!”

      Hope you approve!

      I’m not quite sure why the issue exists. I don’t have any machines of quite that age, and I don’t have/use the Opera web browser. Can you try another browser and see if the same issue occurs?

Leave a Reply