In my second tutorial about Linux, I’m going to look at files and folders and how they work on Linux – because it’s very different compared to the Windows way of C:, D:, and E: etc.
Basically, in Linux (and other Unix-based systems, but I’ll keep it simple for now), there aren’t different drives. In Linux, everything you can access stems from the top folder in the stack. It’s called the root folder and it can be accessed using a single forward slash – /.
Everything else is a folder (or directory, in Unix speak) underneath this forward slash. Your home folder (the equivalent of My Documents) will be at /home/yourname. That means:
“From the root folder, go into the home directory and then go into the yourname directory.”
Also, remember that every folder and file is case-sensitive, so myfile is not the same as MYFILE or mYFilE. Just bear that in mind.
Please support this work!
There used to be advertising here, but I no longer feel sure that advertising delivers the best experience and truly reflects the values of this site.
Keeping things running, however, is not without financial cost. If you would like to support the time and effort I have put into my tutorials and writing, please
consider making a donation.
Mounting Media
So that’s fine for all the stuff on your computer, but what happens if I insert a CD or a USB stick or something? How do I access that? The answer is that the contents of the CD, for example, are mounted in a folder. That basically means that the folder becomes a container for everything on the CD.
Most of the time, when you insert some media, your distro does the work of automatically mounting the contents into a folder, so you don’t see this. To find mounted media, you need to look into a folder called /media (open it up in a file manager), or sometimes /mnt. There should be a list of folders corresponding to the media you have inserted (can’t see anything? Insert something).
Having said that, you aren’t restricted to mounting stuff in these folders, it’s just a convention; if you want you could mount something in your home directory, but I’ll leave that for another tutorial.
Symlinks
OK, got that? Because there are more concepts coming up now. Now in the Linux filesystem, there are also things called symlinks (in fact, there are also hard links, pipes, block devices, character devices and more, but I said I’m keeping it simple). Symlink means ‘symbolic link’ and is a bit like a shortcut, but a lot more powerful. A symlink doesn’t just link to a different file, when you perform operations on it, it acts exactly as if it is that file. The advantages of this are a bit geeky, but believe me it comes in handy a lot of times.
An example of a symlink in action is in my home folder. I do a lot fo web development, so I need quick access to the files on my web server. Rather than me finding the web server documents folder (/var/www/html) every time I want to save or open, I create a symlink in my home directory. That way, not only do I get there quickly, but I can work with the web documents just as if they were actually inside my home directory (all the paths say /home/peter/htdocs/mylatestproject, rather than /var/www/html/mylatestproject).
Permissions
I won’t go into detail here, there’s enough stuff on permissions to make a tutorial in itself, but basically on your Linux filesystem, different people ‘own’ different folders and files and have the right to grant or deny other people permission to read and write to the files they own.
The super-administrator, root (not to be confused with the root filesystem / or root’s home folder /root) has access to every file regardless of its owner or permissions. This is why you should never run as root – it’s so easy to make a mistake that would bring down your whole system.
A lot of the files in Linux are owned, controlled and locked down by root. This includes important system files, folders and programs etc. You won’t be able to edit or delete these files yourself, without first logging on as root (be very careful).
For the files that you own (generally, stuff in your home folder), you can control permissions (you can prevent other users, but not root, from reading or writing to your data). By default, most distributions set your home folder to be readable by others, but not writable by others. It’s out of the scope of this tutorial to go into changing it, but if you’re interested, investigate the command chmod (or look at your file manager, it should be able to change permissions graphically).
Common Folders
I hope you have a fairly good understanding now of how the file system basically works, so now I’m going to iterate through some of the common folders you find on a Linux system, and what they’re for.
Name | Purpose |
---|---|
/ | The root folder, where everything starts, and everything is contained in this folder (or under subfolders of this folder). |
/bin | Contains system-critical programs (binaries) |
/boot | Contains files needed for booting (be careful!) |
/dev | Contains files which can be used to directly access your hardware (files to manipulate all your hard drives etc) |
/etc | Contains mainly configuration files for various programs/system stuff. |
/home | Contains a folder for each user (except root) where they can store their personal files. |
/lib | A folder for system-critical libraries. |
/root | The home folder for root – the super-administrator (generally will be locked to normal users so you can’t take a peek). |
/media | Contains folders for the media attached to your system (CDs, DVDs and USB sticks etc) |
/opt | Optional software – like desktop environments etc. |
/var | Application data, including things like your MySQL databases, for example, as well as cache files, Unix mail storage and any other application data that isnβt really specific to one user |
/proc | A system folder, it’s a bit scary so I won’t go into it! |
/sbin | Contains essential programs only designed to be run by root. |
/tmp | Temporary files, deleted every restart. |
/usr | Stands for Unix System Resources (not user as is commonly said). Contains lots of stuff, so I’ll look into it below. |
/usr/bin | Contains virtually every single program you use. |
/usr/lib | Contains virtually every system library that programs require to run. |
/usr/local | Contains programs that you have manually compiled rather than installed from a package. |
/usr/sbin | Contains virtually every program designed to be run by the super-administrator, root. |
/usr/X11R6 | Houses files which power the X Window System (X version 11 release 6), which is the basis of almost every graphical environment. |
. | Dot (that’s a full stop) means the current directory. Mainly used to start programs in the current directory (like ./configure for example). |
.. | Two full stops means the directory one up from the current directory. So if you’re in /home/peter going to ../ would take you to /home. |
~ | The tilde symbol means your home directory – so that will be /home/yourusername. |
This is just a brief look at the file system on Linux, but I hope it’s helped you understand a bit more how Linux works. More tutorials to come.
Version 1.0 — initial version
Version 1.1 — contains ., .. and ~ in Common Folders and finishes the section about case-sensitivity which I *cough* didn’t finish.
Version 1.2 — 2010-07-25 — added /var.
Nice intro!
Thanks, Marc.
I’ve done a little update now with ., .. and ~ under Common Folders and I finished a little bit about case-sensitivity.
As a Linux newbie, I appreciate having a concise listing of the common directories. Thanks for sharing your knowledge. That is one of the best characteristics of the open source community.
Thanks Man. Pretty informative.
I have saved a file (from windows) in the Linux partition (E:\), in E:\file.txt.
How to I access it from linux?
Harris,
This is a bit more complicated unfortunately than just pointing at the E drive.
There are three steps you need to follow:
Without further information I can’t be of more specific help, however. If you do want more detailed help, drop me an email so I can follow up outside this comment thread.
Thanks for the intro. I have been a long time Linux user but I must admit that I did not know all the purposes of the various root level directories.
Thanks
/usr actually DOES stand for “user”, not “Unix System Resources”, but it is a very misleading name. Ages ago, this directory used to be the equivalent of /home, but its role has since changed.
See the debate on Wikipedia: http://en.wikipedia.org/wiki/Talk:Filesystem_Hierarchy_Standard#.2Fusr_as_a_backronym
Very good information for linux newbie. They will understand the file system and permissions etc. Good Work. Keep it up.
Good explanation.
Linux filesystem just makes more sense. I’m not an expert but I know from experience that windows deteriorates in speed after the first six months and there are loose files everywhere. But maybe that’s just me
Hey Peter – thank you much for the linux file system layout. I’m a linux newbie and I’m spinning my head trying to figure everything out. What can I say – I’m a recovering windows addict.
Thanks again!
Ryan
What about /var and /opt
Yordann Georgiev,
/opt is listed in the guide — “Optional software β like desktop environments etc.”
/var is for “files to which the system writes data during the course of its operation”. (http://www.linfo.org/var.html)
You’ll often find /var used for application data, including things like your MySQL databases, for example, as well as cache files, Unix mail storage and any other application data that isn’t really specific to one user.
I have updated the guide, thanks for pointing out its absence.
Thank you Peter, excellent articles for linux beginners, i am in web development using php , and was not sure where to find the “web directory” on linux its “/var/www/html”
How the changes in the symlinks file gets reflected/saved to main file? or its the same file with symlinks as just the reference like shortcut?
Peter the Rock… thanks for teaching this old man…I’m putting to the New!
Amit Patekar,
A symlink acts as a redirect to its target. If you read the symlink (for example, cat), the original file’s contents are read. If you write to the symlink, the original file is updated. The symlink is completely transparent to the applications which make use of it (contrary to how a shortcut file is on Windows).
The exceptions would be removing and renaming/moving the symlink — in this case it only affects the symlink and not the original file!
Just like Tim Marchand said, this is an excellent and very concise explanation, perfect as quick reference for those like me, who are used to windows but want to go (or are getting used to) Linux. Thanks!
If you’re computer literate but a linux-newbie like me, take my advice: while installing linux, use 3 partitions (swap, home and everything else). For filesystem, choose ext4. And if you’re gonna do dual booting, make a partition for windows, install windows and only then install linux (you can make the partitions while installing). A nice distro for starters is Ubuntu.
Finally, I know what /var, /usr, /bin etc. means. I’ve used Linux for ages but could never be arsed to look ’em up. Glad I stumbled along your blog!
Very nice! But that doesn’t answer the question: “Is a folder exactly a file?”
regards
Chris
Very nice, the list is very helpfull as I am a student just learning about Linux. So, what types of programs use these directories?
Thanks for the help! I’m a newbie, pressed by work to learn Moodle. I’m installing right now, and got stuck:
“you can either place the whole folder in your web server documents directory” (excerpt-http://docs.moodle.org/21/en/Installing_Moodle)
As I said, I’m a newbie. I had to look up-web server documents directory. Give me a path, I’m fine, but the tutorial I was following (from Moodle) didn’t explain that very well.
Thanks again!
Mate, solid guide! Exactly what I’d been looking for. Clear, compact description of the core directories and their purposes. Cheers.
Nice description – thanks a lot!
The more I dig into Linux, the more I understand how better it is comparing to Windows π
However, GUI in Windows is still better in general π
Thanks a lot. Just got my hands dirty into linux. Being windows guy having real difficulty with non gui stuff.The information you provided is very informative for beginners like me. Think found my answer here /usr/local maybe ideal place for any development work