美文网首页
Linux基础操作一

Linux基础操作一

作者: RussellYoung | 来源:发表于2018-03-17 21:50 被阅读0次

    1,查看系统支持的shell类型:

    [root@centos6 ~]# cat /etc/shells  
    /bin/sh
    /bin/bash
    /sbin/nologin
    /bin/dash
    /bin/tcsh
    /bin/csh 
    

    2,查看我的系统用的是哪种shell类型:

    [root@centos6 ~]# echo $SHELL
    /bin/bash
    [root@centos6 ~]# 
    

    3,查看完整的主机名称:

    [root@centos6 ~]# echo $SHELL
    /bin/bash
    [root@centos6 ~]#   //波浪符表示目前在哪个文件夹下
    

    4,查看bash具体信息:

    [root@centos6 ~]#ll /bin/bash
    -rwxr-xr-x. 1 root root 906568 Mar 23  2017 /bin/bash  #可以看到,bash大小有906568KB,里面集成了很多内部命令等文件
    [root@centos6 ~]#ll /bin/ls
    -rwxr-xr-x. 1 root root 109208 Mar 23  2017 /bin/ls  #ls命令在bin下,是外部命令
    

    5,查看全部内部命令:

    方法一:

    [root@centos6 ~]#enable
    enable .
    enable :
    enable [
    enable alias  #从这里开始
    enable bg
    enable bind
    enable break
    enable builtin
    enable caller
    enable cd
    enable command
    enable compgen
    enable complete
    enable compopt
    enable continue
    enable declare
    enable dirs
    enable disown
    enable echo
    enable enable
    enable eval
    enable exec
    enable exit
    enable export
    enable false
    enable fc
    enable fg
    enable getopts
    enable hash
    enable help
    enable history
    enable jobs
    enable kill
    enable let
    enable local
    enable logout
    enable mapfile
    enable popd
    enable printf
    enable pushd
    enable pwd
    enable read
    enable readarray
    enable readonly
    enable return
    enable set
    enable shift
    enable shopt
    enable source
    enable suspend
    enable test
    enable times
    enable trap
    enable true
    enable type
    enable typeset
    enable ulimit
    enable umask
    enable unalias
    enable unset
    enable wait
    

    方法二:

    [root@centos7 ~]# help
    GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
    These shell commands are defined internally.  Type `help' to see this list.
    Type `help name' to find out more about the function `name'.
    Use `info bash' to find out more about the shell in general.
    Use `man -k' or `info' to find out more about commands not in this list.
    
    A star (*) next to a name means that the command is disabled.
    
     job_spec [&]                                                                                   history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
     (( expression ))                                                                               if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
     . filename [arguments]                                                                         jobs [-lnprs] [jobspec ...] or jobs -x command [args]
     :                                                                                              kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
     [ arg... ]                                                                                     let arg [arg ...]
     [[ expression ]]                                                                               local [option] name[=value] ...
     alias [-p] [name[=value] ... ]                                                                 logout [n]
     bg [job_spec ...]                                                                              mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
     bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-c>  popd [-n] [+N | -N]
     break [n]                                                                                      printf [-v var] format [arguments]
     builtin [shell-builtin [arg ...]]                                                              pushd [-n] [+N | -N | dir]
     caller [expr]                                                                                  pwd [-LP]
     case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac                                     read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout>
     cd [-L|[-P [-e]]] [dir]                                                                        readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]>
     command [-pVv] command [arg ...]                                                               readonly [-aAf] [name[=value] ...] or readonly -p
     compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat] [-W wordlist]  [-F function] [>  return [n]
     complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist]  [-F>  select NAME [in WORDS ... ;] do COMMANDS; done
     compopt [-o|+o option] [-DE] [name ...]                                                        set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
     continue [n]                                                                                   shift [n]
     coproc [NAME] command [redirections]                                                           shopt [-pqsu] [-o] [optname ...]
     declare [-aAfFgilrtux] [-p] [name[=value] ...]                                                 source filename [arguments]
     dirs [-clpv] [+N] [-N]                                                                         suspend [-f]
     disown [-h] [-ar] [jobspec ...]                                                                test [expr]
     echo [-neE] [arg ...]                                                                          time [-p] pipeline
     enable [-a] [-dnps] [-f filename] [name ...]                                                   times
     eval [arg ...]                                                                                 trap [-lp] [[arg] signal_spec ...]
     exec [-cl] [-a name] [command [arguments ...]] [redirection ...]                               true
     exit [n]                                                                                       type [-afptP] name [name ...]
     export [-fn] [name[=value] ...] or export -p                                                   typeset [-aAfFgilrtux] [-p] name[=value] ...
     false                                                                                          ulimit [-SHacdefilmnpqrstuvx] [limit]
     fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]                               umask [-p] [-S] [mode]
     fg [job_spec]                                                                                  unalias [-a] name [name ...]
     for NAME [in WORDS ... ] ; do COMMANDS; done                                                   unset [-f] [-v] [name ...]
     for (( exp1; exp2; exp3 )); do COMMANDS; done                                                  until COMMANDS; do COMMANDS; done
     function name { COMMANDS ; } or name () { COMMANDS ; }                                         variables - Names and meanings of some shell variables
     getopts optstring name [arg]                                                                   wait [id]
     hash [-lr] [-p pathname] [-dt] [name ...]                                                      while COMMANDS; do COMMANDS; done
     help [-dms] [pattern ...]                                                                      { COMMANDS ; }
    

    6,查看某命令是外部命令还是内部命令:

    [root@centos6 ~]#type cd  
    cd is a shell builtin    #内部
    [root@centos6 ~]#type ls  
    ls is aliased to `ls --color=auto'   #外部
    [root@centos6 ~]#type hostname  
    hostname is /bin/hostname    #外部
    [root@centos6 ~]#type type  
    type is a shell builtin     #内部
    [root@centos6 ~]#type enable
    enable is a shell builtin     #内部
    注:内部命令保存在内存上,外部命令是磁盘文件,所以外部命令的执行效率不如内部命令
    

    7,查看命令地址:

    [root@centos6 ~]#which ls
    alias ls='ls --color=auto'
        /bin/ls
    [root@centos6 ~]#whereis ls  
    ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
    #whereis命令显示的要比which命令更详细一些,不仅显示了该命令的所在路径,还显示了命令相关文档的路径
    

    8,哈希表:将外部命令的路径保存到了哈希表中,下次执行时可以直接访问,下面是已经保存到哈希表中的路径

    [root@centos6 ~]#hash
    hits    command
       1    /usr/bin/whereis
       2    /bin/ls
    

    9,命令别名:用alias命令

    [root@centos6 ~]#cd /etc/sysconfig/network-scripts/
    [root@centos6 network-scripts]#cd
    [root@centos6 ~]#alias cdnet="cd /etc/sysconfig/network-scripts/"
    [root@centos6 ~]#cdnet
    [root@centos6 ~]#nano .bashrc  //把刚才做出的命令别名改动保存到系统文件中,使得重新登录系统时依然有效
    
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias cdnet="cd /etc/sysconfig/network-scripts/"  //将命令写入文件中,保存生效
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi
    
    ^G Get Help   ^O WriteOut       ^R Read File      ^Y Prev Page  ^K Cut Text   ^C Cur Pos
    ^X Exit           ^J Justify        ^W Where Is       ^V Next Page  ^U UnCut Text     ^T To Spell
    [root@centos6 ~]. .bashrc  //让别名立即生效
    [root@centos6 ~]#unalias cdnet  //取消别名
    [root@centos6 ~]#cdnet
    -bash: cdnet: command not found  //取消别名生效
    [root@centos6 ~]#alias   //查看所有的命令别名
    alias cp='cp -i'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    [root@centos6 ~]#
    注:如果想永久有效,要定义在配置文件中
    仅对当前用户:~/.bashrc
    对所有用户有效:/etc/bashrc  //上面的操作是对所有用户有效
    

    10,让原始命令发挥作用的方法

    [root@centos6 ~]#alias hostname="ls"
    [root@centos6 ~]#hostname
    anaconda-ks.cfg  Documents  install.log         Music     Public     Videos
    Desktop          Downloads  install.log.syslog  Pictures  Templates
    [root@centos6 ~]#"hostname"  //方法一
    centos6.young.magedu
    [root@centos6 ~]#'hostname'   //方法二
    centos6.young.magedu
    [root@centos6 ~]#\hostname   //方法三
    centos6.young.magedu
    [root@centos6 ~]#command hostname  //方法四
    centos6.young.magedu
    [root@centos6 ~]#
    注:命令别名要比原始命令的优先级高
    

    11,查看时间:

    [root@centos6 ~]#clock  //主板上的时间,硬件时间
    Thu 08 Mar 2018 11:13:09 AM CST  -0.635436 seconds
    [root@centos6 ~]#date   //Linux内核时间
    Thu Mar  8 11:13:15 CST 2018
    [root@centos6 ~]#date 030811162018.30  //修改时间
    Thu Mar  8 11:16:30 CST 2018
    [root@centos6 ~]#clock -w   //更新时间
    [root@centos6 ~]#clock
    Thu 08 Mar 2018 11:16:52 AM CST  -0.876316 seconds
    

    12,命令提示符:prompt

    [root@localhost ~]#
    注: “# ”表示管理员 “$”表示普通用户

    [1]显示提示符格式
    [root@centos7 ~]# echo $PS1
    [\u@\h \W]\$
    
    [2]修改提示符格式

    PS1="\[\e[1;5;41;33m\][\u@\h \W]\\$\[\e[0m\]" 该命令提示符修改该后为红底黄字闪烁
    \e \033 \u 当前用户
    \h 主机名简称
    \H 主机名
    \w 当前工作目录
    \W 当前工作目录基名
    \t 24小时时间格式
    \T 12小时时间格式
    ! 命令历史数
    # 开机后命令历史数

    [3] 保存修改后的提示符格式:

    方法一、在虚拟机终端中输入命令
    geit .bash_profile
    进入图形化编辑页面,将PS1="\[\e[1;5;41;33m\][\u@\h \W]\\$\[\e[0m\]"输入进去,点击“Save”
    方法二、输入命令:
    nano .bash_profile
    将上面命令复制进去保存

    相关文章

      网友评论

          本文标题:Linux基础操作一

          本文链接:https://www.haomeiwen.com/subject/qjksqftx.html