Basics of Linux Basics----directory structure and file basic operations

Source: Internet
Author: User
Tags parent directory touch command line editor
Linux directory structure and basic file operations
1. The file organization directory structure of Linux. 2. Relative and absolute paths. 3. Move, copy, rename, and edit files.

First, the Linux directory structure
Before talking about the Linux directory structure, you must first understand something, that is, the difference between the Linux directory and the Windows directory may not be much different for the general operation experience, but they are completely different from their implementation mechanism. of.

One difference is reflected in the relationship between the directory and the storage medium (disk, memory, DVD, etc.). In the past, Windows has always been based on storage media, mainly using drive letters (C drive, D drive ...) and partitions. To achieve file management, and then the directory, the directory is not so important, user files other than the system files are placed in any directory anywhere does not matter much. So after Windows has been used for a period of time, the file directories on the disk will appear cluttered (except for a few good users). However, UNIX / Linux is just the opposite. UNIX is based on directories. Linux also inherits this excellent feature. Linux builds the entire system in the form of a tree-like directory structure, which can be understood as the skeleton of a user-operable operating system. Although in essence both the directory structure and the operating system kernel are stored on disk, logically speaking, Linux disks are "mounted" (mounted) on directories, and each directory can not only use local disk partitions File system, you can also use the file system on the network. For example, you can use a Network File System (NFS) server to load a specific directory.

1.FHS standard
The directory structure of Linux is complex and complex, and simple and simple. The complication is that because the normal operation of the system is based on the directory structure, for beginners most directories do not know its role, whether it is important or not, especially for those recent heavy Windows users, they will struggle with it for a long time. Time, questions about where is the software I installed. It's simple because most of its directory structure is specified (FHS standard), and it's dead. When you master it, everything you do inside it will become orderly.

FHS (English: Filesystem Hierarchy Standard Chinese: File System Hierarchy Standard), most Linux distributions use this file organization form, FHS defines the purpose of each area in the system, the minimum required files and directories are also given. Exception handling and contradiction handling.

FHS defines two layers of specifications. The first layer is what file data should be placed in each directory under /. For example, / etc should place settings files, / bin and / sbin should place executable files, and so on.

The second level is defined for the subdirectories 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 Document

If you don't understand this, then you can try the most realistic and intuitive way, execute the following command:

$ tree /
If it says "command not found", install it first:

# Because of our environment, each new start of the experiment will clear the system and restore the initial state, so we need to manually update the package index so that we can find the source of the corresponding package during installation

sudo apt-get update

sudo apt-get install tree
Regarding the FHS mentioned above, there is another very important content here. You must understand that FHS is based on the experience of countless Linux users and developers in the past and will be updated. FHS is based on the frequency of file system usage And whether to allow users to change at will (note that it is not impossible, do not be afraid of these during the learning process), define the directory as four forms of interaction, as shown in the following table:

Directory path
Someone may not understand what this path means and what it does. As the name suggests, the path is where you are going. If you want to enter a specific directory or want to get the files of a directory (the directory itself is also a file), you have to use the path to find it.

Use the cd command to switch directories. In Linux, use. To indicate the current directory and .. to indicate a higher-level directory (** Note, remember that we introduced in the previous section, files that begin with. Are hidden files, so this The two directories must also be hidden, you can use the ls -a command to view hidden files),-indicates the last directory, ~ usually indicates the current user's "home" directory. Use the pwd command to get the current path (absolute path).

Go to the upper directory:

$ cd ..
Go to your "home" directory:

$ cd ~
# Or cd / home / <your username>
Use pwd to get the current path:

$ pwd
Absolute path
The absolute path is simply a complete path starting from the root "/" directory and ending with the directory you want to reach. The expression is: / usr / local / bin, which means local in the usr directory under the root directory. The bin directory in the directory.

