1. 指令提示符

- 作用:隻有在指令提示符之後,才可以執行指令,比如,在
的程序中輸入指令是無法執行的tail -f
- 檢視指令提示符中的資訊的相關指令
# 檢視目前登陸使用者
[root@node00 ~]# whoami
root
# 檢視主機名
[root@node00 ~]# hostname
node00
# 檢視目前所在的位置
[root@node00 ~]# pwd
/root
複制
- PS1環境變量
指令提示符由PS1環境變量控制
# 預設的PS1設定
[root@node00 ~]# echo $PS1
[\u@\h \W]\$
複制
- 可以通過修改
檔案的内容來設定提示資訊,支援的配置如下:/etc/bashrc
PS1變量 | 含義 |
---|---|
\d | 代表日期,格式為weekday month day,例如"Mon Aug 1" |
\H | 完整的主機名 |
\h | 僅取主機的第一個名字 |
\t | 顯示24小時制的時間,格式為HH:mm:ss |
\T | 顯示12小時制的時間,格式為hh:mm:ss |
\A | 顯示24小時制的時間,格式為HH:mm |
\u | 目前使用者的名稱 |
\v | BASH版本資訊 |
\w | 顯示完整的路徑,家目錄用"~"代替 |
\W | 利用basename擷取工作目錄名稱,隻會列出最後一個目錄 |
\# | 執行的第幾個指令 |
\$ | 提示字元,如果是root,則提示符為#,普通使用者為$ |
- 臨時設定PS1變量
[root@node00 ~]# PS1='[\u@\h \w]\$ '
[root@node00 ~]# cd /etc/sysconfig/network-scripts/
# \w控制的顯示全部路徑已經生效了
[root@node00 /etc/sysconfig/network-scripts]#
複制
- 永久設定PS1變量
編輯
/etc/bashrc
檔案
# 這是檔案中預設的内容
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
# 修改為你自己喜歡的格式,一個美觀的PS1變量的格式如下
PS1="[\[\[\e[34;1m\]\t \[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\h\[\e[0m\]\[\e[31;1m\] \W\[\e[0m\]]\\$ "
# 儲存退出之後
source /etc/bashrc
複制
- 指令格式
# command [arg] [path]
# 名令 可選的參數選項(可選) 檔案或目錄(可選)
rm -rf /tmp/test.txt
# 名令和參數、參數和要操作的檔案或目錄之間必須有至少一個空格
複制
2. 退出登陸狀态
- exit
- logout
- crtl + d
3. 終端
- 每次打開一個指令行視窗,都是開啟一個新的終端,包括遠端連接配接的視窗,使用
指令來檢視目前視窗的終端tty
- 終端與終端的通信
- 系統級别的指令可以自動廣播到全部終端
- 使用
指令手動廣播消息wall
4. Linux關機重新開機指令
4.1 shutdown
shutdown
指令可以安全的關閉或者重新開機系統,并廣播關機或者重新開機的消息給全部的終端
# 預設在一分鐘之後關機
[root@template ~]# shutdown
# 系統計劃在2020-04-05 22:02:11關機,使用'shutdown -c'取消這個計劃
Shutdown scheduled for Sun 2020-04-05 22:02:11 CST, use 'shutdown -c' to cancel.
# 來自root@template的廣播消息,系統将在2020-04-05 22:02:11關機
# 消息将會廣播給目前連接配接系統的所有終端
Broadcast message from root@template (Sun 2020-04-05 22:01:11 CST):
The system is going down for power-off at Sun 2020-04-05 22:02:11 CST!
# 5分鐘之後關機,最大支援15min
shutdown -h 5
# 在14:00關機,也是最多支援15min的延遲
shutdown -h 14:00
# 立刻關機
shutdown -h now
# 立刻關機
shutdown -h 0
# 同理,以下為重新開機的時間設定
shutdown -r 5
shutdown -r 14:00
shutdown -r now
shutdown -r 0
複制
shutdown
指令的參數清單:
-r # 重新開機系統
-h # 關閉系統,可以再後面指定時間
-H # 關閉系統,不常用
-P # 關閉系統,不常用
-c # 取消正在執行的shutdown指令
-k # 隻發送關機消息給所有終端,但并不會真正關機,極少使用
複制
4.2 halt
- 立即關機
4.3 poweroff
- 立即關機
4.4 reboot
- 立即重新開機
4.5 init
-
:立即關機init 0
-
:立即重新開機init 6
4.6 CentOS7的關機、重新開機指令
shutdown
、
reboot
、
poweroff
、
halt
在CentOS7中實際上是
systemctl
指令的參數,CentOS7還有以下系統關機重新開機相關的指令:
-
:重新開機系統systemctl reboot
-
:關閉系統并切斷電源systemctl poweroff
-
:關閉系統,CPU停止工作systemctl halt
-
:暫停系統運作systemctl suspend
-
:系統休眠systemctl hibernate
-
:系統進入互動式休眠狀态systemctl hybrid-sleep
-
:啟動救援狀态systemctl rescue
5. 檢視指令幫助
5.1 man
-
指令使用man
[14:36:59 root@node00 ~]# man cp
# CP(1)
# 括号中的數字可以包括1-8
# 1 使用者指令相關
# 2 系統調用相關
# 3 C的庫函數相關
# 4 裝置和特殊檔案相關
# 5 檔案的格式和規則
# 6 遊戲及其他
# 7 宏、包及其他雜項
# 8 系統管理者指令和程序
CP(1) User Commands CP(1)
# 指令說明及介紹
NAME
cp - copy files and directories
# 指令的基于文法
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
# 指令的較長的描述
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive
same as -dR --preserve=all
--attributes-only
don't copy the file data, just the attributes
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents
copy contents of special files when recursive
-d same as --no-dereference --preserve=links
-f, --force
if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option
is also used)
-i, --interactive
prompt before overwrite (overrides a previous -n option)
-H follow command-line symbolic links in SOURCE
-l, --link
hard link files instead of copying
-L, --dereference
always follow symbolic links in SOURCE
-n, --no-clobber
do not overwrite an existing file (overrides a previous -i option)
-P, --no-dereference
never follow symbolic links in SOURCE
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context,
links, xattr, all
-c deprecated, same as --preserve=context
--no-preserve=ATTR_LIST
don't preserve the specified attributes
--parents
use full source file name under DIRECTORY
-R, -r, --recursive
copy directories recursively
--reflink[=WHEN]
control clone/CoW copies. See below
--remove-destination
remove each existing destination file before attempting to open it (contrast with --force)
--sparse=WHEN
control creation of sparse files. See below
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-s, --symbolic-link
make symbolic links instead of copying
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose
explain what is being done
-x, --one-file-system
stay on this file system
-Z set SELinux security context of destination file to default type
--context[=CTX]
like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
--help display this help and exit
--version
output version information and exit
By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well.
That is the behavior selected by --sparse=auto. Specify --sparse=always to create a sparse DEST file whenever the SOURCE
file contains a long enough sequence of zero bytes. Use --sparse=never to inhibit creation of sparse files.
When --reflink[=always] is specified, perform a lightweight copy, where the data blocks are copied only when modified. If
this is not possible the copy fails, or if --reflink=auto is specified, fall back to a standard copy.
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via
the --backup option or through the VERSION_CONTROL environment variable. Here are the values:
none, off
never make backups (even if --backup is given)
numbered, t
make numbered backups
existing, nil
numbered if numbered backups exist, simple otherwise
simple, never
always make simple backups
As a special case, cp makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same
name for an existing, regular file.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report cp translation bugs to <http://translationpro‐
ject.org/team/>
# 指令的作者
AUTHOR
Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.
# 版權資訊
COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
# 指令的相關資訊說明
SEE ALSO
The full documentation for cp is maintained as a Texinfo manual. If the info and cp programs are properly installed at
your site, the command
info coreutils 'cp invocation'
should give you access to the complete manual.
GNU coreutils 8.22 August 2019 CP(1)
Manual page cp(1) line 131/161 (END) (press h for help or q to quit)
複制
-
UI中的快捷鍵man
- Page Down:向下翻頁
- Page Up:向上翻頁
- Home:跳轉到第一頁
- End:跳轉到最後一頁
- /:向下查找,例如
向下依次查找hello字元串/hello
- ?:向上查找,例如
向上依次查找hello字元串?hello
- n,N:使用
和/
查找出内容後,使用?
跳轉到同方向的下一個比對到的字元串所在行,使用n
跳轉到反方向的上一個比對到的字元串所在行N
- q:退出
UIman
5.2 command --help
使用
command --help
來檢視該指令的幫助資訊,例如
cp --help
:
[14:51:37 root@node00 ~]# cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
or: cp [OPTION]... SOURCE... DIRECTORY
or: cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive same as -dR --preserve=all
--attributes-only don't copy the file data, just the attributes
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents copy contents of special files when recursive
-d same as --no-dereference --preserve=links
-f, --force if an existing destination file cannot be
opened, remove it and try again (this option
is ignored when the -n option is also used)
-i, --interactive prompt before overwrite (overrides a previous -n
option)
-H follow command-line symbolic links in SOURCE
-l, --link hard link files instead of copying
-L, --dereference always follow symbolic links in SOURCE
-n, --no-clobber do not overwrite an existing file (overrides
a previous -i option)
-P, --no-dereference never follow symbolic links in SOURCE
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST] preserve the specified attributes (default:
mode,ownership,timestamps), if possible
additional attributes: context, links, xattr,
all
-c deprecated, same as --preserve=context
--no-preserve=ATTR_LIST don't preserve the specified attributes
--parents use full source file name under DIRECTORY
-R, -r, --recursive copy directories recursively
--reflink[=WHEN] control clone/CoW copies. See below
--remove-destination remove each existing destination file before
attempting to open it (contrast with --force)
--sparse=WHEN control creation of sparse files. See below
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-s, --symbolic-link make symbolic links instead of copying
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-x, --one-file-system stay on this file system
-Z set SELinux security context of destination
file to default type
--context[=CTX] like -Z, or if CTX is specified then set the
SELinux or SMACK security context to CTX
--help display this help and exit
--version output version information and exit
By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well. That is the behavior
selected by --sparse=auto. Specify --sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.
When --reflink[=always] is specified, perform a lightweight copy, where the
data blocks are copied only when modified. If this is not possible the copy
fails, or if --reflink=auto is specified, fall back to a standard copy.
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:
none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups
As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cp invocation'
# 設定系統字元集之後,可以檢視中文描述
[14:54:27 root@node00 ~]# LANG="zh_CN.UTF-8"
[14:55:12 root@node00 ~]# cp --help
用法:cp [選項]... [-T] 源檔案 目标檔案
或:cp [選項]... 源檔案... 目錄
或:cp [選項]... -t 目錄 源檔案...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive 等于-dR --preserve=all
--attributes-only 僅複制屬性而不複制資料 --backup[=CONTROL 為每個已存在的目标檔案建立備份
......
複制
5.3 help
bash程式的内置指令無法使用
command --help
來擷取幫助,需要使用
help command
:
[14:57:53 root@node00 ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status
The default is to follow symbolic links, as if `-L' were specified.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
複制
5.4 info
info command
可以提供一份最詳細的關于該指令的介紹,通常是用不到的。
6. Linux系統快捷鍵
快捷鍵 | 說明 |
---|---|
tab | 補全指令或者路徑 |
Ctrl + a | 光标回到指令行行首 |
Ctrl + e | 光标回到指令行行尾 |
Ctrl + → | 光标以單詞為機關向右跳動 |
Ctrl + ← | 光标以單詞為機關向左跳動 |
Ctrl + f | 光标向右移動一個字元 |
Ctrl + b | 光标向左移動一個字元 |
Ctrl + Insert | 複制選中内容 |
Shift + Insert | 粘貼選中内容 |
Ctrl + k | 剪切光标處到行尾的内容 |
Ctrl + u | 剪切光标處到行首的内容 |
Ctrl + w | 剪切光标前的一個單詞 |
Ctrl + y | 粘貼Ctrl + k、Ctrl + u、Ctrl + w剪切的内容 |
Ctrl + c | 終止正在執行的前台任務或者删除整行 |
Ctrl + h | 删除光标所在處的前一個字元,同Backspace |
Ctrl + d | 删除光标所在處的後一個字元,同Delete |
Ctrl + d | 當指令行無字元的時候,退出目前終端 |
Ctrl + r | 搜尋執行過的指令 |
Ctrl + g | 從搜尋曆史指令的UI中退出 |
Esc + . | 擷取上一條指令使用空格分隔後的最後一部分 |
Ctrl + l | 清除螢幕内容 |
Ctrl + s | 鎖定終端 |
Ctrl + q | 解鎖終端 |
Ctrl + z | 暫停執行在終端運作任務 |
!! | 執行上一條指令 |
!pw | 執行最近以pw開頭的指令 |
!pw:p | 列印最近以pw開頭的指令,但不執行 |
!num | 執行曆史指令清單中第num條指令 |
!$ | 同Esc + . |
Esc + b | 移動到目前單詞的開頭 |
Esc + f | 移動到目前單詞的結尾 |
Esc + t | 颠倒光标所在處及其相鄰單詞的位置 |