天天看点

007.Linux系统命令行入门1. 命令提示符2. 退出登陆状态3. 终端4. Linux关机重启命令5. 查看命令帮助6. Linux系统快捷键

1. 命令提示符

007.Linux系统命令行入门1. 命令提示符2. 退出登陆状态3. 终端4. Linux关机重启命令5. 查看命令帮助6. Linux系统快捷键
  • 作用:只有在命令提示符之后,才可以执行命令,比如,在

    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

    命令来查看当前窗口的终端
007.Linux系统命令行入门1. 命令提示符2. 退出登陆状态3. 终端4. Linux关机重启命令5. 查看命令帮助6. Linux系统快捷键
  • 终端与终端的通信
007.Linux系统命令行入门1. 命令提示符2. 退出登陆状态3. 终端4. Linux关机重启命令5. 查看命令帮助6. Linux系统快捷键
  • 系统级别的命令可以自动广播到全部终端
007.Linux系统命令行入门1. 命令提示符2. 退出登陆状态3. 终端4. Linux关机重启命令5. 查看命令帮助6. Linux系统快捷键
  • 使用

    wall

    命令手动广播消息
007.Linux系统命令行入门1. 命令提示符2. 退出登陆状态3. 终端4. Linux关机重启命令5. 查看命令帮助6. Linux系统快捷键

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

    :关闭系统并切断电源
  • systemctl halt

    :关闭系统,CPU停止工作
  • 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)           

复制

  • man

    UI中的快捷键
    • Page Down:向下翻页
    • Page Up:向上翻页
    • Home:跳转到第一页
    • End:跳转到最后一页
    • /:向下查找,例如

      /hello

      向下依次查找hello字符串
    • ?:向上查找,例如

      ?hello

      向上依次查找hello字符串
    • n,N:使用

      /

      ?

      查找出内容后,使用

      n

      跳转到同方向的下一个匹配到的字符串所在行,使用

      N

      跳转到反方向的上一个匹配到的字符串所在行
    • q:退出

      man

      UI

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 颠倒光标所在处及其相邻单词的位置