- Navigation
- File System Organization
- A Few Shortcuts
- Important facts about file names
- Linux Directory Structure Explained for Beginners
- Linux directory structure
- / – The root directory
- /bin – Binaries
- /dev – Device files
- /etc – Configuration files
- /usr – User binaries and program data
- /home – User personal data
- /lib – Shared libraries
- /sbin – System binaries
- /tmp – Temporary files
- /var – Variable data files
- /boot – Boot files
- /proc – Process and kernel files
- /opt – Optional software
- /root – The home directory of the root
- /media – Mount point for removable media
- /mnt – Mount directory
- /srv – Service data
Navigation
In this lesson, we will introduce our first three commands: pwd (print working directory), cd (change directory), and ls (list files and directories).
Those new to the command line will need to pay close attention to this lesson since the concepts will take some getting used to.
File System Organization
Like Windows, the files on a Linux system are arranged in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (called folders in other systems), which may contain files and subdirectories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.
Most graphical environments include a file manager program used to view and manipulate the contents of the file system. Often we will see the file system represented like this:
One important difference between Windows and Unix-like operating systems such as Linux is that Linux does not employ the concept of drive letters. While Windows drive letters split the file system into a series of different trees (one for each device), Linux always has a single tree. Different storage devices may be different branches of the tree, but there is always just a single tree.
Since the command line interface cannot provide graphic pictures of the file system structure, we must have a different way of representing it. To do this, think of the file system tree as a maze, and that we are standing in it. At any given moment, we are located in a single directory. Inside that directory, we can see its files and the pathway to its parent directory and the pathways to the subdirectories of the directory in which we are standing.
The directory we are standing in is called the working directory. To see the name of the working directory, we use the pwd command.
When we first log on to our Linux system, the working directory is set to our home directory. This is where we put our files. On most systems, the home directory will be called /home/user_name, but it can be anything according to the whims of the system administrator.
To list the files in the working directory, we use the ls command.
We will come back to ls in the next lesson. There are a lot of fun things you can do with it, but we have to talk about pathnames and directories a bit first.
To change the working directory (where we are standing in the maze) we use the cd command. To do this, we type cd followed by the pathname of the desired working directory. A pathname is the route we take along the branches of the tree to get to the directory we want. Pathnames can be specified two different ways; absolute pathnames or relative pathnames. Let’s look with absolute pathnames first.
An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed. For example, there is a directory on your system in which most programs are installed. The pathname of the directory is /usr/bin . This means from the root directory (represented by the leading slash in the pathname) there is a directory called «usr» which contains a directory called «bin».
Let’s try this out:
Now we can see that we have changed the current working directory to /usr/bin and that it is full of files. Notice how the shell prompt has changed? As a convenience, it is usually set up to display the name of the working directory.
Where an absolute pathname starts from the root directory and leads to its destination, a relative pathname starts from the working directory. To do this, it uses a couple of special notations to represent relative positions in the file system tree. These special notations are «.» (dot) and «..» (dot dot).
The «.» notation refers to the working directory itself and the «..» notation refers to the working directory’s parent directory. Here is how it works. Let’s change the working directory to /usr/bin again:
O.K., now let’s say that we wanted to change the working directory to the parent of /usr/bin which is /usr . We could do that two different ways. First, with an absolute pathname:
Or, with a relative pathname:
Two different methods with identical results. Which one should we use? The one that requires the least typing!
Likewise, we can change the working directory from /usr to /usr/bin in two different ways. First using an absolute pathname:
Or, with a relative pathname:
Now, there is something important that we must point out here. In most cases, we can omit the «./». It is implied. Typing:
would do the same thing. In general, if we do not specify a pathname to something, the working directory will be assumed. There is one important exception to this, but we won’t get to that for a while.
A Few Shortcuts
If we type cd followed by nothing, cd will change the working directory to our home directory.
A related shortcut is to type cd
user_name . In this case, cd will change the working directory to the home directory of the specified user.
Typing cd — changes the working directory to the previous one.
Important facts about file names
File names that begin with a period character are hidden. This only means that ls will not list them unless we say ls -a . When your account was created, several hidden files were placed in your home directory to configure things for your account. Later on we will take a closer look at some of these files to see how you can customize our environment. In addition, some applications will place their configuration and settings files in your home directory as hidden files.
File names in Linux, like Unix, are case sensitive. The file names «File1» and «file1» refer to different files.
Linux has no concept of a «file extension» like Windows systems. You may name files any way you like. However, while Linux itself does not care about file extensions, many application programs do.
Though Linux supports long file names which may contain embedded spaces and punctuation characters, limit the punctuation characters to period, dash, and underscore. Most importantly, do not embed spaces in file names. If you want to represent spaces between words in a file name, use underscore characters. You will thank yourself later.
© 2000-2021, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.
Linux® is a registered trademark of Linus Torvalds.
Источник
Linux Directory Structure Explained for Beginners
If you are even faintly acquainted with Linux, you might have heard the terms root, lib, bin etc. These are various directories that you’ll find in all Linux distributions.
In fact, the Linux Foundation maintains a Filesystem Hierarchy Standard (FHS). This FHS defines the directory structure and the content/purpose of the directories in Linux distributions. Thanks to this FHS, you’ll find the same directory structure in (almost) all the Linux distributions.
Let’s see the Linux directory structure in detail.
Linux directory structure
Linux is based on UNIX and hence it borrows its filesystem hierarchy from UNIX. You’ll fine a similar directory structure in UNIX-like operating systems such as BSD and macOS. I’ll be using the term Linux hereafter instead of UNIX though.
/ – The root directory
Everything, all the files and directories, in Linux are located under ‘root’ represented by ‘/’. If you look at the directory structure, you’ll realize that it is similar to a plant’s root.
Linux Directory Structure
Since all other directories or files are descended from root, the absolute path of any file is traversed through root. For example, if you have a file in /home/user/documents, you can guess that the directory structure goes from root->home->user->documents.
The cruel rm -rf / joke
You may have come across some jokes on internet that mentions “rm -rf /” . rm command is used for removing files and directories in Linux.
With rm -rf /, you ask your system to forcefully and recursively delete the contents of the root directory. Since root directory has everything underneath, you end up deleting everything and your Linux system just vanishes (theoretically).
Most Linux distribution won’t run this command unless you provide –no-preserve-root. In any case, don’t be curious to run this command. Curiosity killed the cat, after all.
/bin – Binaries
The ‘/bin’ directly contains the executable files of many basic shell commands like ls, cp, cd etc. Mostly the programs are in binary format here and accessible by all the users in the Linux system.
/dev – Device files
This directory only contains special files, including those relating to the devices. These are virtual files, not physically on the disk.
Some interesting examples of these files are:
- /dev/null: can be sent to destroy any file or string
- /dev/zero: contains an infinite sequence of 0
- /dev/random: contains an infinite sequence of random values
/etc – Configuration files
The /etc directory contains the core configuration files of the system, use primarily by the administrator and services, such as the password file and networking files.
If you need to make changes in system configuration (for example changing the hostname), this is where you’ll find the respective files.
/usr – User binaries and program data
in ‘/usr’ go all the executable files, libraries, source of most of the system programs. For this reason, most of the files contained therein is readonly (for the normal user)
- ‘/usr/bin’ contains basic user commands
- ‘/usr/sbin’ contains additional commands for the administrator
- ‘/usr/lib’ contains the system libraries
- ‘/usr/share’ contains documentation or common to all libraries, for example ‘/usr/share/man’ contains the text of the manpage
/home – User personal data
Home directory contains personal directories for the users. The home directory contains the user data and user-specific configuration files. As a user, you’ll put your personal files, notes, programs etc in your home directory.
When you create a user on your Linux system, it’s a general practice to create a home directory for the user. Suppose your Linux system has two users, Alice and Bob. They’ll have a home directory of their own at locations /home/alice and /home/bob.
Do note that Bob won’t have access to /home/alice and vice versa. That makes sense because only the user should have access to his/her home. You may read about file permissions in Linux to know more on this topic.
/lib – Shared libraries
Libraries are basically codes that can be used by the executable binaries. The /lib directory holds the libraries needed by the binaries in /bin and /sbin directories.
Libraries needed by the binaries in the /usr/bin and /usr/sbin are located in the directory /usr/lib.
/sbin – System binaries
This is similar to the /bin directory. The only difference is that is contains the binaries that can only be run by root or a sudo user. You can think of the ‘s’ in ‘sbin’ as super or sudo.
/tmp – Temporary files
As the name suggests, this directory holds temporary files. Many applications use this directory to store temporary files. Even you can use directory to store temporary files.
But do note that the contains of the /tmp directories are deleted when your system restarts. Some Linux system also delete files old files automatically so don’ store anything important here.
/var – Variable data files
Var, short for variable, is where programs store runtime information like system logging, user tracking, caches, and other files that system programs create and manage.
The files stored here are NOT cleaned automatically and hence it provides a good place for system administrators to look for information about their system behavior. For example, if you want to check the login history in your Linux system, just check the content of the file in /var/log/wtmp.
/boot – Boot files
The ‘/boot’ directory contains the files of the kernel and boot image, in addition to LILO and Grub. It is often advisable that the directory resides in a partition at the beginning of the disc.
/proc – Process and kernel files
The ‘/proc’ directory contains the information about currently running processes and kernel parameters. The content of the proc directory is used by a number of tools to get runtime system information.
For example, if you want to check processor information in Linux, you can simply refer to the file /proc/cpuinfo. You want to check memory usage of your Linux system, just look at the content of /proc/meminfo file.
/opt – Optional software
Traditionally, the /opt directory is used for installing/storing the files of third-party applications that are not available from the distribution’s repository.
The normal practice is to keep the software code in opt and then link the binary file in the /bin directory so that all the users can run it.
/root – The home directory of the root
There is /root directory as well and it works as the home directory of the root user. So instead of /home/root, the home of root is located at /root. Do not confuse it with the root directory (/).
/media – Mount point for removable media
When you connect a removable media such as USB disk, SD card or DVD, a directory is automatically created under the /media directory for them. You can access the content of the removable media from this directory.
/mnt – Mount directory
This is similar to the /media directory but instead of automatically mounting the removable media, mnt is used by system administrators to manually mount a filesystem.
/srv – Service data
The /srv directory contains data for services provided by the system. For example, if you run a HTTP server, it’s a good practice to store the website data in the /srv directory.
I think this much information is enough for you to understand the Linux directory structure and its usage.
In the end, if you want, you can download and save this image for quick reference to the directory structure in Linux systems.
Linux System Directories
Источник