Linux directory structure and file basic operation

Source: Internet
Author: User
Tags create directory perl regular expression touch command

Linux directory structure

The difference between Linux and the Windows directory structure is reflected in the relationship between the directory and the storage medium. In the past, Windows has been mainly storage media, mainly in the drive letter (c, D disk) and partition to achieve file management, and then the directory below. Therefore, the directory is not very important, except for the system files of the user files placed anywhere in any directory is not much of a relationship. So usually after Windows has been in use for a while, the file directories above the disk appear cluttered. 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, logically speaking, Linux disks are "hung" on the directory, each directory can not only use the local disk partition of the file system, you can also use the file system on the network.

FHS Standard

The complexity of the directory structure of Linux is that the normal operation of the system is based on the directory structure, most of which the directory structure is well-formed (FHS standard).

FHS (Filesystem Hierarchy Standard): File system hierarchy standards, most Linux versions use this form of file, FHS defines the purpose of each region of the system, the minimum required composition of files and directories also gives the exception handling and contradictory processing.

FHS defines the two layer specification, the first layer is/below the various directories should be put what file data, such as/etc should put settings file,/bin and/sbin to put executable files and so on. The second tier is defined for sub-directories of the/USR and/var directories. such as/var/log place system login file,/urs/share share data and so on.

/bin is available for general users, commands that are used when the file system is not mounted, commands that can be used,

/boot/grub Boot settings related files

/boot/vmlinuz kernel files

/dev Device files

/ETC/RC.D a linked file that stores startup divisions of different operating levels

/etc/x11 and/ETC/RC.D include an editable configuration file for system oil, which is a local file for control program operation

/Home User Directory

/lib dynamic Library and module files for storing programs

/media/cdrom,/media/floppy, and/meida/u disks for hanging on local disks or other storage devices

/mnt for hanging on other temporary file systems

/opt Some packages for the release version of the attachment

/root Root User's home

/sbin commands that can only be executed by the root user, some systems are updated, backed up, restored, and used by the switch machine

/srv data files required to store the service process love you

/temp storing various temporary files

The second level of the/USR directory structure for storing read-only user data, including the vast majority of user tools and applications

/usr/bin non-essential executable file for all users

/usr/include standard package with header file

Libraries for binary files in/usr/lib/urs/bin/and/usr/sbin/

/usr/local the third level of local data, specifically to the host computer

/usr/share architecture-Independent shared data

/usr/sbin non-essential system binaries

/USR/SRC Source Code

The/var directory structure is used to store variable files, which are constantly changing the contents of a file in a functioning system, sometimes a separate partition.

$ sudo apt-get update; sudo apt-get install tree

$ tree//view level directory

$ tree \//View all directory Structures

The FHS will maintain the update, FHS according to the frequency of use of the file system and whether or not to allow the user arbitrary changes, the directory is defined as four interactive forms:

Shareable (shareable) Non-shareable (unshareable)
Immutable (Static) /usr (software placement)/opt (third-party software) /etc (config file)/boot (boot and kernel files)
Variable (variable) /varmail (user email)/var/news (News Group) /var/run (program related)/var/lock (file lock related)

Directory path

Use the CD command to switch directories. In Linux: Represents the current directory,.. Represents the previous level of the directory. Files that begin with. are hidden files, so the two directories are also hidden, and you can use the ls-a command to view hidden files. -Represents the last directory, ~ represents the current user's ' home ' directory. Use the PWD command to get the absolute path currently in place.

Absolute path: The full path starting with the root "/" directory. such as/usr/local/bin

Relative path: In the current directory. As the starting point, end with the directory you want to reach. such as Usr/local/bin. Indicates that the relative path does not actually include the "." that represents the current directory, but rather begins with the directory name. Because the USR directory is a subdirectory of the/directory, you can omit the. If the current directory is in the top level directory, you need to use ...

In the process of directory switching, you need to use the TAB key Auto-completion, you can avoid input errors, two consecutive tabs can display all candidate results.

Basic operations for Linux files

New

New blank file: Use the touch command to create a blank file. The touch command is primarily to change the timestamp of an existing file, but if you specify only one file name without any arguments, you can create a blank file for the specified file name.

$ Touch Test

Create directory: Use the mkdir (make directories) command to create an empty directory, or you can specify permission properties to create a directory.

$ mkdir Mydir

Use the-p parameter to create the parent directory at the same time (if the parent directory does not exist)

$ mkdir-p Father/son/grandson

 

Copy

Copy files: Use the CP (copy) command to copy a file or directory to the specified directory.

$ CP Test Father/son/grandson

Copy directory: After the CP with the-R or-r parameter, indicating recursive replication, you can copy a directory

$ cp-r Father Family

Delete

Delete files: Use the RM (remove files or directories) command to delete a file or directory.

$ RM Test

You can use the-f parameter to force the deletion of a file (ignoring the prompt to delete a read-only permission file)

Delete directory: After using the RM command, add the parameter-R or-R.

$ rm-r Family

Moving files and renaming files

Move files: Move files (cut) using the MV (move or rename files) command.

$ Touch File1

$ ls file1

$ mv File1 Documents

$ CD Documents

$ ls

Rename file: Use MV command mv old filename new file name

$ mv File1 myfile

Batch rename: Use the rename command for batch renaming. But it requires a Perl regular expression as a parameter.

$ touch file{1..5}.txt//Use wildcards to create 5 of files in bulk

$ Rename ' s/\.txt/\.c/' *.txt//batch 5 suffix. txt files are renamed to files with a. c suffix

$ Rename ' y/a-z/a-z/' *.c//batch of these 5 files, file name changed to uppercase

View Files

View files using the cat, TAC, NL commands.

Both the cat and TAC are used to print the file contents to the standard output terminal, where cat is the positive sequence display and the TAC is displayed in reverse order.

Standard input and output: when we execute a shell command, we usually automatically open three standard files, namely the standard input file (stdin), the default corresponding to the terminal's keyboard, the standard output file (stdout) and the 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.

$ cp/etc/passwd.

$ cat-n passwd//-n parameter display line number

The NL command adds line numbers and prints more professionally than cat-n.

The following parameters are commonly used after NL:

-B: Specifies the line number to add. There are two main types:-B A: Indicates that the line number is also listed (Cat-n is this way), whether or not it is a blank line;-b-t: Lists only the number of empty lanes and lists (default)

-N: Sets the style of the line number. There are three main kinds:-N ln: line Number field drunk left side display;-N RN: Line Number field is displayed at the far right and no 0;-N RZ: line number is displayed at the far right, plus 0

-W: Number of digits occupied by the line Number field (default 6-bit)

$ nl-b a passwd

$ nl-b T passwd

$ nl-n LN passwd

$ nl-n RZ passwd

$ nl-n RN passwd

$ nl-w 2-n RZ passwd

Paging through files using the more and less commands

The more command can only scroll in one direction, while less is developed based on more and VI and is more powerful. Displays only one screen after opening, the bottom of the terminal shows the current reading progress (percentage), you can use ENTER to scroll down one line, use the space key to scroll down a screen. h Display Help, Q exit.

$ more passwd

The head and tail commands view only the first 10 rows (the default) and the tail 10 lines of the file. The tail with parameter-F enables you to read and display the contents of a file continuously. This feature allows us to dynamically view the role of the log as a real-time monitor.

$ tail/etc/passwd

$ tail-n 1/etc/passwd//View only the last line of the file

View File types

The file command allows you to view the type of files

$ file/bin/ls

Edit file: You can use the Vimtutor internal command to learn the use of vim.

 

 

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.