Jump to content

CL-07: Understanding the Directory Structure


Recommended Posts

Browse: [About the FAQ Forum] [Table of Contents] [FAQs] [Contribute]

 

CL-07: Understanding & Manuevering Through the Directory Structure

 

(I couldn't really name this anything else, but man! It's so long! I had to chop it to fit in the subject line)

 

The purpose of this article is to aid your understanding of the way the linux filesystem, or directories (folders), are laid out, and to help you learn the basics of manuevering through them, along with the methods on manipulating files within your linux domain.

 

Understanding the Directory Structure

 

Without covering every directory, here's a metaphor to help you understand the directories (folders), are laid out.

 

/ is the yard, and the directory /home is the house where you keep your stuff. In /home, there can be (and will be) different directories (rooms) for the different people who live in the house.

 

Let's say Joe, the owner of the computer, is going to let his little sister Jen use his computer. There's a room in the house for both Joe and Jen; /home/joe and /home/jen. That is where each of them can keep their personal stuff, and neither of them have rights to the other's directory. You see, in linux, every file, like an mp3, and even every directory, like /home/joe, belongs to somebody. We'll see how great this can be for security and stability later.

 

The directory /dev is the garage where the devices are kept. Within /dev there's /dev/cdrom (for your CD drive), /dev/fd0 (for your floppy drive), and so on. However, these are more like files that define the device drive rather than what is on the cdrom in the drive, say.

 

/mnt is a special directory. Distributions usually mount devices in /dev for you, but here's what is happening. To use a device, i.e. to be able to see files on a cdrom, you need to mount the /dev/cdrom to /mnt/cdrom. Now, if you look in /mnt/cdrom, you will see the files on the plastic disc in the drive /dev/cdrom. Picture /dev/cdrom as the engine, and /mnt/cdrom the running car. Which would you rather get into? The same goes for your floppy disks - /dev/fd0 usually gets mounted to /mnt/floppy.

 

So let's recap the filesystem layout:

 

