Bash 允许您创建终端别名以加快您的工作流程,减少所需的输入量,并使您的日常任务更有效率。 终端是 Linux 中一切的关键,我们都必须在旅途中的某个时候使用过它,要么是为了排除故障,要么只是为了看看漂亮的 Neofetch 输出。
但是,有时输入长命令可能会让人筋疲力尽,如果您忘记运行该命令,可能会浪费您的一些宝贵时间 sudo现在你必须重新输入一遍。
这就是别名的用武之地。Bash(或其替代品,如 Zsh、Fish、Ksh 等)是一个强大的工具,您可以通过它使某些单词或字母充当运行某些命令的“触发器”,从本质上为您节省大量时间。
在本文中,我们将向您展示如何修改 bashrc(或 Zshrc 等)以为您的操作系统添加某些别名。
目录
识别正在使用的外壳
大多数发行版都默认使用 Bash,但是 Manjaro 使用 zsh 作为默认 shell。 在终端中键入以下命令以了解您使用的是哪个 shell:
ps $$
或键入以下内容
grep `whoami` /etc/passwd | cut -d':' -f7
您将知道您使用的是哪个外壳。
备份你的 .bashrc
在我们更改任何内容之前,请备份您的原始 Bashrc,以便如果出现任何问题,您可以随时返回它。 打开终端并键入以下命令:
cp ~/.bashrc ~/.bashrc.bak
修改 .bashrc 文件添加终端别名
如果您有默认的文本编辑器 nano,请打开终端并键入以下命令:
nano .bashrc
走到最后,按回车,我们现在可以开始添加别名了。
分布独立终端别名
无论您拥有哪种发行版,您都可以添加这些别名,它们在每个发行版中都可以正常工作。
# Power related commands alias shutdown="sudo shutdown -P now" alias reboot="sudo shutdown -r now" # Colorize Grep output alias grep="grep --color=auto" # ls command alias ls="ls -C --color=auto" alias lm="ls -lhA --color=auto | more" # For big folders and Hidden files alias ll="ls -lh --color=auto" # To show permissions of files # Make mount command output pretty and readable alias mount="mount |column -t" # Stop after sending 5 pings alias ping='ping -c 5' # A quick way to get out of current directory alias ..='cd ..' alias ...='cd ../../../' alias ....='cd ../../../../' alias .....='cd ../../../../' alias .4='cd ../../../../' alias .5='cd ../../../../..' # Date and Time alias now="date +"%T"" alias nowtime=now alias nowdate="date +"%d-%m-%Y"" # Miscellaneous alias mkdir="mkdir -pv" # Creates parent directories if needed alias hist="history" alias home="cd ~/" alias root="sudo su" alias rm="rm -i" # Interactive mode delete alias diskspace="du -S | sort -n -r |more" # To find out what’s taking so much space # To show size of all the folders in the current directory alias folders="find . -maxdepth 1 -type d -print | xargs du -sk | sort -rn" # Add this to easily extract compressed files, use extract <filename> to extract, this will only work if you have all the packages installed extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xvf $1 ;; *.tbz2) tar xvjf $1 ;; *.tgz) tar xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "don't know how to extract '$1'..." ;; esac else echo "'$1' is not a valid file!" fi }
按 Ctrl + O 保存文件,然后按 Ctrl + X 退出。 现在,要应用这些更改,您可以随时重新启动终端应用程序,但您也可以键入以下内容以应用更改:
source .bashrc
Linux 的发行版特定终端别名
从现在开始,选择您的发行版并相应地添加别名。
Ubuntu 和基于 Ubuntu 的发行版
再次打开终端,然后输入
nano .bashrc
导航到最后一个并添加以下脚本:
# Update alias update="sudo apt update && sudo apt full-upgrade" alias remove="sudo apt-get autoremove"
Arch 和基于 Arch 的发行版
打开 .bashrc 文件并将以下脚本复制并粘贴到文件末尾:
# Update alias update="sudo pacman -Syu” # Autoremove alias remove="sudo pacman -Rns"
Save 文件并退出
RHEL 和基于 RHEL 的发行版
再次打开 .bashrc 并在文件末尾键入以下脚本:
# Update alias update="sudo yum update”
Save 文件并退出。

概括
我们希望这些 Bashrc 修改能够为您节省大量时间,并且您不必再在终端中键入长命令。 你可以随时添加新的别名,只要确保你总是有一个备份,以防你搞砸了。