In this lesson,
we're going to discuss the Linux Directory Structure.
Now, you need to become familiar
with the structure and design
of a typical Linux file system from a user's perspective.
That way, you'll be able to easily navigate the file system
and find the data and system files
that you're looking for at any given time.
Linux contains regular files that includes text files,
executable files or programs,
input for programs and output from programs.
Besides all of these, the Linux file system
also consists of other types of files,
including directories, special files, links,
domain sockets and named pipes.
Directories refer to a container for other files.
Special files include system files
that are stored in the /dev directory.
Links make a file accessible
in multiple parts of the system's file tree.
Domain sockets provide inter-process networking
that's protected by the file system's access controls,
whereas name pipes are going to enable processes
to communicate with each other
without using network sockets.
If you want to look at all these
and learn more about the different types of files,
you can type file [Options] and the file name
to determine the type of file you're looking at.
Now, every file has a name to describe what they contain
and how they relate to other files.
By using the right combination of characters in a file name,
you can ensure that the files are unique
and easy to recognize.
Although file names may contain a space,
convention on Linux systems dictates
that the words in a file name
are more frequently going to be demarcated
by using a hyphen or an underscore instead of a space,
as this makes it easier to manage on the command-line.
For example, if I have the file auditfile.txt
or audit_file.txt,
both of those would be much more acceptable
than audit file.txt.
For somebody who's new
to the command-line environment in Linux,
you may find yourself wondering,
why are there so many unusual directories
and what are they all there for,
and why are these things all organized
the way that they are?
Well, directories should be organized
and which files should be stored where,
is going to be really important.
So distributions have created a single reference point
from which to work.
The file system hierarchy standard or FHS,
is a collaborative document that specifies
a set of guidelines for the names of files and directories
and their locations on a given Linux system.
As defined by the FHS, the top-most directory
in a Linux file system
is always going to be the root directory.
This is indicated by a single forward slash.
Below the root directory are various sub directories
that are standardized as part of the FHS.
There are a lot of directories
inside of a Linux file system
and so we're just going to cover some of them here.
/bin is going to store essential command-line utilities
and binary files.
For example, /bin/ls is the binary for the ls command,
/boot is going to store all the files
that are necessary to boot up the Linux operating system.
/dev is going to store hardware and software device drivers.
This directory maintains a file system entry
that's going to represent the devices
that are connected to the system,
for example, your hard drive or a printer.
If I'm looking at your first hard drive, for example,
that's going to be /dev/sda1
and that indicates the first partition
on the first hard disc.
If you look at /etc,
this directory is going to store basic configuration files,
for example, you might have the /etc/samba/smb/conf file.
This stores your Samba configuration data
and allows you to use SMB on the network.
If you look at the /home directory,
this stores your users' home directories,
including all of their personal files.
The /lib directory stores shared program libraries
that are required by the kernel,
command-line utilities and other binaries.
The /media directory is going to store mount points
for removable media, such as CD ROMs,
DVDs or even old school floppy discs.
/mnt stands for mount.
This refers to the mount point
for temporary mounting file systems,
for example, if I put a USB thumb drive into my system,
I might mount it under the /mnt directory.
/opt stores optional files for large software packages
and this is our optional directory.
These packages normally create a sub directory
bearing their name underneath the /opt directory
and then place their files in that sub directory.
For example, if I installed
the Nessus vulnerability scanner,
I might see one that's called /opt/nessus
and it contains all the files for this scanning program.
Another directory we have is /proc,
which is the process directory.
This directory refers to a virtual file system
that represents continuously updated kernel information
to the user in a typical file format.
For example, the /proc/mounts file
is going to have all of our mounted information inside of it.
/root is going to refer to the home directory of the root user.
/sbin stores binaries that are used
for completing the booting process
and other ones that are going to be used by the root user,
for example, /sbin/ifconfig
is a file for the binary of the ifconfig command
that's used to manage the network interfaces
on a given system.
Another directory we have is /sys
and the sys is our system directory.
It's going to be a virtual file system
that stores information about devices,
for example, /sys/block includes links to devices
that are stored in various sub directories
under the /sys/devices/location directories.
And this presents as a hierarchy of devices
inside of our kernel.
The /temp directory is going to store temporary files
that will be lost on system shutdown.
The /usr directory is our user directory.
It's a read-only directory
that stores small programs and files
that's accessible to all users.
Finally, we have the /var directory
which stores variable files
or files that are expected to constantly change
as the system is running.
This includes things like log files, printer spools
and some networking service configuration files.
Now, I mentioned the /usr directory.
This directory actually has
some important sub directories too.
The /usr/bin directory includes executable programs
that can be executed by all users.
The /usr/local directory includes custom-built applications
that are stored here by default.
/usr/lib includes object libraries and internal binaries
that are needed for executable programs.
/usr/lib64 has the same purpose as /usr/lib,
except it's meant for 64 bit systems
instead of older 32 bit systems.
The /usr/share is going to include read-only architecture
independent files that can be shared
among different architectures of an operating system,
for example, are we using an X86-based system
or an X64 based system or an ARM-based system.
All those things will be read-only
and independent architecture files
in this /usr/share directory.
Now, another important directory is the home directory
and I briefly mentioned it earlier in this lesson.
The home directory contains a user's personal files
or files that are otherwise specific to that user.
The home directory is where you're placed
when you log into the system initially.
In Linux, by default, every single user
except the root user, is assigned a sub directory
under the /home.
So if my username was Jason, I would be /home/jason.
A user can create sub directories and files
within their directory then,
so I can have /home/jason/movies
or /home/jason/downloads,
/home/jason/documents.
You get the idea here.
Now, for the root user,
they have a special home directory
that's outside of the home directory.
It's actually going to be at /root.
Now, if you're going to be talking about a directory,
you need to know where you are.
What is your current working directory?
As I said, when you first log in the system,
you're usually going to be located in your home directory.
So for me, that would be /home/jason.
Now, this is known as your current working directory.
This is the location on the system
that you're accessing at any point in time right now.
Now, there's two ways to talk about
the current working directory.
You can call it by the actual full name,
like /home/jason for my home directory
or you can also refer to it
with a shorthand of a single period.
So if I talk about just the dot directory,
that is the current directory I'm in.
Now, if you go one level
above that current working directory,
you have the parent directory.
We call this the dot dot.
All directories, except the root directory,
have a parent directory.
Now, you can use this double period notation
to switch to your parent directory at any time.
So if I was in /home/jason
and I wanted to go to the /home directory.
I could type in cd .. and hit ENTER.
Now, I would be in the /home directory
and if I wanted to talk about my current working directory,
that would actually also be known as the dot.
So my dot has now gone to being /home,
because that's where I'm sitting right now.
Now, when we talk about a path in a file system,
we're talking about a specific location.
Now, we always read this
as starting out with the slash, which is the root directory.
That is the directory at the top of the directory tree
and then as we start talking about different directories,
we start adding the words.
So I mentioned we had the slash, that's the root,
then we had the word home, that's the home directory
which is the first directory under the root.
If I wanted to talk about home/jason,
that would actually be two directories down.
We have the root directory, we have the home directory
and then the Jason folder underneath that home directory.
This is how we start talking about a path,
so that full path becomes /home/jason.
You can refer to a particular file
by providing the path to that directory
that contains the file.
So if I want to refer to a file called my work
and my documents folder,
I will give you the full path,
/home/jason/documents/mywork.
Notice I'm using forward slashes here.
In Windows, we tend to use back slashes
and this is a key difference between Linux and Windows.
Now, when we talk about the slash
that precedes the home, that is that root directory.
So if I have just a single forward slash,
that is the root and everything else works its way
from the root down to the particular file I'm working at.
Now, there's also two ways to name these paths.
You can use an absolute path,
which is what I was just using
or you can use a relative path.
When I talk about an absolute path,
this refers to the specific location
irrespective of any current working directory
or combined paths.
These paths are usually written
with reference to the root directory.
So we're going to start with that forward slash
and give all of the directories
all the way down to that file.
Now, anytime you have a path that doesn't begin
with a forward slash,
that is known as a relative path,
and a relative path is the path relative
to your current working directory.
So instead of having the full absolute path,
I wouldn't need that,
I can just put the path to where I am to where I want to go.
And we can do this by using the dot,
which is the current working directory or the dot dot
which goes up to the parent.
So let's say I wanted to go
and access the absolute location
and I'm sitting inside of /home/jason
and I want to get to the /usr/bin directory.
I can do that by using an absolute path
and typing in cd /usr/bin.
Now, if I want to do that same thing using a relative path,
I could do it by doing cd ../../usr/bin.
And that would say to go up to directories
from the Jason directory,
to the home directory, to the root directory
and then go down into user and into bin.
This is how you can use absolute or relative paths.
Now, after having a good sense
of what a Linux directory structure looks like
from this overview I provided you,
one of the common things you'll need to know,
is how these commands are used to navigate
through the file structure.
As we just showed, you can use the cd command
to go through a directory structure
using either absolute or relative pass
to change your current working directory.
If you want to list out all the files and directories
in that current working directory
or some relative or absolute path you specify,
you can use the ls command, which lists all of those things.
In a long form listing, you can use -l
and this will also show you the first bit
that indicates the type of file.
Is it a file or is it a directory, for example?
Also, if you're ever lost
and you want to know what your current working directory is,
you can use the command pwd.
If you type in pwd,
it's going to display your current working directory
to the screen and I remember this by saying,
my present working directory for pwd.
All these are things you need to be comfortable with
inside the command-line environment, using cd,
using ls and using pwd.