Shell Introduction/Command History/Command completion/wildcard/output input redirection

Source: Internet
Author: User
Tags aliases echo command

Shell Introduction
* shell是一个命令解释器,提供用户和机器之间的交互。* 支持特定语法,比如逻辑判断、循环。* 每个用户都可以有自己特定的shell。* Centos7默认shell为bash。* 还有zsh、ksh
Command history

We have the command on the terminal, all have its history, such as pressing the UP button at this point will see you last entered a command, and then press on to go forward, here began to learn to understand the command history

Use The history command to view historical commands;

[Email protected] ~]# history

Here you can see that I have entered 429 commands.

The history command is a file saved, in the root user's home directory, named . Bash_history, which is a hidden file so don't forget the dots;

[Email protected] ~]# ls/root/.bash_history
/root/.bash_history
[Email protected] ~]# cat/root/.bash_history

The command history environment variable system set the maximum saving of the command, can be viewed by the following command;

[Email protected] ~]# echo $HISTSIZE
1000

In the current input command is only in memory, and for has been written to the command history file, through the following command, the current input (memory memory) of the command record can be deleted;

[Email protected] ~]# history-c

When you press the UP ARROW key at this point, you'll find that you don't see the command you entered earlier, and this action will not delete the contents of your command history.
If you feel that the maximum save 1000 command is not enough, you can change it in the configuration file in the following path;

[Email protected] ~]# Vi/etc/profile

Find in this line in the number of changes, here I will change the value:

You will need to re-enter the terminal if you want this value to take effect, or execute the following command;

[Email protected] ~]# Source/etc/profile
[Email protected] ~]# echo $HISTSIZE
5000

You can see that the value has been changed to.
At present, when we enter the history command, the display is only the line number and command, if you want to know the details of its operation time, we can change the following environment variables;

[Email protected] ~]# histtimeformat= "%y/%m/%d%h:%m:%s"
[Email protected] ~]# history
1 2018/01/10 18:07:50 Vi/etc/profile
2 2018/01/10 18:12:13 Cat/etc/profile
3 2018/01/10 18:13:55 Source/etc/profile
4 2018/01/10 18:14:49 Echo $HISTSIZE
5 2018/01/10 18:21:19 histtimeformat= "%y/%m/%d%h:%m:%s"
6 2018/01/10 18:21:28 history****

However, such operations are limited to the current, as long as the exit after re-entry and restore the original appearance, want to make it permanent, into the profile configuration file, the number of histsize under the value of the above parameter can be added. Do not forget to execute the source command after you modify it.

[Email protected] ~]# Vim/etc/profile
[Email protected] ~]# Source/etc/profile

If you want to permanently save the command history, then you can add hidden permissions to the file, like with a permission, so that can only increase can not be deleted, even if you do not set the maximum value it will not be deleted;

[Email protected] ~]# chattr +a ~/.bash_history

If the terminal is not properly shut down, or the terminal is restarted, the commands used before logging in will not be recorded in the command history file.

!! : Two exclamation mark for quick execution of the last command.
!n: n= (number), which is used to execute the first command in the history of a command, such as!400 is the command that executes No. 400 in the history of the command
!echo: An exclamation point after a command you, will be repeated in the command history of the last time distance history (and also the last execution of the command) here we take the echo command analogy.

Command completion and aliases

tab : Tap the following fill command, double-click to display the characters containing the current input of multiple commands, such as ls hit two to show a number of commands containing ls , when I enter the lsb Press tab the key will complete lsblk this command;

[[email protected] ~]# ls
LS lscgroup lsipc lsmcli LSNs lsscsi lsusb.py
Lsattr lscpu lslocks lsmd lsof Lssubsys
Lsblk lsinitrd lslogins lsmod lspci Lsusb
[Email protected] ~]# lsblk

The use of tab completion can not only complete the command, but also complete the file path, in the CENTOS6 when the system does not support the completion of the command parameters, the need to install the bash_completion package, And CENTOS7 is the system support incomplete parameters;

