侧边栏壁纸
博主头像
小周的个人博客 博主等级

行动起来,活在当下

  • 累计撰写 90 篇文章
  • 累计创建 11 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录

命令管理命令

Administrator
2026-07-21 / 0 评论 / 0 点赞 / 0 阅读 / 0 字

1、alias别名管理

  • alias命令用于设置命令别名,用户可以使用alias自定义命令别名来简化命令的复杂度

  • .bashrc 文件存放命令别名

  • 命令格式:aliasi [别名]=[命令] #注意事项:等号(=)前后不能有空格

  • unalias 别名 #取消别名

#定义别名
[root@test ~]# alias lsnet='ls /etc/sysconfig/network-scripts/'
[root@test ~]# lsnet
[root@test ~]# alias myls='ls -ldh'
[root@test ~]# myls /opt

#查看当前系统可用命令别名
[root@test ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias lsnet='ls /etc/sysconfig/network-scripts/'
alias mv='mv -i'
alias myls='ls -ldh'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --showtilde'

#两条命令效果相同
[root@test ~]# ls -l hello
-rw-r--r--. 1 root root 426 3月 28 15:00 hello
[root@test ~]# ll hello
-rw-r--r--. 1 root root 426 3月 28 15:00 hello
[root@test ~]# which ls
alias ls='ls --color=auto'
/usr/sbin/ls
[root@test ~]# /usr/sbin/ls
[root@test ~]# ls

#取消本次命令的别名功能“\”
[root@test ~]# \ls

#取消命令别名
[root@test ~]# unalias myls
[root@test ~]# myls
bash: myls: 未找到命令...

#定义别名不要跟系统命令发生冲突
[root@test ~]# alias ls=hostname
[root@test ~]# ls
test

#取消命令别名
[root@test ~]# unalias ls
[root@test ~]# alias

#重新定义别名
[root@test ~]# alias ls='ls --color=auto'
[root@test ~]# ls

2、history 管理命令历史

  • history命令用于显示历史记录和执行过的命令,登录系统时会读取.bash_history历史文件中记录 下的命令,当退出时,会自动保存到历史命令文件,该命令单独使用时,仅显示历史命令

  • 历史命令默认只能存储1000条,可以通过/etc/profile文件修改

  • 命令格式:history [-选项] [参数]

  • 常用选项:

    • -a 追加本次新执行的命令至历史命令文件中

    • -d 删除历史命令中指定的命令

    • -c 清空历史命令列表

  • 快捷操作:

    • !# 调用命令历史中第N条命令

    • !string 调用命令历史中最近一次以strind开头的命令

    • !! 重复执行上一条命令

#获取命令帮助
[root@test ~]# help history

#查看历史命令
[root@test ~]# history

#查看记录历史命令文件
[root@test ~]# cat .bash_history

#将历史命令同步至历史命令配置文件中
[root@test ~]# history -a
[root@test ~]# cat .bash_history

#删除历史命令中655条命令历史
[root@test ~]# history -d 655
[root@test ~]# history -d 637

#清空缓存中所有历史命令
[root@test ~]# history -c
[root@test ~]# history
1 history

#删除历史命令配置文件(该文件删除后系统会再次自动创建)
[root@test ~]# rm -rf .bash_history

#快速调用历史命令中第1条
[root@test ~]# !1
[root@test ~]# !3

#调用历史命令中以cat开头的命令(只调用最近使用的cat历史命令)
[root@test ~]# !cat

#重复执行上一条命令
[root@test ~]# !!

#历史命令默认只能记录1000条,可以通过/etc/profile文件修改
[root@test ~]# vim /etc/profile
...
46 HISTSIZE=100

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区