Directory structure and file basic operation

Source: Internet
Author: User
Tags line editor

Directory structure and file basic operation Linux directory structure and file basic operation introduction

1, the Linux file organization directory structure. 2, relative path and absolute path. 3, the movement of files, copying, renaming, editing and other operations.

First, 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 of the user files are 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 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 close to the heavy Windows users, they will struggle for a long time, about the software I installed where such problems. It's simply because, most of its directory structure is regulated (FHS standard), is dead, and when you master it, everything you do in it 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 layer specification, the first layer is,/below the directory should be put what file data, such as/etc should be placed in the settings file,/bin and/sbin should be placed executable files and so on.

The second tier is defined for sub-directories of the/USR and/var directories. For example,/var/log places system login files,/usr/share places shared data, and so on.

fhs_2.3 Standard Documentation

If you feel that you do not understand this, 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, remember 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 command to view hidden files), - representing the last directory, usually representing the "home" directory of the current user. 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:


Absolute path

With respect to absolute paths, simply the full path starting with the root "/" directory, with the directory you want to end with, in the form of a: /usr/local/bin , representing the bin directory in the local directory in the USR directory under the root directory.

Relative path

Relative path, that is, relative to your current directory path, 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 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 is the "home" directory, the root should be represented as the previous level of the directory ../../ ("Home" directory) of the upper level directory ("/" directory).

Below we use your "home" directory as the starting point, respectively, the absolute path and relative path to enter the /usr/local/bin directory:

# 绝对路径$ 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 top 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

Ii. basic operation of Linux files 1. Create new blank file

Use the touch command to create a blank file, about the touch command, which is mainly to change the time stamp of the existing file (for example, the last access time, last modified time), but it does not add any parameters, only specify a file name, You can create a blank file for 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 to specify permission properties for creating 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 and configuring the installation path at times):

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 or directory 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 need to first copy the file in the source directory, and then into the destination directory to paste the file, the command line operation step is one step.

Copy Directory

If you cp copy a directory directly using the command, the following error will appear:

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

rmDelete a file or directory using the (remove files or directories) command:


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 mv the (move or rename files) command to move the file (cut). Move the file "File1" to the "Documents" directory mv 源目录文件 目的目录 :

$ 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 reach, and we can use a seemingly more professional command rename . However, it is 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 file with the regular expression provided by the first parameter, .txt .c after we learned the command behind us sed , I believe you will understand better.

5. View File Usage cat, tacAnd nlcommand to view files

Both 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), All two files correspond to the screen being 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 view the files that were copied from the "/etc" Directory passwd :

$ cat passwd

You can add -n parameters to display line numbers:

$ cat -n passwd

nlcommand, 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 moreAnd lesscommand paging to view files

If the cat above is used to quickly view the contents of a file, then this more and less is meant to be used to "read" The contents of a file, such as "Man" inside the manual is used to display content. morethe commands are relatively simple and can only be scrolled in one direction, while "less" is more powerful than "more" and "VI" (a powerful editor, we have separate courses to let you learn). The use of less is basic and more consistent, specific use please check the Man manual, here only to describe the use of more commands.

To more open a file using a tool passwd :

$ more passwd

When turned on, only one screen is displayed by default, and the bottom of the terminal shows the current reading progress (percentage). 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 headAnd tailcommand to view files

These two commands those who are more anxious people should prefer, because they are only the first few lines of view (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. This is the idea of adding a new user to the system, which should add the user's information 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:

$ tail -n 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 log as a function of real-time monitoring, but I will not introduce it in this basic course approached more details, interested users can get to know.

6. View File types

As I mentioned earlier, the types of files under Linux are not judged by the file suffix, we usually use file commands to view the file types:

$ file /bin/ls

This means that 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 use a dedicated command-line editor like (Emacs,vim,nano), because the content of the editor involved in Linux is more and more important, so we have a separate basic course specifically about one of these editors (VIM). here is a strong hope that you are learning this Linux basic course, you pause here first, to learn the use of the Vim editor (at least master 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 Vim learning tutorial inside Linux, and enter the following command:

$ vimtutor
Tags: learning path----Big Data engineer

Directory structure and file basic operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.