/ - the root directory - every other directory is contained within / (and that is why we usually write their names preceeded with a / - it's the root of the filesystem.)

 

/bin - This isn't a bin for trash! This is where system binaries are kept. Binaries are executable files that you can run, and they are for programs that run the operating system.

 

/boot - All the files for booting up your computer and loading the OS are kept in here.

 

/dev - All your devices, like CDROMs and floppy drives, are in here

 

/etc - this is where the system configuration files are kept (the way the OS is setup, so to speak)

 

/home - Within home, there is always a directory for each user. If your name is Joe, you are only allowed to download and save files (or change existing files) in /home/joe (sometimes called ~)

 

/usr - this is where applications go. Inside /usr looks very much like a mini / but the difference is instead of OS binaries or configs, it's the binaries or configs for applications not vital to the OS such as office or games. All users are allowed to run apps in /usr , but they aren't allowed to alter or delete the files there in any way.

 

- /usr/bin - binaries for apps like office or games

- /usr/etc - configs or setup for them

- /usr/src - source code for the apps (preinstalled packages)

 

/var - If you're running a server, the files that you are serving other people on the internet (like music or webpages) would be contained in here.

 

Using the Command Line

OK, now onto command line fun. And it is fun, so don't leave now! It may take more than one reading to grasp it all, but a million mile journey starts with a single step. Throughout this tutorial, I will give commands in quotes; You don't type the quotes, I just do to avoid confusion between command text and tutorial text, capice?

 

When we talk about going "into" a directory, like opening a folder in a graphical user interface (GUI) with your mouse, we will be able to view the contents of it. "ls" (for list) will list the files and directories within your current working directory (CWD), that is, the directory you are currently sitting in. When you first log onto the computer, or when you first open a "terminal" in X windows (X windows is a GUI incredibly similar to microsoft's GUI called windows), you are at the command line, and your working directory is usually /home/joe to start (if your name is joe - and for now IT IS >:| )

 

Let's look at the command line. It looks kinda funny, doesn't it?

[joe@joes.computer joe]$

looks really funny! What this is telling you is joe is logged into (joe@) the computer, which happens to have been cleverly named "joes.computer". joe@joes.computer makes sense, doesn't it. The last joe is telling you the current working directory, /home/joe (it always only shows the last directory name of your CWD.) Not so funny anymore, is it? The command "pwd" will print your working directory to the screen. It will look like this:

[joe@joes.computer joe]$ pwd
/home/joe

[joe@joes.computer joe]$ _

 

O.K. joe, your home directory /home/joe)

[joe@joes.computer joe]$ cd ..

[joe@joes.computer home]$ _

 

"cd .." again would take you up a directory to /

[joe@joes.computer home]$ cd ..

[joe@joes.computer /]$ _

 

Every directory has two special directories in them; "." and ".." ; "." means "this directory" and ".." means "the directory above me" or "my parent directory". The only exception is / has no parent directory there is no /.. to "cd" to.

 

Remember ~ being the same as /home/joe? Well "cd ~" from anywhere will take you straight to /home/joe. Also, "cd /" will take you straight to the top, /. From /, you could type "cd home" and do an "ls" just to see

[joe@joes.computer /]$ cd home

[joe@joes.computer home]$ ls
./
../
jen/
joe/

[joe@joes.computer home]$ _

Directories are shown in blue and have a forward slash after their name. Executable binaries are shown in green, installable rpms are red, text and unknown types are white, pictures are purple, links (like shortcuts) are a light aqua, etc.

 

Now lets cover some basic commands that you would use everyday. In X windows, people often right click to select cut, copy or paste, to make a directory or link (shortcut), or to move or remove (delete) a file. Believe it or not, these tasks go faster using the command line interface (CLI)! Some commands take arguments to understand exactly what you want to do, like "ls -l". The "-l" is the argument and tells "ls" to list the files in the directory using the long, detailed format, showing you the file sizes and attributes. You can check the options or the syntax for a command in the manual for that command by typing "man command" (e.g. "man ls"). For even more detail, you can type "info command".

 

Command Quick Reference

  • cd = change directory
     
  • ls = list directory contents (show files)
     
  • cp = copy
     
  • mv = move/rename
     
  • rm = remove/delete. Use "-R" argument to remove an entire directory and all of it's contents. Use with Caution!
     
  • su = switch user (without an argument means switch to root, the superuser/administrator)
     
  • pwd = print working directory
     
  • mkdir = make a directory
     
  • ln -s = make a soft link (shortcut)

Examples

cp song.mp3 ~ => Copy song to /home/joe

mv ~/song.mp3 ~/newname.mp3 => rename song in /home/joe to newname

cd ~ => change directory to /home/joe

mkdir downloads => make a new directory "downloads" within the current working directory.

 

ln -s is awesome. It works like a shortcut in windows. Let me explain how I use this to my major advantage.

 

I use some applications (well, games :oops: ) in both windows and linux, so instead of having the files installed twice, I can link to the windows files.

ln -s /mnt/windows/KlingonWOF/gamedata /usr/games/KlingonWOF/gamedata

 

Now everytime my linux looks at /usr/games/KlingonWOF/gamedata it's actually following the link and looking at the file on the windows partition! This works for entire directories, too. You can "ln -s /path/to/directory ~", and from then on "cd ~/directory" will take you into it!

 

Permissions

 

Earlier I mentioned that users don't have access to other's files. This is because each file, as well as each directory, has permissions. There are read, write, and execute perms. You need write perms on a something before you can modify or delete it, and you need execute perms before you can execute a binary (executable). Let's do an "ls -l" and examine the output.

[joe@joes.computer joe]$ ls -l

Permissions  Owner Group  Size

drwxr-----  1 joe  users    0      Jan 1 1970 ./

drwxr-----  1 joe  users    0      Jan 1 1970 ../

drwxr-----  1 joe  users    0      Jan 1 1970 Desktop/

drwxr-----  1 joe  users    0      Jan 1 1970 Documents/

-rwxr-----  1 joe  users    32     Jan 1 1970 music -> /mnt/windows/My Documents/My Music/

-rw-r-----  1 joe  users  1,000    Jan 1 1970 picture.jpg

-rw-r-----  1 joe  users   64      Jan 1 1970 readme.text

-rwxrwxrwx  1 joe  users   64      Jan 1 1970 readme_from_windows.txt

-rw-r-----  1 joe  users 2,345,255 Jan 1 1970 zinf-1.0.i586.mdk.rpm

 

Because permissions for files and directories requires a lengthy explaination, I'm going to short-hand this a little to give you more of a general knowledge rather than an "administrative guru" status on the topic. It isn't a tough subject, there's just a lot to talk about.

 

What we are seeing in the first column: Directories have a "d" as the very first character, followed by the permissions for the owner of the file or directory, permissions for users in the specified group, then the permissions for everyone else.

According to the output, Joe has read and write access to every file and directory, and execute rights on every directory (and the link "music"). Anybody who belongs to the group "users" can read every file and directory, and anyone else has no rights. The one exception is "readme_from_windows.txt" - a file copied over from a windows partition - which gives everyone full rights to the file. This can be dangerous!! Insane punk-clown hackers can do what they please with this file!

 

To change the permissions of a file (for instance - Joe sees his "readme_from_windows.txt" file permissions and has a conniption fit so decides to do something about it) you use the command "chmod". WTH that stands for beats the fudge outta me, but I sure know how to use it. Each of the permissions, read, write, and execute can be represented by a number to "simplifiy" things. :roll:

  • read = 4
     
  • write = 2
     
  • exectute = 1

In other words, right now "readme_from_windows.txt" has permissions of 777 (4+2+1 ; 4+2+1 ; 4+2+1). You can give everyone full permissions to a file with the command "chmod 777 filename"; however, most of the time you would rather give more restricted access. So here's how.

 

Let's say Joe decides he wants everyone who belongs to the "users" group to read "readme_from_windows.txt", (but not write to it, i.e. change or delete it). He might issue the command "chmod 640 readme_from_windows.txt". Now user joe has read(4) + write(2) = 6 ; group users has read(4) = 4 ; and others have no rights = 0 ; therefore, 640. See? This will get clearer as time goes by.

 

Conclusion

Now you know how to move around at the command prompt, view the files in the directories and their permissions, copy and move files, and read the manuals for new commands as you hear about them.

Edited by static
Link to comment
Share on other sites

 Share

×
×
  • Create New...