2017-10-27linux Basics (9) Bash basic features

Source: Internet
Author: User
Tags clear screen parent directory

2017-10-27linux Basics (9) Bash basic features

In the previous chapter we talked about FHS, as well as some of the features of command and Bash command history, FHS is the standard filesystem hierarchy, which specifies the classification of the file system to be stored, so that the distribution of the various Linux to get the reference and unity, then we introduced some basic commands, such as: add aliases, etc. , finally we introduce the type of Linux file system, and so on, then we continue to introduce the basic features of bash, as well as some other commands.

I. Order completion

The user in the Shell interface (or CLI interface) to enter a character, resolved to whether it is an internal command or external command, if it is an external command, the shell program in the specified file path to find the user typed Word typeface corresponding, if the character and the directory file is unique, you can use the TAB key to complete, If not unique, click the tab will not show what, but the second will show a list of commands to give you a reference, if you press the tab is not used, even if you enter, the shell will prompt you do not have this command.
So to summarize, the shell program receives the user executes the command request, after the analysis completes, the leftmost string will be treated as the command, but the lookup processing mechanism divides into two kinds, if is the shell internal command, looks for its shell internal command, if is the external command, The file names under the directory are searched from left to right according to the directory set in the PATH environment variable.
When you enter a command, if it is unique, press and hold the tab to complete the completion, because the given heading character can uniquely identify a program file for a command, the direct completion of the program file that does not uniquely identify a command, the TAB key once, give the corresponding list for you to choose.
So the command complement is a given starting path, the corresponding path under the beginning of the string to match each file under the starting path, if you can uniquely identify, then directly complete, otherwise, once again tab, give the list.

1.1 Commands for the Directory management class

The first thing we want to talk about is mkdir and rmdir these two commands, then as the name implies, is to create and delete directories, in Windows we create a directory, the right-click New folder, in Linux we use the mkdir command in the creation of the directory, Its command format is:

# mkdir [OPTION] ... DIRECTORY ...

The following options are commonly used:

-P: Automatically create parent directory on demand;-v:verbose, show detailed process;-M MODE: directly given permissions;

It is important to note that the base name of the path is the object of the command; the path before the base name must exist; The starting base name is the last name.
Next is the delete directory command, delete the directory command for RmDir, if the directory is an empty directory to delete, if there is a file or directory, we can delete in bulk, the use of the command is the same as the use of the directory to create the same:

# rmdir [OPTION] ... DIRECTORY ...

The options are as follows:

-P: After deleting a directory, if its parent directory is empty and deleted;-V: the display process;

The following two questions are now available:
1, how to create/tmp/x/y1,/tmp/x/y2,/tmp/x/y1/a,/tmp/x/y1/b?
2, how to create A_c, A_d, B_c, b_d?

Second, the command line to expand

command-line expansion There are just a few categories: one is the home directory, the class is {}, we also talked about at the command prompt ~ represents the current home directory or is automatically expanded to the user's home directory, and {} is a list of paths that can host a comma, and can be expanded to multiple paths, as an example of the following:

For example:/tmp/{a,b}, equivalent to:/tmp/a,/tmp/b

OK above is the command line expansion, then the answers to the first two questions are as follows:

# MKDIR-PV X/{y1/{a,b},y2} # MKDIR-PV {a,b}_{c,d}

Now there's a problem 3: Create the following structure:

/tmp/mysysroot/binsbinetc Sysconfignetwork-scriptsusr bin Sbin localbinsbinetcliblib lib64 v AR Cache Log Run

Answer:

# mkdir-pv/tmp/sysroot/{bin,sbin,etc/sysconfig/network-scripts,usr/{bin,sbin,local/{bin,sbin,etc,lib}},lib/ Lib64,var/{cache,log,run}}

We can use the tree command to look at, it should be noted that in the Linux system, there may be no such command, we can use the Yum command to install, as for the use of the Yum command, we then talk about system management again.

# yum-y Install tree

Command usage:

# tree [Options] [Directory]-l level: Specifies the hierarchy to display;
# tree/tmp/sysroot//tmp/sysroot/|--bin |--etc |       '--sysconfig |   '--network-scripts |--Lib |   '--lib64 |--sbin |--usr |   |--Bin |   |--Local |   |   |--Bin |   |   |--etc |   |   |--Lib |   |   '--sbin | '--sbin '--VAR |--cache |--Log '--run directories, 0 files

Iii. execution status results of the command

when we enter a command in the shell, if the execution of the right will show its corresponding results, the command error also has a corresponding prompt, and the result of each command has its return value, the result is nothing more than correct and error, then Bash is the execution status of the return value to output this result, Success is 0, the range of failures is large, from 1 to 255, when your command executes, its status return value is stored in Bash's special variables $? .

# ls anaconda-ks.cfg fstab functions lover.txt shell_scripts # echo "$?"  0 # Lss-bash:lss:command not found # $? -bash:127:command not found

Also, when the command executes normally, some return the returned value of the command result, and the results vary depending on the command and its function.
We can also refer to the execution result of the command in the following two ways:

$ (command) or ' command ' # mkdir ' date + '%f_%t ' # ll Drwxr-xr-x. 2 root root 6 Oct 29 11:13 2017-10-29_11:13:15

Iv. references

We can use single and double quotation marks to refer to variables and statements, etc., the difference is a strong reference, the latter is a weak reference, the strong reference is whether you are not a variable, you are shown as is, do not show the variable result, only the variable name, weak reference and strong reference is different, the weak reference is able to display the result of the variable

# echo ' $PATH ' $PATH # echo "$PATH"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

There is also a command reference, which we talked about here, a little bit here.