relative path
The relative path is the path relative to your current directory. The relative path is based on the current directory. The starting point is the directory you want to reach. The expression is as follows: usr / local / bin (assuming your current directory is root) table of Contents). You may notice that we said that the relative path does not actually add the one that represents the current directory, but directly starts with the directory name, because this usr directory is a subdirectory under the / directory, which can be omitted. I mentioned a similar situation that cannot be omitted); if it is a directory higher than the current directory, you need to use .. For example, if your current directory is the "home" directory, the root directory should be expressed as ../../ The parent directory ("/" directory) of the parent directory ("home" directory).

Let's start with your "home" directory and enter the / usr / local / bin directory with absolute and relative paths:

# Absolute path
$ cd / usr / local / bin
# relative path
$ cd ../../usr/local/bin
To enter a directory, you can use absolute or relative paths. When should we choose the correct way to enter a directory? It's just intuition. You can use whichever one is convenient instead of using only one. For example, suppose that I am currently in the / usr / local / bin directory, and I want to go to the local directory of the upper level. Do you want to use cd .. convenient or cd / usr / local convenient. And if you want to enter the usr directory, then cd / usr is a little more convenient than cd ../ ..

Tip: During the directory switching process, please use the Tab key to complete automatically to avoid typing mistakes. Press Tab twice in succession to display all candidate results.

Second, the basic operation of Linux files
Use the touch command to create a blank file. The touch command is mainly used to change the timestamp of an existing file (for example, the most recently accessed time and the most recently modified time), but it only specifies one file without any parameters. Name, you can create a blank file with the specified file name (the existing file with the same name will not be overwritten). Of course, you can also specify the time stamp of the file at the same time. Involved.

Create a blank file named test. Because you don't have permissions in other directories, you need to cd ~ first and switch back to the user's / home / shiyanlou directory:

$ cd ~
$ touch test
New directory
Use the mkdir (make directories) command to create an empty directory or specify the permissions attributes of the created directory.

Create an empty directory named "mydir":

$ mkdir mydir
Use the -p parameter to create the parent directory at the same time (if the parent directory does not exist). As below, we create a multi-level directory at the same time (this is very useful when installing software and configuring the installation path):

$ mkdir -p father / son / grandson
Subsequent directory paths can also be expressed as absolute paths.

Copy file
Use the cp (copy) command to copy a file or directory to the specified directory. Copy the "test" file you created earlier into the "/ home / shiyanlou / father / son / grandson" directory:

$ cp test father / son / grandson
Is it very convenient? If you need to copy the files in the source directory first and then go to the destination directory to paste the files in the graphical interface, the command line operation steps are in place.

Copy directory
If you use the cp command directly to copy a directory, the following error will occur:

To successfully copy the directory, you need to add the -r or -R parameter, which means recursive replication, that is, it means a little "strains of nine families":

$ cp -r father family
3. delete delete file
Use the rm (remove files or directories) command to delete a file or directory:

$ rm test
Sometimes you will encounter that you want to delete some files with read-only permissions. Using rm directly to delete will display a prompt, as follows:

If you want to ignore this prompt and delete the file directly, you can use the -f parameter to force the deletion:

$ rm -f test
Delete directory
Like copying a directory, to delete a directory, you also need to add the -r or -R parameter:

$ rm -r family
4. Move files and file renaming Move files
Use the mv (move or rename files) command to move (cut) files. Move file "file1" to "Documents" directory mv source directory file destination directory:

$ mv file1 Documents
Rename file
Rename the file "file1" to "myfile" mv Old file name New file name:

$ mv file1 myfile
Batch renaming
To implement batch renaming, the mv command is a bit weak. We can use a more professional command named rename. However, it uses perl regular expressions as parameters. We will introduce regular expressions later. This is just a demonstration. You just need to remember that the rename command can be renamed in batches. There will be no problems, after all you have mastered a more commonly used mv command.

# Create 5 files in batches using wildcards
$ touch file {1..5} .txt

# Batch rename these 5 text files with a .txt suffix to files with a .c suffix
$ rename ‘s / \. txt / \. c /‘ * .txt

# Change these 5 files in batches, and change the file names to uppercase
$ rename ‘y / a-z / A-Z /’ * .c
Briefly explain the above command, rename first uses the wildcard character of the second parameter to match all files with the suffix .txt, and then uses the regular expression provided by the first parameter to replace the .txt suffix of these files with .c This point, after we learn the sed command later, I believe you will understand better.

