Second, the LINUX directory structure
Before you talk about the Linux directory structure, the first thing you need to know is that the difference between the Linux directory and the Windows directory may not be much different from the general operating experience, but it is completely different from the implementation mechanism.
A difference is reflected in the directory and storage media (disk, memory, DVD, etc.) of the relationship, the previous Windows has been mainly in storage media, mainly in the drive letter (C drive, D disk ...) ) and partition to achieve file management, and then the directory is not so important, the directory is not so significant, except for the system files in any place, any directory is not much of a relationship. So usually after Windows has been in use for a while, the file directories on the disk will appear cluttered (except for a handful of well-organized users). However, Unix/linux is the opposite, UNIX is a directory-based, Linux has inherited this excellent feature. Linux is a tree-shaped directory structure to build the entire system, can be understood as a tree directory is a user-operable system skeleton. Although essentially both the directory structure and the operating system kernel are stored on disk, the Linux disk is logically "hung" on the directory (mounted), each directory can not only use the local disk partition of the file system, but also can use the file system on the network. For example, you can use a network file system (SYSTEM,NFS) server to load a specific directory, and so on.
1. FHS Standard
The directory structure of Linux is complex, and simple and easy to say. The complication is that because the system's normal operation is based on the directory structure, for beginners, most of the directories do not know its role, important or not, especially for those who have been heavy Windows users, they will be tangled for a long time, about the software I installed where such problems. It's simple because most of the directory structures are well-defined (FHS standard) and are dead, and when you do, everything you do inside will become orderly.
FHS (English: Filesystem Hierarchy Standard Chinese: File system hierarchy Standards), most Linux versions use this form of file, FHS defines the purpose of each region in the system, The required minimum composition of the file and the directory also gives the exception handling and contradictory processing.
FHS defines the two-tier specification, and the first layer is /
what file data should be placed in each of the following directories, such as the /etc
settings file that should be placed, the /bin
/sbin
executable file, and so on.
The second layer is defined for the subdirectories /usr
/var
of the two directories. such as /var/log
placing system login files, /usr/share
placing shared data, and so on.
fhs_2.3 Standard Documentation
If you think the picture is not clear, it is recommended to save as a local zoom view:
If you feel that you do not understand, then you can try the most realistic and intuitive way, execute the following command:
$ tree /
If you are prompted for "command not found", install it first:
# 因为我们的环境的原因,每次新启动实验会清除系统恢复到初始状态,所以需要手动更新软件包索引,以便我们安装时能找到相应软件包的源。sudo apt-get updatesudo apt-get install tree
As for the FHS mentioned above, there is a very important thing you must understand, FHS is based on the experience of countless Linux users and developers, and will maintain the update, FHS based on the frequent use of the file system and whether or not to allow users to change freely (note, not not, the learning process, Do not be afraid of these), define the directory as a form of four interactions, as shown in the following table:
2. Directory path Path
Someone may not understand what this path means and what it does. As the name implies, the path is the route where you are going. If you want to enter a specific directory or want to get a directory of files (the directory itself is a file) then you have to use the path to find.
Use the cd
command to switch directories, in Linux used to .
represent the current directory, representing the previous level of the ..
directory (note that we described in the previous section, to begin with the .
files are hidden files, so these two directories must also be hidden, you can use the command to ls -a
check (see hidden files), which -
represents the last directory, ~
usually the current user's home
directory. Use the pwd
command to get the current path (absolute path).
Go to the top level directory:
cd ..
Go to your home
directory:
cd ~ # 或者 cd /home/<你的用户名>
Use to pwd
get the current path:
pwd
Absolute path
With respect to absolute paths, simply the complete path starting with the root "/" directory, which is the end of the directory you want to go to, in the form of:
/usr/local/bin
A directory that represents a directory in the directory under the root directory usr
local
bin
.
Relative path
Relative path, that is, relative to your current directory path, the relative path is the current directory .
as the starting point, the directory you want to the end, the form of expression such as:
usr/local/bin
(This assumes that your current directory is the root directory.) You may notice that we mean that the relative path actually does not add the one that represents the current directory, .
but rather begins with the directory name, because this directory is a subdirectory usr
/
of the directory, which can be omitted (in the case of .
a similar cannot be omitted) If it is the top level directory of the current directory, you need to use it ..
, such as your current directory as a home
directory, the root directory should be represented as the top level directory (directory) ../../
home
of the previous directory ( /
directory).
Let's start with your home
directory and enter the directory in absolute and relative paths, respectively /usr/local/bin
:
# 绝对路径$ cd /usr/local/bin# 相对路径$ cd ../../usr/local/bin
Enter a directory, you can use the absolute path can also use a relative path, then we should choose the right way to enter a directory. By intuition, you can use whichever you feel comfortable with, rather than using only one. For example, if I am currently in the /usr/local/bin
directory, I would like to go to the previous level of the local directory you say is easy to use cd ..
or cd /usr/local
convenient? And if you want to get into a usr
directory, then cd /usr
it's cd ../..
a little more convenient.
Tip: In the process of directory switching, please use the Tab
key auto-completion, to avoid input errors, two consecutive times Tab
can display all candidate results.
Third, the basic operation of Linux files 1. New New Blank file
Using touch
commands to create a blank file, the touch
main function of the command is to change the time stamp of the existing file (for example, the last access time, the last modified time), but it does not add any parameters, only specify a file name, You can create a blank file with the specified file name (not overwriting a file with the same name), or you can specify the timestamp of the file at the same time, and more about the usage of the touch
command, which will be covered in the next file search.
Create a blank file named Test, because you do not have permissions in other directories, so you need to cd ~
switch back to the user's /home/shiyanlou
directory first:
cd ~$ touch test
New Catalog
Use the Make mkdir
directories command to create an empty directory, or you can specify a permission attribute to create a directory.
Create an empty directory named "Mydir":
mkdir mydir
Using -p
parameters to create the parent directory at the same time (if the parent directory does not exist), we create a multilevel directory at the same time (this is useful when installing the software, configuring the installation path):
mkdir -p father/son/grandson
The following directory path, expressed as an absolute path, is also possible.
2. Copying and Copying files
Use cp
the (copy) command to copy a file to the specified directory.
Copy the previously created "test" file to the "/home/shiyanlou/father/son/grandson" directory:
test father/son/grandson
is not very convenient ah, if in the graphical interface you need to first copy the file in the source directory, and then into the destination directory to paste the file, and the command line operation step is one step.
Copy Directory
If you use cp
the command to copy a directory directly, the following error will occur:
To successfully copy a directory, you need to add -r
or -R
parameter, meaning recursive replication, which means "personalities":
$ cp -r father family
3. Delete deleted files
rm
Delete a file using the (remove files or directories) command:
test
Sometimes you encounter a file that you want to delete as read-only permission, and the immediate use of rm
Delete displays a prompt, as follows:
If you want to ignore this hint and delete the file directly, you can use the -f
parameter to force the deletion:
-f test
Delete Directory
As with the copy directory, to delete a directory, you also need to add -r
or -R
parameter:
$ rm -r family
4. Move file and file rename move file
Use the mv
(move or rename files) command to move the file (cut). To move the file "File1" to the Documents
directory:
mv 源目录文件 目的目录
:
$ mkdir Documents$ mv file1 Documents
Renaming files
Rename the file "File1" to "myfile":
mv 旧的文件名 新的文件名
:
$ mv file1 myfile
Batch Rename
To achieve batch renaming, the mv
command is a bit out of order, and we can use a seemingly more professional command rename
to implement it. But it has to use Perl regular expressions as parameters, about the regular expression we will be introduced in the following, only to do the demonstration, you just remember this rename
command can be batch rename, and then re-study will not have any problems, after all, you have mastered a more common mv
command.
# 使用通配符批量创建 5 个文件:$ touch file{1..5}.txt# 批量将这 5 个后缀为 .txt 的文本文件重命名为以 .c 为后缀的文件:$ rename ‘s/\.txt/\.c/‘ *.txt# 批量将这 5 个文件,文件名改为大写:$ rename ‘y/a-z/A-Z/‘ *.c
A simple explanation of the above command rename
is to first use the wildcard character of the second argument to match all .txt
the suffixes of the file, and then replace the suffix of the matching files with the regular expression provided by the first parameter, .txt
.c
after we have learned the commands behind us sed
, I'm sure you'll understand better.
5. View File Usage
cat
,
tac
And
nl
command to view files
The first two commands are used to print the file contents to the standard output (terminal), which is cat
a positive sequence display, in tac
reverse order.
Standard input and output: when we execute a shell command line normally will automatically open three standard files, namely standard input file (stdin), default corresponding to the terminal's keyboard, standard output file (STDOUT) and standard error output file (stderr), The following two files correspond to the screen that is redirected to the terminal so that we can see the output directly. The process will get input data from the standard input file, output normal output data to the standard output file, and send the error message to the standard error file.
For example, we want to see /etc
the files that were copied from the directory before passwd
:
$ cat passwd
You can add -n
parameters to display line numbers:
$ cat -n passwd
nl
command, add line numbers and print, which is a cat -n
more professional line number than the Print command.
Here is a brief list of some of its commonly used parameters:
-b : 指定添加行号的方式,主要有两种: -b a:表示无论是否为空行,同样列出行号("cat -n"就是这种方式) -b t:只列出非空行的编号并列出(默认为这种方式)-n : 设置行号的样式,主要有三种: -n ln:在行号字段最左端显示 -n rn:在行号字段最右边显示,且不加 0 -n rz:在行号字段最右边显示,且加 0-w : 行号字段占用的位数(默认为 6 位)
You will find the use of these commands, the default terminal window size, a screen display of the content of the text, you have to use the mouse to drag the scroll bar or slide the wheel to continue to page down, if you can directly use the keyboard to turn the page, then you can use the following command to introduce.
Use
more
And
less
command paging to view files
If the above cat
is used to quickly view the contents of a file, then this more
and less
is a natural to "read" The content of a file, such as the Man Manual is used less
to display content. The more
commands are relatively simple and can only be scrolled in one direction, while less
for the basis of more
and vi
(a powerful editor we have separate courses to let you learn) development, more powerful. less
the use of basic and more
consistent, specific use please check the Man manual, here only to describe more
the use of commands.
To more
open a file with a command passwd
:
$ more passwd
When turned on, only one screen is displayed by default, and the bottom of the terminal shows the current reading progress. You can use the key to scroll Enter
down Space
a line, scroll down one screen with the key, press h
show Help, q
exit.
Use
head
And
tail
command to view files
These two commands, those who are impatient people should like, because they are only to view the first few lines of the file (the default is 10 lines, less than 10 lines show all) and the last few lines. Or take the passwd file example, such as when we want to see the recently added user, then we can view the /etc/passwd
file, but we also saw in front of the file, a lot of messy things, it seems really bothered. Because the system adds a new user, the user's information will be added to the end of the passwd file, then we can use the tail
command:
$ tail /etc/passwd
Even more directly look at one line, plus the -n
parameter, followed by the number of rows:
1 /etc/passwd
About tail
the command, you have to mention it is a very good parameter -f
, this parameter can be implemented to read the contents of a file and display. This allows us to dynamically view the logs to achieve real-time monitoring purposes. But I'm not going to go into this basic course approached more details about it, and interested users can learn it for themselves.
6. View File types
As I mentioned earlier, the types of files in Linux are not judged by file suffixes, we usually use file
commands to view the file types:
$ file /bin/ls
Description This is an executable file that runs on a 64-bit platform and uses a dynamic link file (Shared library).
7. Edit the file
Editing files under Linux usually we will use a dedicated command line editor like (Emacs,vim,nano), because the content of the editor involved in Linux is more, and very important, so we have a separate course dedicated to this editor vim. Strongly hope that you are learning this Linux basic course you first pause here, to learn the use of the Vim editor (at least the basic operation), and then continue the course after the content, because the following will assume that you have learned the use of the Vim editor. If you want to get started faster, you can start by using the internal vim learning tutorial in Linux and enter the following command:
$ vimtutor
(go) Linux directory structure