translate done: 20120611 30 Handy Bash Shell Aliases For Linux - Unix - Mac OS X.md

This commit is contained in:
darksun 2018-01-08 22:36:47 +08:00
parent ad031db1ec
commit c6a426a6b4

View File

@ -1,20 +1,22 @@
translating by lujun9972
30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
Linux / Unix / Mac OS X 中的 30 个方便的 Bash shell 别名
======
An bash alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to [~/.bashrc][1] file. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.
bash 别名不是把别的,只不过是指向命令的快捷方式而已。`alias` 命令允许用户只输入一个单词就运行任意一个命令或一组命令(包括命令选项和文件名)。执行 `alias` 命令会显示一个所有已定义别名的列表。你可以在 [~/.bashrc][1] 文件中自定义别名。使用别名可以在命令行中减少输入的时间,使工作更流畅,同时增加生产率。
This post shows how to create and use aliases including 30 practical examples of bash shell aliases.
[![30 Useful Bash Shell Aliase For Linux/Unix Users][2]][2]
本文通过 30 个 bash shell 别名的实际案例演示了如何创建和使用别名。
## More about bash alias
![30 Useful Bash Shell Aliase For Linux/Unix Users][2]
The general syntax for the alias command for the bash shell is as follows:
## bash alias 的那些事
### How to list bash aliases
bash shell 中的 alias 命令的语法是这样的:
Type the following [alias command][3]:
`alias`
Sample outputs:
### 如何列出 bash 别名
输入下面的 [alias 命令 ][3]
```
alias
```
结果为:
```
alias ..='cd ..'
alias amazonbackup='s3backup'
@ -23,11 +25,11 @@ alias apt-get='sudo apt-get'
```
By default alias command shows a list of aliases that are defined for the current user.
默认 alias 命令会列出当前用户定义好的别名。
### How to define or create a bash shell alias
### 如何定义或者说创建一个 bash shell 别名
To [create the alias][4] use the following syntax:
使用下面语法 [创建别名 ][4]
```
alias name =value
alias name = 'command'
@ -36,22 +38,19 @@ alias name = '/path/to/script'
alias name = '/path/to/script.pl arg1'
```
alias name=value alias name='command' alias name='command arg1 arg2' alias name='/path/to/script' alias name='/path/to/script.pl arg1'
In this example, create the alias **c** for the commonly used clear command, which clears the screen, by typing the following command and then pressing the ENTER key:
举个例子,输入下面命令并回车就会为常用的 `clear`( 清除屏幕)命令创建一个别名 **c**
```
alias c = 'clear'
```
Then, to clear the screen, instead of typing clear, you would only have to type the letter 'c' and press the [ENTER] key:
然后输入字母 `c` 而不是 `clear` 后回车就会清除屏幕了:
```
c
```
### How to disable a bash alias temporarily
### 如何临时性地禁用 bash 别名
An [alias can be disabled temporarily][5] using the following syntax:
下面语法可以[临时性地禁用别名 ][5]
```
## path/to/full/command
/usr/bin/clear
@ -61,37 +60,37 @@ An [alias can be disabled temporarily][5] using the following syntax:
command ls
```
### How to delete/remove a bash alias
### 如何删除 bash 别名
You need to use the command [called unalias to remove aliases][6]. Its syntax is as follows:
使用 [unalias 命令来删除别名 ][6]。其语法为:
```
unalias aliasname
unalias foo
```
In this example, remove the alias c which was created in an earlier example:
例如,删除我们之前创建的别名 `c`
```
unalias c
```
You also need to delete the alias from the [~/.bashrc file][1] using a text editor (see next section).
你还需要用文本编辑器删掉 [~/.bashrc 文件 ][1] 中的别名定义(参见下一部分内容)。
The alias c remains in effect only during the current login session. Once you logs out or reboot the system the alias c will be gone. To avoid this problem, add alias to your [~/.bashrc file][1], enter:
### 如何让 bash shell 别名永久生效
别名 `c` 在当前登录会话中依然有效。但当你登出或重启系统后,别名 `c` 就没有了。为了防止出现这个问题,将别名定义写入 [~/.bashrc file][1] 中,输入:
```
vi ~/.bashrc
```
The alias c for the current user can be made permanent by entering the following line:
输入下行内容让别名 `c` 对当前用户永久有效:
```
alias c = 'clear'
```
Save and close the file. System-wide aliases (i.e. aliases for all users) can be put in the /etc/bashrc file. Please note that the alias command is built into a various shells including ksh, tcsh/csh, ash, bash and others.
保存并关闭文件就行了。系统级的别名(也就是对所有用户都生效的别名) 可以放在 `/etc/bashrc` 文件中。请注意alias 命令内建于各种 shell 中,包括 kshtcsh/cshashbash 以及其他 shell。
### A note about privileged access
### 关于特权权限判断
You can add code as follows in ~/.bashrc:
可以将下面代码加入 `~/.bashrc`
```
# if user is not root, pass all commands via sudo #
if [ $UID -ne 0 ]; then
@ -100,9 +99,9 @@ if [ $UID -ne 0 ]; then
fi
```
### A note about os specific aliases
### 定义与操作系统类型相关的别名
You can add code as follows in ~/.bashrc [using the case statement][7]:
可以将下面代码加入 `~/.bashrc` [使用 case 语句 ][7]
```
### Get os name via uname ###
_myos="$(uname)"
@ -116,13 +115,13 @@ case $_myos in
esac
```
## 30 bash shell aliases examples
## 30 个 bash shell 别名的案例
You can define various types aliases as follows to save time and increase productivity.
你可以定义各种类型的别名来节省时间并提高生产率。
### #1: Control ls command output
### #1:控制 ls 命令的输出
The [ls command lists directory contents][8] and you can colorize the output:
[ls 命令列出目录中的内容 ][8] 而你可以对输出进行着色:
```
## Colorize the ls output ##
alias ls = 'ls --color=auto'
@ -134,7 +133,7 @@ alias ll = 'ls -la'
alias l.= 'ls -d . .. .git .gitignore .gitmodules .travis.yml --color=auto'
```
### #2: Control cd command behavior
### #2:控制 cd 命令的行为
```
## get rid of command not found ##
alias cd..= 'cd ..'
@ -148,9 +147,9 @@ alias .4= 'cd ../../../../'
alias .5= 'cd ../../../../..'
```
### #3: Control grep command output
### #3:控制 grep 命令的输出
[grep command is a command-line utility for searching][9] plain-text files for lines matching a regular expression:
[grep 命令是一个用于在纯文本文件中搜索匹配正则表达式的行的命令行工具 ][9]
```
## Colorize the grep command output for ease of use (good for log files)##
alias grep = 'grep --color=auto'
@ -158,44 +157,44 @@ alias egrep = 'egrep --color=auto'
alias fgrep = 'fgrep --color=auto'
```
### #4: Start calculator with math support
### #4:让计算器默认开启 math 库
```
alias bc = 'bc -l'
```
### #4: Generate sha1 digest
### #4:生成 sha1 数字签名
```
alias sha1 = 'openssl sha1'
```
### #5: Create parent directories on demand
### #5:自动创建父目录
[mkdir command][10] is used to create a directory:
[mkdir 命令 ][10] 用于创建目录:
```
alias mkdir = 'mkdir -pv'
```
### #6: Colorize diff output
### #6:为 diff 输出着色
You can [compare files line by line using diff][11] and use a tool called colordiff to colorize diff output:
你可以[使用 diff 来一行行第比较文件 ][11] 而一个名为 colordiff 的工具可以为 diff 输出着色:
```
# install colordiff package :)
alias diff = 'colordiff'
```
### #7: Make mount command output pretty and human readable format
### #7:让 mount 命令的输出更漂亮,更方便人类阅读
```
alias mount = 'mount |column -t'
```
### #8: Command short cuts to save time
### #8:简化命令以节省时间
```
# handy short cuts #
alias h = 'history'
alias j = 'jobs -l'
```
### #9: Create a new set of commands
### #9:创建一系列新命令
```
alias path = 'echo -e ${PATH//:/\\n}'
alias now = 'date +"%T"'
@ -203,7 +202,7 @@ alias nowtime =now
alias nowdate = 'date +"%d-%m-%Y"'
```
### #10: Set vim as default
### #10:设置 vim 为默认编辑器
```
alias vi = vim
alias svi = 'sudo vi'
@ -211,7 +210,7 @@ alias vis = 'vim "+set si"'
alias edit = 'vim'
```
### #11: Control output of networking tool called ping
### #11:控制网络工具 ping 的输出
```
# Stop after sending count ECHO_REQUEST packets #
alias ping = 'ping -c 5'
@ -220,16 +219,16 @@ alias ping = 'ping -c 5'
alias fastping = 'ping -c 100 -s.2'
```
### #12: Show open ports
### #12:显示打开的端口
Use [netstat command][12] to quickly list all TCP/UDP port on the server:
使用 [netstat 命令 ][12] 可以快速列出服务区中所有的 TCP/UDP 端口:
```
alias ports = 'netstat -tulanp'
```
### #13: Wakeup sleeping servers
### #13:唤醒休眠额服务器
[Wake-on-LAN (WOL) is an Ethernet networking][13] standard that allows a server to be turned on by a network message. You can [quickly wakeup nas devices][14] and server using the following aliases:
[Wake-on-LAN (WOL) 是一个以太网标准 ][13],可以通过网络消息来开启服务器。你可以使用下面别名来[快速激活 nas 设备 ][14] 以及服务器:
```
## replace mac with your actual server mac address #
alias wakeupnas01 = '/usr/bin/wakeonlan 00:11:32:11:15:FC'
@ -237,9 +236,9 @@ alias wakeupnas02 = '/usr/bin/wakeonlan 00:11:32:11:15:FD'
alias wakeupnas03 = '/usr/bin/wakeonlan 00:11:32:11:15:FE'
```
### #14: Control firewall (iptables) output
### #14:控制防火墙 (iptables) 的输出
[Netfilter is a host-based firewall][15] for Linux operating systems. It is included as part of the Linux distribution and it is activated by default. This [post list most common iptables solutions][16] required by a new Linux user to secure his or her Linux operating system from intruders.
[Netfilter 是一款 Linux 操作系统上的主机防火墙 ][15]。它是 Linux 发行版中的一部分,且默认情况下是激活状态。[这里列出了大多数 Liux 新手防护入侵者最常用的 iptables 方法 ][16]。
```
## shortcut for iptables and pass it via sudo#
alias ipt = 'sudo /sbin/iptables'
@ -252,7 +251,7 @@ alias iptlistfw = 'sudo /sbin/iptables -L FORWARD -n -v --line-numbers'
alias firewall =iptlist
```
### #15: Debug web server / cdn problems with curl
### #15:使用 curl 调试 web 服务器 /cdn 上的问题
```
# get web server headers #
alias header = 'curl -I'
@ -261,7 +260,7 @@ alias header = 'curl -I'
alias headerc = 'curl -I --compress'
```
### #16: Add safety nets
### #16:增加安全性
```
# do not delete / or prompt if deleting more than 3 files at a time #
alias rm = 'rm -I --preserve-root'
@ -277,9 +276,9 @@ alias chmod = 'chmod --preserve-root'
alias chgrp = 'chgrp --preserve-root'
```
### #17: Update Debian Linux server
### #17:更新 Debian Linux 服务器
[apt-get command][17] is used for installing packages over the internet (ftp or http). You can also upgrade all packages in a single operations:
[apt-get 命令 ][17] 用于通过因特网安装软件包 (ftp 或 http)。你也可以一次性升级所有软件包:
```
# distro specific - Debian / Ubuntu and friends #
# install with apt-get
@ -290,25 +289,25 @@ alias updatey = "sudo apt-get --yes"
alias update = 'sudo apt-get update && sudo apt-get upgrade'
```
### #18: Update RHEL / CentOS / Fedora Linux server
### #18:更新 RHEL / CentOS / Fedora Linux 服务器
[yum command][18] is a package management tool for RHEL / CentOS / Fedora Linux and friends:
[yum 命令 ][18] 是 RHEL / CentOS / Fedora Linux 以及其他基于这些发行版的 Linux 上的软件包管理工具:
```
## distrp specifc RHEL/CentOS ##
alias update = 'yum update'
alias updatey = 'yum -y update'
```
### #19: Tune sudo and su
### #19:优化 sudo 和 su 命令
```
# become root #
alias root = 'sudo -i'
alias su = 'sudo -i'
```
### #20: Pass halt/reboot via sudo
### #20:使用 sudo 执行 halt/reboot 命令
[shutdown command][19] bring the Linux / Unix system down:
[shutdown 命令 ][19] 会让 Linux / Unix 系统关机:
```
# reboot / halt / poweroff
alias reboot = 'sudo /sbin/reboot'
@ -317,7 +316,7 @@ alias halt = 'sudo /sbin/halt'
alias shutdown = 'sudo /sbin/shutdown'
```
### #21: Control web servers
### #21:控制 web 服务器
```
# also pass it via sudo so whoever is admin can reload it without calling you #
alias nginxreload = 'sudo /usr/local/nginx/sbin/nginx -s reload'
@ -328,7 +327,7 @@ alias httpdreload = 'sudo /usr/sbin/apachectl -k graceful'
alias httpdtest = 'sudo /usr/sbin/apachectl -t && /usr/sbin/apachectl -t -D DUMP_VHOSTS'
```
### #22: Alias into our backup stuff
### #22:与备份相关的别名
```
# if cron fails or if you want backup on demand just run these commands #
# again pass it via sudo so whoever is in admin group can start the job #
@ -343,7 +342,7 @@ alias rsnapshotmonthly = 'sudo /home/scripts/admin/scripts/backup/wrapper.rsnaps
alias amazonbackup =s3backup
```
### #23: Desktop specific - play avi/mp3 files on demand
### #23:桌面应用相关的别名 - 按需播放的 avi/mp3 文件
```
## play video files in a current directory ##
# cd ~/Download/movie-name
@ -366,9 +365,9 @@ alias music = 'mplayer --shuffle *'
```
### #24: Set default interfaces for sys admin related commands
### #24:设置系统管理相关命令的默认网卡
[vnstat is console-based network][20] traffic monitor. [dnstop is console tool][21] to analyze DNS traffic. [tcptrack and iftop commands displays][22] information about TCP/UDP connections it sees on a network interface and display bandwidth usage on an interface by host respectively.
[vnstat 一款基于终端的网络流量检测器 ][20]。[dnstop 是一款分析 DNS 流量的终端工具 ][21]。[tcptrack 和 iftop 命令显示 ][22] TCP/UDP 连接方面的信息,它监控网卡并显示其消耗的带宽。
```
## All of our servers eth1 is connected to the Internets via vlan / router etc ##
alias dnstop = 'dnstop -l 5 eth1'
@ -382,7 +381,7 @@ alias ethtool = 'ethtool eth1'
alias iwconfig = 'iwconfig wlan0'
```
### #25: Get system memory, cpu usage, and gpu memory info quickly
### #25快速获取系统内存cpu 使用,和 gpu 内存相关信息
```
## pass options to free ##
alias meminfo = 'free -m -l -t'
@ -405,9 +404,9 @@ alias cpuinfo = 'lscpu'
alias gpumeminfo = 'grep -i --color memory /var/log/Xorg.0.log'
```
### #26: Control Home Router
### #26:控制家用路由器
The curl command can be used to [reboot Linksys routers][23].
curl 命令可以用来 [重启 Linksys 路由器 ][23]。
```
# Reboot my home Linksys WAG160N / WAG54 / WAG320 / WAG120N Router / Gateway from *nix.
alias rebootlinksys = "curl -u 'admin:my-super-password' 'http://192.168.1.2/setup.cgi?todo=reboot'"
@ -416,15 +415,15 @@ alias rebootlinksys = "curl -u 'admin:my-super-password' 'http://192.168.1.2/set
alias reboottomato = "ssh admin@192.168.1.1 /sbin/reboot"
```
### #27 Resume wget by default
### #27 wget 默认断点续传
The [GNU Wget is a free utility for non-interactive download][25] of files from the Web. It supports HTTP, HTTPS, and FTP protocols, and it can resume downloads too:
[GNU Wget 是一款用来从 web 下载文件的自由软件 ][25]。它支持 HTTPHTTPS以及 FTP 协议,而且它页支持断点续传:
```
## this one saved by butt so many times ##
alias wget = 'wget -c'
```
### #28 Use different browser for testing website
### #28 使用不同浏览器来测试网站
```
## this one saved by butt so many times ##
alias ff4 = '/opt/firefox4/firefox'
@ -439,9 +438,9 @@ alias ff =ff13
alias browser =chrome
```
### #29: A note about ssh alias
### #29:关于 ssh 别名的注意事项
Do not create ssh alias, instead use ~/.ssh/config OpenSSH SSH client configuration files. It offers more option. An example:
不要创建 ssh 别名,代之以 `~/.ssh/config` 这个 OpenSSH SSH 客户端配置文件。它的选项更加丰富。下面是一个例子:
```
Host server10
Hostname 1.2.3.4
@ -452,12 +451,12 @@ Host server10
TCPKeepAlive yes
```
Host server10 Hostname 1.2.3.4 IdentityFile ~/backups/.ssh/id_dsa user foobar Port 30000 ForwardX11Trusted yes TCPKeepAlive yes
然后你就可以使用下面语句连接 peer1 了:
```
$ ssh server10
```
You can now connect to peer1 using the following syntax:
`$ ssh server10`
### #30: It's your turn to share…
### #30:现在该分享你的别名了
```
## set some other defaults ##
@ -487,20 +486,18 @@ alias cdnmdel = '/home/scripts/admin/cdn/purge_cdn_cache --profile akamai --stdi
alias amzcdnmdel = '/home/scripts/admin/cdn/purge_cdn_cache --profile amazon --stdin'
```
## Conclusion
## 结论
This post summarizes several types of uses for *nix bash aliases:
本文总结了 *nix bash 别名的多种用法:
1. Setting default options for a command (e.g. set eth0 as default option for ethtool command via alias ethtool='ethtool eth0' ).
2. Correcting typos (cd.. will act as cd .. via alias cd..='cd ..').
3. Reducing the amount of typing.
4. Setting the default path of a command that exists in several versions on a system (e.g. GNU/grep is located at /usr/local/bin/grep and Unix grep is located at /bin/grep. To use GNU grep use alias grep='/usr/local/bin/grep' ).
5. Adding the safety nets to Unix by making commands interactive by setting default options. (e.g. rm, mv, and other commands).
6. Compatibility by creating commands for older operating systems such as MS-DOS or other Unix like operating systems (e.g. alias del=rm ).
1。为命令设置默认的参数(例如通过 `alias ethtool='ethtool eth0'` 设置 ethtool 命令的默认参数为 eth0)。
2。修正错误的拼写(通过 `alias cd。.='cd .。'``cd。.` 变成 `cd .。`)。
3。缩减输入。
4。设置系统中多版本命令的默认路径(例如 GNU/grep 位于 /usr/local/bin/grep 中而 Unix grep 位于 /bin/grep 中。若想默认使用 GNU grep 则设置别名 `grep='/usr/local/bin/grep'` )。
5。通过默认开启命令(例如 rmmv 等其他命令)的交互参数来增加 Unix 的安全性。
6。为老旧的操作系统(比如 MS-DOS 或者其他类似 Unix 的操作系统)创建命令以增加兼容性(比如 `alias del=rm` )。
I've shared my aliases that I used over the years to reduce the need for repetitive command line typing. If you know and use any other bash/ksh/csh aliases that can reduce typing, share below in the comments.
我已经分享了多年来为了减少重复输入命令而使用的别名。若你知道或使用的哪些 bash/ksh/csh 别名能够减少输入,请在留言框中分享。
--------------------------------------------------------------------------------
@ -516,26 +513,26 @@ via: https://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html
[a]:https://www.cyberciti.biz
[1]:https://bash.cyberciti.biz/guide/~/.bashrc
[2]:https://www.cyberciti.biz/tips/wp-content/uploads/2012/06/Getting-Started-With-Bash-Shell-Aliases-For-Linux-Unix.jpg
[3]://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html (See Linux/Unix alias command examples for more info)
[3]:https://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html (See Linux/Unix alias command examples for more info)
[4]:https://bash.cyberciti.biz/guide/Create_and_use_aliases
[5]://www.cyberciti.biz/faq/bash-shell-temporarily-disable-an-alias/
[5]:https://www.cyberciti.biz/faq/bash-shell-temporarily-disable-an-alias/
[6]:https://bash.cyberciti.biz/guide/Create_and_use_aliases#How_do_I_remove_the_alias.3F
[7]:https://bash.cyberciti.biz/guide/The_case_statement
[8]://www.cyberciti.biz/faq/ls-command-to-examining-the-filesystem/
[9]://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
[10]://www.cyberciti.biz/faq/linux-make-directory-command/
[11]://www.cyberciti.biz/faq/how-do-i-compare-two-files-under-linux-or-unix/
[12]://www.cyberciti.biz/faq/how-do-i-find-out-what-ports-are-listeningopen-on-my-linuxfreebsd-server/
[13]://www.cyberciti.biz/tips/linux-send-wake-on-lan-wol-magic-packets.html
[8]:https://www.cyberciti.biz/faq/ls-command-to-examining-the-filesystem/
[9]:https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
[10]:https://www.cyberciti.biz/faq/linux-make-directory-command/
[11]:https://www.cyberciti.biz/faq/how-do-i-compare-two-files-under-linux-or-unix/
[12]:https://www.cyberciti.biz/faq/how-do-i-find-out-what-ports-are-listeningopen-on-my-linuxfreebsd-server/
[13]:https://www.cyberciti.biz/tips/linux-send-wake-on-lan-wol-magic-packets.html
[14]:https://bash.cyberciti.biz/misc-shell/simple-shell-script-to-wake-up-nas-devices-computers/
[15]://www.cyberciti.biz/faq/rhel-fedorta-linux-iptables-firewall-configuration-tutorial/ (iptables CentOS/RHEL/Fedora tutorial)
[16]://www.cyberciti.biz/tips/linux-iptables-examples.html
[17]://www.cyberciti.biz/tips/linux-debian-package-management-cheat-sheet.html
[18]://www.cyberciti.biz/faq/rhel-centos-fedora-linux-yum-command-howto/
[19]://www.cyberciti.biz/faq/howto-shutdown-linux/
[20]://www.cyberciti.biz/tips/keeping-a-log-of-daily-network-traffic-for-adsl-or-dedicated-remote-linux-box.html
[21]://www.cyberciti.biz/faq/dnstop-monitor-bind-dns-server-dns-network-traffic-from-a-shell-prompt/
[22]://www.cyberciti.biz/faq/check-network-connection-linux/
[23]://www.cyberciti.biz/faq/reboot-linksys-wag160n-wag54-wag320-wag120n-router-gateway/
[24]:/cdn-cgi/l/email-protection
[25]://www.cyberciti.biz/tips/wget-resume-broken-download.html
[15]:https://www.cyberciti.biz/faq/rhel-fedorta-linux-iptables-firewall-configuration-tutorial/ (iptables CentOS/RHEL/Fedora tutorial)
[16]:https://www.cyberciti.biz/tips/linux-iptables-examples.html
[17]:https://www.cyberciti.biz/tips/linux-debian-package-management-cheat-sheet.html
[18]:https://www.cyberciti.biz/faq/rhel-centos-fedora-linux-yum-command-howto/
[19]:https://www.cyberciti.biz/faq/howto-shutdown-linux/
[20]:https://www.cyberciti.biz/tips/keeping-a-log-of-daily-network-traffic-for-adsl-or-dedicated-remote-linux-box.html
[21]:https://www.cyberciti.biz/faq/dnstop-monitor-bind-dns-server-dns-network-traffic-from-a-shell-prompt/
[22]:https://www.cyberciti.biz/faq/check-network-connection-linux/
[23]:https://www.cyberciti.biz/faq/reboot-linksys-wag160n-wag54-wag320-wag120n-router-gateway/
[24]:https:/cdn-cgi/l/email-protection
[25]:https://www.cyberciti.biz/tips/wget-resume-broken-download.html