Книга: Linux Command Line: An Admin Beginners Guide
Назад: What is the Linux Shell?
Дальше: The Command Line - Basic Commands
CHAPTER FOUR: Linux Basic File Directory
Linux is modeled on the UNIX operating system. As such, the filesystem and commands are similar to UNIX. If you are already familiar with systems like Apple OS, the file system may be familiar to you.
It's also recommended that you use a bootable Linux USB stick for learning Linux. Changes are not stored on a bootable Linux USB stick so you can easily restart if you have a problem or make a mistake.
What is the Linux Filesystem?
The Linux filesystem is a collection of directories, and it is composed of various files and folders that keep it organized. You may be familiar with this concept if you've ever worked in an office. A "file system," whether we're talking about computers or not, is a system of organizing information.
In Linux, every piece of information is represented by a file. Everything looks like a file.
It's all a file that a person can read, access, delete, and modify.
These files are stored in folders called directories, which are then stored in other directory folders, and so on. It's like having a folder full of files, stuffed into another larger folder, stuffed into another even larger folder and so on.
We will show a file hierarchy of folders as
/var/log/mail
Where mail is a subfolder of log which is a subfolder of var which is a subfolder of root.
Visualizing the Linux Filesystem
In the Linux Command Line section, we'll learn about the tree command. For now, we'll just show the output of the tree command.
The root directory has many sub-directories. Without these files and directories, certain programs and bits of hardware and software would not function at all on the computer. Because everything is a file in Linux, it's not uncommon for crucial parts of a program to be stored in separate folders.
It can also be difficult to find a file that you've downloaded if you don't know where to look.
Let's begin by looking at the Linux file system, what certain folder names represent, and where a downloaded file is likely to end up.
/
root (top level)
/bin
binaries (applications)
/boot
boot (startup)
/dev
device
/etc
system configuration
/home
home directory
/lib
libraries
/media
removable mount drives
/mnt
permanent mount drives
/opt
optional software
/proc
process information
/root
root home directory
/sbin
secure binaries
/sys
system status
/tmp
temporary directory
/usr
user data
/var
variable files (log)
Picture a House
Think of the Linux Filesystem as a house. Different rooms in the house have different functions. As we wander through the filesystem below, we'll also describe a house to make it easier to remember its function.
root
The / represents the "root" of the filesystem.
The forward slash is a symbol representing the "root" or "main" directory. The main directory should not contain any user files for optimum performance and to prevent clutter. But some versions of Linux, such as Debian, allow a root user to customize any parts of the program as he or she sees fit.
Ubuntu does not allow any single user automatic access to the / folder. Instead, it requires that the user enter special commands to access its contents. This will be covered in more detail later in this book, but that command is sudo, or "super user do." Even if you have administrator privileges, if you are using an Ubuntu-based system, you will have to enter sudo to execute commands that are only supposed to be carried out by the root user.
The main part of the house is the Living Room. All the other rooms branch off of the Living Room. The Linux Filesystem "Living Room" is the root directory. If you were to search the filesystem hierarchy, you wouldn't find a file named "root". The root folder is at the top in the picture above and is designated / . Like root, the homeowner has to open the front door and invite the visitor in.
bin
The letters bin stand for "binary." This folder contains the built-in executable programs that are normally executed.
The absolute path of this subdirectory is simply, /bin. As mentioned in the last section, the /, or forward slash, represents the root directory, while bin is a subdirectory inside of, or below, the root directory.
In our Linux House, this is the kitchen where the awesome food dishes are cooked up. These /bin programs are the ingredients that we can mix together to exciting dishes. We'll learn more about these built-in programs (ingredients) in the Linux Command Line chapters.
boot
This folder also contains boot-related files and it's best that the end-user not tamper with the contents of this folder unless that user knows what he or she is doing. In Linux, it is possible to put the operating system in a state where it will not function properly. You typically won't spend much time in this directory.
In our Linux House, this is the sidewalk leading up to the house. The boot directory leads you into the house just like the boot directory has the instructions for starting up and getting you into Linux.
dev
Think of the abbreviation dev as standing for "device files." This folder contains data for all of the essential devices installed on your system. Unlike Windows, Linux device files are accessible by the root user, or admin, depending on what permissions the user has. In some cases, the admin can even access the files controlling the hard drive and other essential equipment.
The devices need kernel drivers to connect to the Linux kernel. Some Linux kernel drivers also expose their interfaces to users through this directory. An example is the older UARTs that print out the logs in Raspberry Pi and other mini-computers.
In our Linux House, these are the appliances of the house. The stove, the refrigerator, the washing machine, and the sinks and toilets are all available for the family to use. The Linux kernel drivers are the electrical wiring and plumbing connecting the appliances to the outside world. The Linux drivers are hidden from view just like the wiring and plumbing are in the walls of the house. Some drivers expose their interfaces through the dev directory just like a sink exposes the water to the family.
etc
The etc folder contains configuration data for individual programs that are used system-wide. This is not the same as the individual user's etc folder. If the user has the proper permissions, they can hand-edit the data in these folders using a text editor.
home
Individual user folders are found in the home folder. On the Ubuntu Desktop, the Download folder and Documents folder can be found under the home directory. On a command line, it might look something like, /home/username. For example, the command line would look like /home/kevin if the user's name happens to be Kevin.
The home directory has a shortcut symbol which is the ~. We'll discuss it more in the Linux Command Line sections but kevin can quickly jump to the home directory by typing either cd ~ or cd /home/kevin.
In our Linux House, these are the bedrooms of the house. Kevin has his own bedroom and Suzy has her own bedroom. They keep their private possessions in their separate bedrooms. The bedroom is similar to the home directory. Each user's personal setup is stored in the home directory. Each user can create and add files to their home directory as they see fit just like each person can decorate their bedroom.
lib
The Linux system library, which may look like /lib in the terminal as the direct path, contains libraries shared by application programs that are necessary for them to run. The libraries are needed for the application programs to run properly.
In our Linux House, this is the pantry with the spices that are used for cooking. The spices aren't used for every meal but they're available when they are needed. The main ingredients were the programs in the /bin directory and to taste just right, the spices in the pantry are needed.
media
The /media folder, or directory, contains subdirectories and subfolders where removable devices that store media can be mounted. Without this directory, devices such as USB drives and DVD ROMS might not work properly.
In our Linux House, this is the driveway to the house. Visitors to the house (USB) must park in the driveway before making their way to the house.
How to Use:
When you insert a USB drive or new removable device with media files, look for it to be added to this folder.
mnt
The /mnt is referred to as the "mount" command. This command enables you to mount and unmount files. The /mnt directory provides a place to mount more permanent files such as the CD or DVD drive.
In our Linux House, this is the garage attached to the house. Permanent residents of the house park in the garage instead of in the driveway.
opt
This directory holds software applications physically configured by the user or administrator. You may find files for your desktop environment and other software non-essential to the system here.
In our Linux House, this is the back patio. It provides some needed recreational space but it isn't an important part of the house.
proc
The /proc directory holds many details about your Linux system. The files stored here are referred to as "virtual" files because they don't actually exist on the disk. If you have the proper permissions, you can still explore this directory to learn more about the Linux system and programs on the computer you're running. It may help to picture the files in the proc directory as nothing more than ghosts of recently accessed files.
In our Linux House, this includes the thermostat, the water meter, and the electrical usage meter. If you want to see what is going on in your house, you'd check the thermostat to see what the temperature is, the water meter to see how much water you are using, and the electrical meter to see how much electricity you are currently using.
root
This is the home directory for the root user. Root is considered the highest level of anything, so the root user is the highest level user on the computer.
In our Linux House, this is the master bedroom. When you're in the living room, you're saying this is Jesse's house but when you're in Jesse's bedroom, that is where his personal things are kept.
sbin
The /sbin directory holds system commands that affect the entire system, and only the root user is allowed to run them.
In our Linux House, this is the private library or home office that includes the plans for the house, the house insurance, and the builder's warranty on the house. The root occupant is usually the only one that uses this room.
sys
The /sys directory stores temporary files with device information in a virtual file system. This is very similar to the /proc directory and is basically a newer and better-maintained version of the /proc directory. The /sys directory typically has more ways to control device drivers while the /proc was typically for reading.
In our Linux House, this includes the thermostat and meters as well as the light switches, faucets and circuit breakers. We can turn the water and electricity off and on as desired.
tmp
This directory stores temporary files from applications and users. Do not store anything important in the /tmp folder, as the contents may be regularly deleted when a user logs off or the system is shut down.
In our Linux House, this is the trash can.
usr
Although it looks like an abbreviation for "user," it's not. It stands for "Universal System Resources" or, on Unix, "Unix System Resources." Regardless, the /usr directory still stores much of the information used by individual user accounts on a Linux system, including binaries, header files, and documentation for the binaries.
In our Linux House, this is the drawer that has all the user manuals and warranty information for the appliances.
var
The /var folder contains files that the system writes data to while it's operating. var is a high-level directory that is not shared over a network or with other computers, and it contains some user information that is not read-only. This is the folder where the log files that report on system problems or health are stored.
In our Linux House, this is the laundry room. When you walk into the laundry room, you can determine from the clothes that Kevin was playing in the mud at the playground and Suzy had an ice cream cone that melted before she could finish it.
Common Shortcut Paths
. A single period means the current directory
.. Two periods means the parent directory so the directory above the current directory
~ The user's home directory. Each user has their own home directory (private bedroom).
/ The root directory.
Relative and Absolute Paths
The relative path of a file is the path of that file from the current working directory. For example, if you were currently working in the home file and you stored something that keeps track of user passwords, you would be able to access that file simply by typing the filename, and maybe sudo if your account does not have administrator permission.
In our house example, we can describe a relative path as
On the other hand, if you are working in another folder or directory, let's say, the root directory, and you typed in just the name of the file, you might come up with an error. Remember that, in the previous scenario, you stored the file in the home directory and not the root directory. Merely typing in the name won't work--you will either have to change directories and then type in the relative path.
Another option is to type in the absolute path which is the entire path to that file starting at the root directory. Let's assume that you already know the path, in this case, let's say it's something like, /home/usr/passwords. Typing this whole path into the terminal would be one way to get to the file you need.
Quiz Time:
Take out your notebook and answer the following questions. Feel free to look at an earlier part of the chapter if you think you need a reminder.
What is housed inside the Linux file system?
Why is it important to know what kind of files each folder is designated to handle?
Answers:
The Linux file system houses files and directories that are vital to the operation of the system in addition to files that are important to the user.
Linux has designations for what certain programs do, based on where they are stored.
Where you store a file in Linux is important, but it's also important to understand syntax. You might have noticed that the cd and ls commands follow a certain format. What did they have in common? For one thing, they were all lowercase. Capitalization or lack thereof is important in Linux, which is a concept we will go over in the next section.
Capitalization and Syntax in Linux
Capitalization does not necessarily matter in Windows, but in Linux, lowercase and capital letters are treated as different letters. For example, a folder called "home" would not be the same as "Home." It is also true of your username login.
Did you notice that some of the subheadings in this book are not capitalized? It's not an oversight by the editor - in Linux, capitalization, or lack thereof, matters, and those subheadings were written to remind you how to properly write the commands.
Linux is very sensitive when it comes to capitalization and the correct usage of syntax. You may want to double-check the commands you type to make sure that you've typed in the correct syntax. A missing period, misplaced colon, or any other identifying sign could result in an error or could lead you to a new directory entirely.
Tips for Completing Exercises in Future Chapters:
To avoid any issues, it's highly recommended that you look at all examples very carefully and make sure you understand what is being presented before practicing on your own. If there is ever any doubt and you have internet access, you should consult the internet, especially the Linux forums and tutorial websites, for more information. However, it is best to search for the answer yourself if you want to strengthen your programming abilities and understanding of the Linux system.
Let's say you type in a directory path and get an error. Look at the picture and provided examples carefully--did the instructions tell you to type in a backward slash or a forward slash? Did you miss any steps, such as first checking if that directory existed?
If you don't understand the information presented, re-read it while carefully taking notes. Ask yourself, "what exactly do I not understand?" If it's something simple, for example, you aren't sure whether the command is asking for a capital "I" or a lowercase "l," try applying some logic. What does the command stand for? If it appears to stand for something that starts with an "l," such as list, it's reasonable to guess that it's a lowercase "l." 
You should also keep in mind that, with a few exceptions, most commands in Linux are in lowercase. If there's a time where they are not in lowercase, it is one of those few exceptions and will be clear from the context. If there is ever any doubt go with lowercase.
Although this book is designed to be beginner-friendly, it may appear confusing at first while you are familiarizing yourself with the Linux terminal and trying to learn the commands. Don't get frustrated if you type a command and receive an error message. You may have mistyped or entered the wrong letter or symbol, which is easy to do if you are just starting.
There's also a good chance that you're simply in the wrong directory or you may be attempting to perform operations that you don't have permission to perform, depending on what version of Linux you're using. These errors appear more often if you are searching for online help without a full understanding of your system. This should be less of a problem for you if you're using the latest version of Ubuntu Linux because the examples in this book assume you are using Ubuntu Linux. 
If you still want to follow along using another distribution of Linux out of convenience for your situation, feel free to do so. You can try learning Kali Linux or Fedora for server maintenance and you don't want to go through the extra hassle of learning another version of Linux, for example.
You should be able to pick up the syntax of your distro of choice fairly easily - just don't get discouraged if the commands don't work exactly the same in different versions of Linux. The syntax may differ but the basic commands and functions are the same; you may just have to learn slight variations in syntax to get them to work for you.
If you would like more information about the Linux file hierarchy, there is a lot of useful information about it online. Feel free to do a Google search if you are interested.
Chapter Summary
Назад: What is the Linux Shell?
Дальше: The Command Line - Basic Commands