Linux directory structure and file basic operation

Source: Internet
Author: User
Tags parent directory line editor

Organize from

Https://www.shiyanlou.com/courses/running/59

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 a two-tier 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.

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 update

 sudo 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

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).

Second, the basic operation of Linux files

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.

test

New Catalog

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

2. Copying

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

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":

3. Delete

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

$ rm -r family

4. Move file and file rename

Moving files

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

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.

# Batch creation of 5 files using wildcards

$ Touch file{1: 5}.txt

# Rename the 5 text files with a. c suffix to a file

$ rename ' s/\.txt/\.c/' *.txt

# Batch The 5 files, the file name to uppercase

$ 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 Files

Use 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: Specifies how the line number is to be added, mainly in two ways:

-B A: Indicates that the line number is also listed ("Cat-n" Is this way), regardless of whether it is a blank line

-B T: Lists only the number of non-blank lines and lists them (this is the way that is implied)

-N: Sets the style of line number, there are three main types:-N ln: The leftmost display of the row number field

-N RN: The far right of the line number field is displayed without 0-n RZ: The line Number field is displayed at the far right, plus 0

-W: the number of digits occupied by the line Number field (default is 6 bits)

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

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:

Linux 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: 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.