Five, shortcut keys

We will know all kinds of shortcut keys in Windows, the simplest is simply copy (CTRL + C) and paste (Ctrl + V), in Linux also has its corresponding shortcut keys, we use shortcut keys to better deal with the Linux command:

CTRL + A: Jump to the beginning of the command line, Ctrl+e: Jump to the end of the command line, Ctrl+u: Delete all characters from the beginning of the line cursor, ctrl+k: Delete all the characters at the end of the cursor; Ctrl+l: Clear screen, equivalent to clear;

5.1 File View class commands

We've talked about the commands for the file view class before, the cat and the TAC are in the positive order, the latter is the reverse view, this is two basic commands, but these two commands are displayed to the bottom directly, and there is no viewing process, so the more command and the less command is to show the viewing process, These two command formats are also very simple:

# more FILE

The more command has a feature: turn the screen to the end of the file will automatically exit;

# less FILE

Another file View Class command is head and tail, unlike the former command to view the first n rows, while the latter command looks at the penultimate line, in the following format:

# Head [options] FILE

Options:

-N #-#

Head default does not add the option, just show the first 10 lines by default, tail command is also shown as the last 10 lines;

# tail [options] FILE

Options:

-N #-#-F: View the end of the file and not exit, followed by the display of new lines;

Next to the stat command, the Stat command is undoubtedly showing the file or file system status, the command format is

# stat FILE ...

If you look at a file status, you'll see the results, and we'll show you an example:

   # stat anaconda-ks.cfg     File:  ' anaconda-ks.cfg '     Size: 1612      Blocks: 8           IO Block: 4096   regular file   device: 803h/2051dinode: 50824939    links: 1  access:   (0600/-RW-------)   Uid:  (    0/    root)     Gid:  (    0/    root)   context: system _u:object_r:admin_home_t:s0  access: 2017-10-03 22:18:51.777786103 +0800   modify: 2017-10-03 22:18:51.794786340 +0800  change: 2017-10-03  22:18:51.794786340 +0800 

We are mainly talking about the last three lines, the first few lines we are easy to understand. As shown above, there will be three types of time, namely access time, modification time, and change time.

Access time: Indicates the date of the last access (only access, no changes) to the file; Modify time: The date when the file was last modified; Change time: Last file property modification, including permissions, size, attributes, etc.;

5.2 File Management Tools

There is a File View tool has its management tools, such as our copy, move and delete are used to manage the file commands, first we say the copy command, the process of copying the command is very simple, the data stream of the file is piped to a new file, the original file is retained, the move will also delete the source file, While the data flow is retained in the new file, the deletion is to remove the data from the disk, but does not mean that there is no data in the disk block, when the new data is written, then those disk blocks will be automatically replaced, and the block of the source data will not be retained; Now let's talk about the CP command.
The CP command is nothing more than a copy command, a process that produces a target file from a source file, with single source replication and multiple source replication options, with the following command format:

Single source copy: # CP [OPTIONS] ... [-T] SOURCE REST Multi-source replication: # CP [OPTIONS] ... SOURCE ... Directory # CP [OPTIONS] ...-t DIRECTORY SOURCE ...

Let's start with the single source. Copy, single source copy: If Dest does not exist: The file is created beforehand and the data flow from the source file is copied to dest, but if dest exists, then two things are considered if dest is a non-directory file: Overwrite its destination file The other is if Dest is a directory file: Then the Dest directory is now created with a file with the same name as the source file and its data stream copied.

Single source copy: # CP [OPTIONS] ... [-T] SOURCE REST

The other is multi-source replication, is to copy multiple files to a place, if dest does not exist there are two options, the first is if the dest is a non-directory file: The direct error, but if dest is a directory file: Copy each file to the target directory, and keep the original.
The common options for CP are as follows:

-I: Interactive replication, that is, to notify the user before overwriting;-F: Forcibly overwrite the target file;-r: Recursively copy the directory;-d: Copy the symbolic link itself instead of the source file;-A:---preserve=all, archive, for archiving--preserv=m Ode: Permissions Ownership: Owner and group timestamps: Timestamp context: Security label Xattr: Extended Properties Links: Symbolic link all: All of the above properties

Then the command for the move for the MV command, using the format of the command is similar to the CP command, the approximate format is:

MV [OPTION] ... [-T] SOURCE DEST MV [OPTION] ... SOURCE ... Directory mv [OPTION] ...-t DIRECTORY SOURCE ...

Common options:

-I: Interactive;-f:force

The next step is to delete the command, the RM command is to clear the command, this command is very dangerous, can not get the system to delete, it is simply ashes, and this delete command, is irreversible operation, please use this command, the command format is as follows:

# RM [OPTIONS] ... FILE ...

Common options:

-i:interactive-f:force-r:recursive

If you want to forcibly delete a directory, use the following format:

# Rm-rf/path/to/dir

One of the most dangerous operations:

# RM-RF/*

Dangerous commands recommend using it and experience its severity, so that you can be cautious in your production environment. Note that all unused files are recommended not to be deleted directly, but rather to move a dedicated directory. (Analog Recycle Bin)

Vi. variables

Variable is actually a very big topic, here is simply said that the variable is in memory to apply for a piece of space to hold the data, with the variable name to identify its memory address of the variable, named memory space, there are many types of variables, roughly divided into character type, numerical type, etc., we take the character type as an example, is divided into exact values and approximate values. Using variables we must consider its storage format, data scope and the nature of participating operations, then the copy operation of the variable is: Name=var.

This article from "Scorpio" blog, reproduced please contact the author!

2017-10-27linux Basics (9) Bash basic features

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.