[[email protected] ~]# yum install-y bash_completion//After installation, the following systems need to be restarted before they take effect.

You can use the alias command to customize the command when you enter a command and you feel that the command has been used for a long time to reduce efficiency;

[[email protected] ~]# alias restartnet= ' systemctl restart Network.service '//than we will systemctl restart Network.service this command renamed to Restartnet

alias 's custom profile in the PROFILE.D under etc , there is also a place in the root user's home directory;

[Email protected] ~]# cd/etc/profile.d/
[[email protected] profile.d]# ls
256term.csh colorgrep.csh flatpak.sh less.sh vte.sh
256term.sh colorgrep.sh lang.csh packagekit.sh which2.csh
abrt-console-notification.sh colorls.csh lang.sh vim.csh which2.sh
bash_completion.sh colorls.sh less.csh vim.sh
[Email protected] profile.d]# LS/ROOT/.BASHRC
/root/.bashrc

unalias: Custom aliases are required

[Email protected] ~]# Unalias restartnet

Wildcard characters

[* * * * *:] When you use the asterisk to view, you will get the asterisk before or after the relevant documents, such as;

[[Email protected] document]# LS * * * * *.
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt test. txt
or a
[[Email protected] document]# LS test

Test 1.txt Test 2.txt Test 3.txt Test 4.txt test. txt

[? :] Use the question mark to view any file with a character name

[[Email protected] document]# ls?. Txt
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt

[0-9]: Using square brackets, can be listed in a range of files, such as 1-3;

[[Email protected] document]# LS [1-3].txt
1.txt 2.txt 3.txt

{n}: The use of curly braces and square brackets is similar, but the operation is slightly different. He uses "," to differentiate;

[[Email protected] document]# ls {1,2}.txt
1.txt 2.txt

Input and output re-set
[]: The greater than sign, indicating that the output of the former display of the content output is written to the following file (note that the files written to the latter file contents will be replaced);

[[Email protected] Document]# cat 1.txt
Hello World
[Email protected] Document]# cat 1.txt >2.txt
[[Email protected] Document]# cat 2.txt
Hello World

[>>:] Two greater than sign, indicating append, the former display of the contents of the latter to append to the file and will not replace the original file contents;

[Email protected] Document]# cat 1.txt >>2.txt
[[Email protected] Document]# cat 2.txt
Hello World
Hello World

[2>:] The greater than is preceded by a 2 to indicate the error output, the former execution of the error results output to the latter file;

[[Email protected] Document]# car 1.txt
Bash:car: Command not found ...
Similar commands are: ' SAR '
[[Email protected] Document]# car 1.txt 2>3.txt
[[Email protected] Document]# cat 3.txt
Bash:car: Command not found ...
Similar commands are: ' SAR '

[2>>:] Indicates an append output, which appends the error result of the former execution to the latter file;

[[Email protected] Document]# car 4.txt 2>>3.txt
[[Email protected] Document]# cat 3.txt
Bash:car: Command not found ...
Similar commands are: ' SAR '
Bash:car: Command not found ...
Similar commands are: ' SAR '

[&>:] combined with correct and incorrect output results, written to the latter file

[[Email protected] Document]# cat 1.txt 8.txt &>5.txt
[[Email protected] Document]# cat 5.txt
Hello World
Cat:8.txt: No file or directory

[>+2>:] A command can be executed at the same time the result of the correct and error output written to different files;

[[Email protected] Document]# cat 1.txt 8.txt >2.txt 2>3.txt
[[Email protected] Document]# cat 2.txt
Hello World
[[Email protected] Document]# cat 3.txt
Cat:8.txt: No file or directory

[<:] Output The following file to the previous command;

[[Email protected] document]# wc-l <5.txt
2

Shell Introduction/Command History/Command completion/wildcard/output input redirection

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.