5. View files View files using cat, tac and nl commands
These two commands are used to print the contents of the file to the standard output (terminal), where cat is displayed in positive order and tac is displayed in reverse order.

Standard input and output: When we execute a shell command line, three standard files are usually automatically opened, namely standard input file (stdin), which corresponds to the keyboard of the terminal by default; standard output file (stdout) and standard error output file (stderr). Both files are redirected to the screen of the terminal so that we can directly see the output. The process gets input data from the standard input file, outputs normal output data to the standard output file, and sends error information to the standard error file.

For example, we want to view the passwd file copied from the "/ etc" directory:

$ cat passwd
You can add the -n parameter to display the line number:

$ cat -n passwd
nl command, add line number and print, this is a more professional line number print command than cat -n.

Here are a few of its commonly used parameters:

-b: Specify the way to add line numbers. There are two main ways:
    -b a: indicates that the line number is listed regardless of whether it is a blank line ("cat -n" is this way)
    -b t: only list and list non-blank lines (this is the default)
-n: Set the style of line number. There are three main types:
    -n ln: display at the left end of the line number field
    -n rn: display at the far right of the line number field without adding 0
    -n rz: display at the far right of the line number field, and add 0
-w: the number of digits occupied by the line number field (default is 6 digits)
You will find that using these commands, the default terminal window size, and the endless display of text content, you must use the mouse to drag the scroll bar or slide the scroll wheel to continue to page down. If you can use the keyboard to directly turn the page OK, then you can use the commands described below.

Use more and less commands to page through files
If the above cat is used to quickly view the contents of a file, then the more and less are inherently used to "read" the contents of a file, for example, the "man" manual uses less to display the contents. among them The more command is relatively simple and can only scroll in one direction. "less" is developed based on "more" and "vi" (a powerful editor, we have separate courses for you to learn), and more powerful. The use of less is basically the same as that of more. For specific usage, please refer to the man page. Only the use of the more command is introduced here.

Use the more tool to open the passwd file:

$ more passwd
By default, it only displays one screen of content, and the bottom of the terminal displays the current reading progress (percentage). You can use the Enter key to scroll down one line, use the Space key to scroll down one screen, press h to display help, and q to exit.

View files with head and tail commands
These two commands should be preferred by those who are more quick-tempered, because they are only the first few lines (the default is 10 lines, all are displayed below 10 lines) and the last few lines. Let's take the passwd file as an example. For example, when we want to view the newly added users, we can view the / etc / passwd file, but we also saw earlier that there are a lot of messy things in this file, which looks really troublesome. what. Here I think that a new user is added to the system, and the user information should be added to the end of the passwd file. Then we can use the tail command at this time:

$ tail / etc / passwd
Even more directly looking at only one line, plus the -n parameter, followed immediately by the number of lines:

$ tail -n 1 / etc / passwd
Regarding the tail command, I have to mention that it has a very good parameter -f, which can continuously read the content of a file and display it. This allows us to dynamically view the log for real-time monitoring, but I won't introduce more details about it in this basic course, and interested users can learn it by themselves.

6. View file types
I mentioned earlier that the type of file under Linux is not determined by the file suffix. We usually use the file command to view the type of file:

$ file / bin / ls
This means that this is an executable file that runs on a 64-bit platform and uses dynamically linked files (shared libraries).

7. Edit the file
When editing files under Linux, we usually directly use a special command line editor such as (emacs, vim, nano). Because the content of the editor on Linux is more and more important, we have a separate basic course dedicated to it. One of these editors (vim). It is strongly hoped that those who are learning this Linux basic course will pause here first, learn the use of the vim editor (at least master the basic operations), and then continue the rest of this course, because the latter content will assume that you Learned to use the vim editor. If you want to get started more quickly, you can use the vim tutorial inside Linux directly, enter the following command to get started:

$ vimtutor
Introduction to Linux Basics-Directory Structure and Basic File Operations

Related Article

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: info-contact@alibabacloud.com 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.