Linux(9)、bash相关常用命令

shell & bash 介绍

  • shell

    俗称 壳,它是指UNIX系统下的一个命令解析器。主要用于用户和系统的交互。UNIX系统上有很多种Shell。首个shell,即Bourne Shell,于1978年在V7(AT&T的第7版)UNIX上推出。后来,又演变出C shell、bash等不同版本的shell。
    Shell 既是一种命令语言,又是一种程序设计语言。Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务

  • bash

    全称为Bourne-Again Shell。它是一个为GNU项目编写的Unix shell。bash脚本功能非常强大,尤其是在处理自动循环或大的任务方面可节省大量的时间。bash是许多Linux平台的内定Shell,这也是我们介绍它主要的原因。

    类别:
    • GUI(graphical user interface 图形化用户接口): Gnome、KDE、Xface
    • CLI(command line interface命令行接口): sh、bash、cshell、kshell,

bash常用命令

命令行光标跳转

ctrl+a      # 跳到命令行首
ctrl+e      # 跳到命令行尾 
ctrl+u      # 删除光标至行首
ctrl+k      # 删除光标只行尾
ctrl+l      # 清屏

history

用户命令历史会缓存在文件中

-c          # 清空命令历史
-d600       # 删除第600个命令
-d600 10    # 指定偏移量10,删除第600-611行命令
!n        # 打印命令历史第n条记录并显示执行结果
!-n        # 倒数第n条命令
!!         # 执行上一条命令
!string    # 执行命令历史中最近一个以指定字符串开头的命令

环境变量

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@localhost ~]# echo $HISTSIZE
1000

命令自动补全

  • tab自动补全
  • 双击tab显示所有匹配的

命令别名alias

  • 当前进程有效

    alias cls=clear
    alias rm="rm -i"
  • 永久生效:
    • 1、切换root用户:sudo -i pwd
    • 2、修改/root/.bashrc,增加命名内容
    • 3、生效文件:source /root/.bashrc
  • 解除别名: unalias或直接进入文件中修改。

命令替换

把命令中某个子命令替换为其执行的结果

# 方式 1
[root@localhost ~]# echo the current directory is $(pwd)
the current directory is /root

# 方式 2
[root@localhost ~]# echo the current directory is `pwd`
the current directory is /root


touch file-$(date +%d).txt

bash支持的引号

``      # 命令替换
[root@localhost ~]# echo my name is `pwd`
my name is /root


""      # 弱引用,可以实现变量替换
[root@localhost ~]# echo my name is "`pwd`"
my name is /root


''      # 强引用,不能完成变量替换
[root@localhost ~]# echo my name is '`pwd`'
my name is `pwd`

文件名通配globbing

在利用find查询 文件 或 目录 时候使用。

  • *:代表任意长度的任意字符

    # 查找名称以 .txt 结尾的文件
    [root@localhost ~]# find -type f -name '*.txt'
    ./2.txt
    ./3.txt
    ./file-07.txt
    ./redis-4.0.11/deps/jemalloc/include/jemalloc/internal/private_symbols.txt
    ./redis-4.0.11/deps/jemalloc/include/jemalloc/internal/public_symbols.txt
    ./1.txt
    
    
    # 查找名称以 d 开头的目录
    [root@localhost ~]# find -type d -name 'd*'
    ./redis-4.0.11/deps
    ./redis-4.0.11/deps/jemalloc/doc
    ./redis-4.0.11/deps/lua/doc
    
    
    # 列出以1开头的文件
    [root@localhost fangqihan]# ls 1*
    1.txt
    
    [root@localhost fangqihan]# ls d?
    d1:
    1.txt  3.txt  c.txt
    
    d2:
    2.txt
  • ?:代表任意字符,只能代表一个字符

    [root@localhost fangqihan]# find -type d -name 'd?'
    ./d1
    ./d2
  • []:匹配指定范围内的任意单个字符

    [root@localhost fangqihan]# find -type d -name 'd[0-9]'
    ./d1
    ./d2
  • [:space:]
  • [:punct:]
  • [:lower:]:匹配单个小写字母

    [root@localhost fangqihan]# ls [[:lower:]]?
    d1:
    1.txt  3.txt  c.txt
    
    d2:
    2.txt
  • [:upper:]
  • [:alnum:]:匹配单个字母或数字

    [root@localhost fangqihan]# ls d[[:alnum:]]
    d1:
    1.txt  3.txt  c.txt
    
    d2:
    2.txt
  • [:alpha:]:匹配单个字母

  • [^]:匹配指定范围之外的任意单个字符

    [root@localhost fangqihan]# find -type d -name 'd[^2-9]'
    ./d1
  • $:换行符

猜你喜欢

转载自www.cnblogs.com/fqh202/p/9436930.html