• 设置 Linux 命令别名


      在操作 Linux 的时候可能会碰到一些非常长的命令,这个命令平时用的比较频繁,每次输入执行就会感觉非常麻烦。如果把一段长的命令设置成一个短的别名,就会很方便。Linux 的 alias 命令可以帮我们设置长命令的别名。

    一、服务器上的 alias 命令一览

      在 Linux 服务器上执行一下 alias 命令,可以看到几个很熟悉的命令。如下所示。

    [test@271ba307f4954c74955b28c8389bc648 ~]$ alias
    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 vi='vim'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

      例如:ll 命令不是 Linux 命令,ll 就是通过设置别名的方式执行 ls -l --color=auto

    二、alias 添加别名
    2.1 alias 添加别名语法

      alias 添加别名的语法为:alias [别名]='真实命令'

    2.2 alias 添加别名步骤

      如下所示,为当前 Linux 用户 test 添加命令别名 svccdl,目的是进入当天的日志文件路径,操作步骤如下。

    1. 进入当前用户的 home 路径下,执行 vi .bashrc,添加如下一行文本:
    alias svccdl='cd /home/test/logs/`date +%Y-%m-%d`'
    
    • 1

      编辑后的 .bashrc 文件内容如下所示。

    # .bashrc
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi
    alias svccdl='cd /home/test/logs/`date +%Y-%m-%d`'
    # Uncomment the following line if you don't like systemctl's auto-paging feature:
    # export SYSTEMD_PAGER=
    
    # User specific aliases and functions
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 执行 source .bashrc 使别名永久生效。
    [test@271ba307f4954c74955b28c8389bc648 ~]$ source .bashrc 
    
    • 1
    1. 使用测试
    [test@271ba307f4954c74955b28c8389bc648 ~]$ svccdl
    [test@271ba307f4954c74955b28c8389bc648 2022-08-30]$ pwd
    /home/test/logs/2022-08-30
    
    • 1
    • 2
    • 3
  • 相关阅读:
    大学生游戏静态HTML网页设计 (HTML+CSS+JS仿英雄联盟网站15页)
    Matlab 实用代码集
    SQL零基础入门教程,贼拉详细!贼拉简单! 速通数据库期末考!(十一)
    安卓应用自动化测试工具Appium实操分享
    [vue3] 菜单动态折叠效果
    需求分析简介
    数据库的三大范式(重要)
    pacemaker常用命令
    P1827 [USACO3.4] 美国血统 American Heritage(前序 + 中序 生成后序)
    基于postgis实现坐标转换的几个函数
  • 原文地址:https://blog.csdn.net/piaoranyuji/article/details/126607984