This commit is contained in:
geekpi 2018-12-19 08:53:08 +08:00
commit f0f1c0e755
7 changed files with 175 additions and 179 deletions

View File

@ -1,34 +1,36 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: subject: (How a university network assistant used Linux in the 90s)
[#]: via: (https://opensource.com/article/18/5/my-linux-story-student)
[#]: author: ([Alan Formy-Duva](https://opensource.com/users/alanfdoss)
[#]: url: ( )
[#]: url: (https://linux.cn/article-10359-1.html)
大学网络助理如何在 90 年代使用 Linux
90 年代的大学网管如何使用 Linux
======
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/moneyrecycle_520x292.png?itok=SAaIziNr)
在 20 世纪 90 年代中期,我报名了计算机科学课。我大学的计算机科学系为学生提供了一台 SunOS 服务器,它是一个多用户、多任务的 Unix 系统。我们登录它并编写我们在学习的编程语言代码,例如 C、C++ 和 ADA。在那些日子里在社交网络和 IM 出现之前,我们还使用该系统相互通信,发送电子邮件和使用诸如 `write``talk` 之类的程序。我们每个人被允许托管一个个人网站。我很高兴它能够完成我的作业并联系其他用户。
在上世纪 90 年代中期,我报名了计算机科学课。我大学的计算机科学系为学生提供了一台 SunOS 服务器,它是一个多用户、多任务的 Unix 系统。我们登录它并编写我们学习的编程语言代码,例如 C、C++ 和 ADA。在那时在社交网络和 IM 出现之前,我们还使用该系统发送电子邮件和使用诸如 `write``talk` 之类的程序来相互通信。我们每个人被允许托管一个个人网站。我很高兴能够使用它完成我的作业并联系其他用户。
这是我第一次体验这种类型的操作环境但我很快就了解了另一个可以做同样事情的操作系统Linux。
当我还是一名学生的时候,我还在大学兼职工作。我的第一个职位是住房和住宅部 HR 的网络安装人员。这包含将学生宿舍与校园网络连接起来。由于这是该大学的第一个宿舍网络服务,因此只有两幢楼和大约 75 名学生已经连接
那会我还是学生我在大学找了份兼职工作。我的第一个职位是住房和住宅部HR的网络安装人员。这工作涉及到将学生宿舍与校园网络连接起来。由于这是该大学的第一个宿舍网络服务,因此只有两幢楼和大约 75 名学生连上了网
我工作的第二年该网络扩展到另外两幢楼。HR 决定让该大学的信息技术办公室 OIT 管理这不断增长的业务。我进入 OIT 并开始担任 OIT 网络经理的学生助理。这就是我发现 Linux 的方式。我的新职责之一是管理防火墙系统,它为宿舍提供网络和互联网访问。
在第二年该网络扩展到另外两幢楼。HR 决定让该大学的信息技术办公室OIT管理这不断增长的业务。我进入 OIT 并开始担任 OIT 网络经理的学生助理。这就是我发现 Linux 的方式。我的新职责之一是管理防火墙系统,它为宿舍提供网络和互联网访问。
每个学生都注册了他们硬件的 MAC 地址。注册学生可以连接到宿舍网络并获得 IP 地址及访问互联网。与大学使用的其他昂贵的 SunOS 和 VMS 服务器不同,这些防火墙使用运行着免费和开源 Linux 操作系统的低成本计算机。截至年底,该系统已注册近 500 名学生。
每个学生都注册了他们硬件的 MAC 地址。注册学生可以连接到宿舍网络并获得 IP 地址及访问互联网。与大学使用的其他昂贵的 SunOS 和 VMS 服务器不同,这些防火墙使用运行着自由开源的 Linux 操作系统的低成本计算机。截至年底,该系统已注册近 500 名学生。
![Red hat Linux install disks][1]
OIT 网络工作人员使用 Linux 运行 HTTP、FTP 和其他服务。他们还在个人桌面上使用 Linux。就在那时我意识到我手上的计算机看起来和运行起来就像 CS 系昂贵的 SunOS 机器一样但没有高昂的成本。Linux 可以在商用 x86 硬件上运行,例如有 8 MB RAM 和 133Mhz Intel Pentium CPU 的 Dell Latitude。那对我来说是个卖点我在从一个剩余的仓库中清理出来的机器上安装了 Red Hat Linux 5.2,并给了我的朋友登录帐户。
OIT 网络工作人员使用 Linux 运行 HTTP、FTP 和其他服务。他们还在个人桌面上使用 Linux。就在那时我意识到我手上的计算机看起来和运行起来就像 CS 系昂贵的 SunOS 机器一样但没有高昂的成本。Linux 可以在商用 x86 硬件上运行,例如有 8 MB RAM 和 133Mhz Intel Pentium CPU 的 Dell Latitude。那对我来说是个卖点我在从一个存货仓库中清理出来的机器上安装了 Red Hat Linux 5.2,并给了我的朋友登录帐户。
我使用我的新 Linux 服务器来托管我的网站并向我的朋友提供帐户,同时它还提供 CS 系服务器没有的图形功能。它使用了 X Windows 系统,我可以使用 Netscape Navigator 浏览网页,使用 [XMMS][2] 播放音乐,并尝试不同的窗口管理器。我也可以下载并编译其他开源软件并编写自己的代码。
我了解到 Linux 提供了一些非常先进的功能,其中许多功能比更主流的操作系统更方便或更优越。例如,许多操作系统尚未提供应用更新的简单方法。在 Linux 中,这很简单,感谢 [autoRPM][3],一个由 Kirk Bauer 编写的更新管理器,它向 root 用户每日发送邮件,其中包含可用的更新。它有一个直观的界面,用于审查和选择要安装的软件更新 - 这对于 90 年代中期来说非常了不起。
我了解到 Linux 提供了一些非常先进的功能,其中许多功能比更主流的操作系统更方便或更优越。例如,许多操作系统尚未提供应用更新的简单方法。在 Linux 中,这很简单,感谢 [autoRPM][3],一个由 Kirk Bauer 编写的更新管理器,它向 root 用户每日发送邮件,其中包含可用的更新。它有一个直观的界面,用于审查和选择要安装的软件更新 —— 这对于 90 年代中期来说非常了不起。
Linux may not have been well-known back then, and it was often received with skepticism, but I was convinced it would survive. And survive it did!
当时 Linux 可能并不为人所知,而且它经常受到怀疑,但我确信它会存活下来。而它确实生存下来了!
--------------------------------------------------------------------------------
@ -37,11 +39,11 @@ via: https://opensource.com/article/18/5/my-linux-story-student
作者:[Alan Formy-Duval][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://opensource.com/users/alanfdoss
[1]:https://opensource.com/sites/default/files/styles/panopoly_image_original/public/images/life-uploads/red_hat_linux_install_disks.png?itok=VSw6Cke9 (Red hat Linux install disks)
[2]:http://www.xmms.org/
[3]:http://www.ccp14.ac.uk/solution/linux/autorpm_redhat7_3.html
[3]:http://www.ccp14.ac.uk/solution/linux/autorpm_redhat7_3.html

View File

@ -1,26 +1,22 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-10362-1.html)
[#]: subject: (Four Easy Ways to Search Or Find Files And Folders in Linux)
[#]: via: (https://www.2daygeek.com/four-easy-ways-to-search-or-find-files-and-folders-in-linux/)
[#]: author: (Prakash Subramanian https://www.2daygeek.com/author/prakash/)
四种简单的方法来搜索或查找 Linux 中的文件和文件夹
搜索 Linux 中的文件和文件夹的四种简单方法
======
Linux 管理员一天都不能离开搜索文件,因为这是他们的日常活动。
Linux 管理员一天都不能离开搜索文件,因为这是他们的日常活动。了解一些搜索的东西是不错的,因为这能帮助你在命令行服务器中工作。这些命令记忆起来不复杂,因为它们使用的是标准语法。
了解一些搜索的东西是不错的,因为这能帮助你在命令行服务器中工作。
这些命令记忆起来不复杂,因为它们使用的是标准语法。
这里执行四个 Linux 命令,每个命令都有自己独特的功能。
可以通过四个 Linux 命令啦执行此操作,每个命令都有自己独特的功能。
### 方法 1使用 find 命令在 Linux 中搜索文件和文件夹
find 命令被广泛使用,并且是非常著名的在 Linux 中搜索文件和文件夹的命令。它搜索当前目录中的给定文件,并根据搜索条件递归遍历其子目录。
`find` 命令被广泛使用,并且是在 Linux 中搜索文件和文件夹的著名命令。它搜索当前目录中的给定文件,并根据搜索条件递归遍历其子目录。
它允许用户根据大小、名称、所有者、组、类型、权限、日期和其他条件执行所有类型的文件搜索。
@ -72,19 +68,19 @@ find 命令被广泛使用,并且是非常著名的在 Linux 中搜索文件
### 方法 2使用 locate 命令在 Linux 中搜索文件和文件夹
locate 命令比 find 命令运行得更快,因为它使用 updatedb 数据库,而 find 命令在真实系统中搜索。
`locate` 命令比 `find` 命令运行得更快,因为它使用 `updatedb` 数据库,而 `find` 命令在真实系统中搜索。
它使用数据库而不是搜索单个目录路径来获取给定文件。
locate 命令未在大多数发行版中预安装,因此,请使用你的包管理器进行安装。
`locate` 命令未在大多数发行版中预安装,因此,请使用你的包管理器进行安装。
数据库通过 cron 定期更新,但我们可以通过运行以下命令手动更新它。
数据库通过 cron 任务定期更新,但我们可以通过运行以下命令手动更新它。
```
$ sudo updatedb
```
只需运行以下命令即可列出给定的文件或文件夹。在 locate 命令中不需要指定特定选项来打印文件或文件夹。
只需运行以下命令即可列出给定的文件或文件夹。在 `locate` 命令中不需要指定特定选项来打印文件或文件夹。
在系统中搜索 `ssh` 文件夹。
@ -107,13 +103,13 @@ $ sudo updatedb
### 方法 3在 Linux 中搜索文件使用 which 命令
which 返回在终端输入命令时执行的可执行文件的完整路径。
`which` 返回在终端输入命令时执行的可执行文件的完整路径。
当你想要为可执行文件创建桌面快捷方式或符号链接时,它非常有用。
which 命令搜索当前用户而不是所有用户的 PATH 环境变量中列出的目录。我的意思是,当你登录自己的帐户时,你无法搜索 root 用户文件或目录。
`which` 命令搜索当前用户而不是所有用户的 `$PATH` 环境变量中列出的目录。我的意思是,当你登录自己的帐户时,你无法搜索 root 用户文件或目录。
运行以下命令以打印 vim 可执行文件的完整路径。
运行以下命令以打印 `vim` 可执行文件的完整路径。
```
# which vi
@ -132,12 +128,13 @@ which 命令搜索当前用户而不是所有用户的 PATH 环境变量中列
### 方法 4使用 whereis 命令在 Linux 中搜索文件
whereis 命令用于搜索给定命令的二进制、源码和手册页文件。
`whereis` 命令用于搜索给定命令的二进制、源码和手册页文件。
```
# whereis vi
vi: /usr/bin/vi /usr/share/man/man1/vi.1p.gz /usr/share/man/man1/vi.1.gz
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/four-easy-ways-to-search-or-find-files-and-folders-in-linux/
@ -145,9 +142,9 @@ via: https://www.2daygeek.com/four-easy-ways-to-search-or-find-files-and-folders
作者:[Prakash Subramanian][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/prakash/
[b]: https://github.com/lujun9972
[b]: https://github.com/lujun9972

View File

@ -1,22 +1,24 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: subject: (Bring some color to your Linux terminal with lolcat)
[#]: via: (https://opensource.com/article/18/12/linux-toy-lolcat)
[#]: author: (Jason Baker https://opensource.com/users/jason-baker)
[#]: url: ( )
[#]: url: (https://linux.cn/article-10361-1.html)
使用 lolcat 为你的 Linux 终端带来一些颜色
使用 lolcat 为你的 Linux 终端带来彩虹
======
使用这个简单的程序,你可以为所需的任何程序的输出添加彩色。
> 使用这个简单的工具,你可以为所需的任何程序的输出变成七彩。
![](https://opensource.com/sites/default/files/styles/image-full-size/public/uploads/linux-toy-lolcat.png?itok=Es6dYcph)
今天是 Linux 命令行玩具日历的第五天。如果这是你第一次访问该系列,你可能会问自己,什么是命令行玩具。即使我不太确定,但一般来说,它可能是一个游戏,或任何简单的可以帮助你在终端玩得开心的东西。
很可能你们中的一些人之前已经看过我们日历中的各种选择,但我们希望每个人至少见到一件新事物。
很可能你们中的一些人之前已经看过我们日历中的各种玩具,但我们希望每个人至少见到一件新事物。
今日的选择,**lolcat**,是我选择的第一个没有在我的 Linux 发行版中打包的程序,但它安装仍然很简单。它是一个 Ruby 程序,你应该可以使用下面的命令轻松地添加到系统中。
今日的选择,`lolcat`,是我选择的第一个没有在我的 Linux 发行版中打包的程序,但它安装仍然很简单。它是一个 Ruby 程序,你应该可以使用下面的命令轻松地添加到系统中。
```
$ gem install lolcat
@ -29,15 +31,18 @@ $ fortune | boxes -a c -d parchment | lolcat
```
根据你的运气,你可能会看到这样:
![](https://opensource.com/sites/default/files/uploads/linux-toy-lolcat-parchment.png)
你可以传递给 **lolcat** 一些参数而不必重复它们,我建议你访问 **lolcat** 的 [GitHub 页面][1] 或者在终端输入 **lolcat --help** 了解。但一般来说,它们能设置彩虹的传递和频率,以及我个人最喜欢的动画。谁不喜欢终端的彩色动画打印?让我们再试一次,用一个不同的边框(当然是以猫为主题)和一句在我的句子列表中的适合猫的句子。
你可以传递给 `lolcat` 一些参数。这里不再赘述,我建议你访问 `lolcat` 的 [GitHub 页面][1] 或者在终端输入 `lolcat --help` 了解。但一般来说,它们能设置彩虹的传递和频率,以及我个人最喜欢的动画。谁不喜欢终端的彩色动画输出呢?让我们再试一次,用一个不同的边框(当然是以猫为主题)和一句在我的格言列表中的适合猫的句子。
```
fortune -m "nine tails" | boxes -a c -d cat | lolcat -a
```
![](https://opensource.com/sites/default/files/uploads/linux-toy-lolcat-animated.gif)
**lolcat** 是 BSD 许可下的开源软件。
`lolcat` 是一个 BSD 许可下的开源软件。
你有特别喜欢的命令行小玩具需要我介绍的吗?这个系列要介绍的小玩具大部分已经有了落实,但还预留了几个空位置。如果你有特别想了解的可以评论留言,我会查看的。如果还有空位置,我会考虑介绍它的。如果没有,但如果我得到了一些很好的意见,我会在最后做一些有价值的提及。
@ -50,11 +55,11 @@ via: https://opensource.com/article/18/12/linux-toy-lolcat
作者:[Jason Baker][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/jason-baker
[b]: https://github.com/lujun9972
[1]: https://github.com/busyloop/lolcat
[2]: https://opensource.com/article/18/12/linux-toy-cowsay
[2]: https://opensource.com/article/18/12/linux-toy-cowsay

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -1,3 +1,5 @@
translating by cycoe
cycoe 翻译中
9 flowchart and diagramming tools for Linux
======

View File

@ -1,133 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (HankChow)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Aliases: DIY Shell Commands)
[#]: via: (https://www.linux.com/blog/learn/2018/12/aliases-diy-shell-commands)
[#]: author: (Paul Brown https://www.linux.com/users/bro66)
Aliases: DIY Shell Commands
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/jodi-mucha-540841-unsplash.jpg?itok=n1d1VDUV)
Aliases, in the context of the Linux shell, are **commands you build yourself** by packing them with combinations of other instructions that are too long or too hard to remember.
You create an alias by using the word `alias`, then the name of the command you want to create, an equal sign (`=`), and then the Bash command(s) you want your alias to run. For example, `ls` in its base form does not colorize its output, making it difficult to distinguish between directories, files, and links. You can build a new command that shows colors by making an alias like this:
```
alias lc='ls --color=auto'
```
where `lc` is the name you have picked for your new command. When creating aliases, be sure to check that the name you picked isn't already in use, or you may override an existing command. In this case, `lc` stands for "list (with) color". Notice there is no space in front of or behind the `=`. Finally, you have the regular Bash command(s) you want to run when `lc` is executed. In this case, the `ls` command with the `--color` option.
After defining your alias, every time you type `lc`, the contents of the current directory will be shown in color.
But, you may think, "my `ls` command already lists files in different colors!" That is because most Linux distros come with some aliases already set up for you.
### Aliases you (probably) already have
Indeed, you can use the `alias` instruction without any options to see what aliases you already have. These will vary by distro, but some typical preset aliases are:
* `alias ls='ls --color=auto'`: You already saw this one above. The `auto` modifier of the `--color` option tells `ls` to use color when standard output is connected to a terminal. That is, the output of `ls` is going to show up in a terminal window or a text screen, instead of, say, being piped to a file. Other alternatives for `--color` are `always` and `never`.
* `alias cp='cp -i'`: The `-i` option stands for _interactive_. Sometimes, when you use `cp` you may inadvertently overwrite an existing file. By using the `-i`, `cp` will ask you before clobbering anything.
* `alias free='free -m'`: Using `-m` with `free`you can see how much free memory you have and how much your applications are using in megabytes instead of the default bytes. This makes the output of `free` easier to read for a human.
There may be more (or less, or even none), but regardless of what your distribution comes with, you can always use the base form (vs. the aliased form) of a command with the `\` modifier. For example:
```
\free
```
will execute `free` without the `-m` option, and
```
\ls
```
will execute `ls` without the `--color=auto` option.
If you want to get rid or modify the preset aliases forever, note that they live in the global _.bashrc_ file which hangs out in [our old haunt, the _/etc/skel_ directory][1].
### Aliases for muscle memory
Distro designers try their best to predict which aliases are going to be useful for you. But every user is different and comes from a different background. If you are new to GNU+Linux, it may be because you are coming from another system, and the basic commands vary from shell to shell. If you come from a Windows/MS-DOS background, you may want to define an alias like
```
alias dir='ls'
```
to list files or directories.
Likewise,
```
alias copy='cp'
alias move='mv'
```
may also come in handy, at least until you get used to Linux's new lexicon.
The other problem occurs when mistakes become ingrained in your muscle memory, so you always mistype some words the same way. I, for instance, have great difficulty typing _admnis-_... _adminsi-_... _A-D-M-I-N-I-S-T-R-A-T-I-ON_ ( _phew!_ ) at speed. That is why some users create aliases like
```
alias sl='ls'
```
and
```
alias gerp='echo "You did it *again*!"; grep'
```
Although we haven't formally introduced `grep` yet, in its most basic form, it looks for a string of characters in a file or a set of files. It's one of those commands that you will tend to use A LOT once you get to grips with it, as those ingrained mistyping habits that force you to type the instruction twice every time get annoying really quickly.
Another thing to note in the `gerp` example is that it is not a single instruction, but two. The first one (`echo "You did it *again*!"`) prints out a message reminding you that you misspelled the grep command, then there is a semicolon (`;`) that separates one instruction from the other. Finally, you've got the second command (`grep`) that does the actual grepping.
Using `gerp` on my system to search for the lines containing the word " _alias_ " in _/etc/skel/.bashrc_ , the output looks like this:
```
$ gerp -R alias /etc/skel/.bashrc
You did it *again*!
alias ls='ls --color=auto'
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'
alias cp="cp -i"
alias df='df -h'
alias free='free -m'
alias np='nano -w PKGBUILD'
alias more=less
shopt -s expand_aliases
```
Running commands sequentially as part of an alias, or, even better, chaining commands so that one command can use the results coughed up by another, is getting us perilously close to Bash scripting. This has been in the making of this series for quite some time, and we'll start covering it in the very next article.
For the time being, if you want to get rid of an alias you temporarily set up in a running terminal, use the `unalias` command:
```
unalias gerp
```
If you want to make your aliases permanent, you can drop them into the _.bashrc_ file you have in your home directory. This is the same thing we did with custom environment variables in [last week's article][2].
See you next time!
--------------------------------------------------------------------------------
via: https://www.linux.com/blog/learn/2018/12/aliases-diy-shell-commands
作者:[Paul Brown][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/bro66
[b]: https://github.com/lujun9972
[1]: https://www.linux.com/learn/intro-to-linux/2018/7/users-groups-and-other-linux-beasts
[2]: https://www.linux.com/blog/learn/2018/12/bash-variables-environmental-and-otherwise

View File

@ -0,0 +1,123 @@
命令别名:定义自己的命令
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/jodi-mucha-540841-unsplash.jpg?itok=n1d1VDUV)
<ruby>命令别名<rt>Alias</rt></ruby>在 Linux shell 中指的是将一些太长或者太难记的多个命令组合起来,成为一个由用户自定义构建的命令。
可以通过 `alias` 命令来创建命令别名。在 `alias` 后面跟上想要创建的别名名称、一个等号(`=`),以及希望使用这个别名来执行的命令,这样一个命令别名就创建好了。举个例子,`ls` 命令在默认情况下是不会对输出的内容进行着色的,这样就不能让用户一眼分辨出目录、文件和连接了。对此,可以创建这样一个命令别名,在输出目录内容的时候为输出内容着色:
```
alias lc='ls --color=auto'
```
其中 `lc` 是自定义的命令别名代表“list with color”的意思。在创建命令别名的时候需要先确认使用的别名是不是已经有对应的命令了如果有的话原本的命令就会被覆盖掉了。注意定义命令别名的时候`=` 两端是没有空格的。当运行 `lc` 的时候,就相当于执行了 `ls --color` 命令。
此后,执行 `lc` 列出目录内容的时候,就会输出带有着色的内容了。
你可能会发现你在执行 `ls` 的时候,本来就是输出带有着色的内容。那是因为大部分 Linux 发行版都已经将 `ls` 设定为带有着色的命令别名了。
### 可以直接使用的命令别名
实际上,执行不带任何内容的 `alias` 命令就可以看到当前已经设定的所有命令别名。对于不同的发行版,包含的命令别名不尽相同,但普遍都会有以下这些命令别名:
* `alias ls='ls --color=auto'`:这个命令别名在前面已经提到过了。`--color=auto` 参数会让 `ls` 命令在通过标准输出在终端中显示内容时进行着色,而其它情况(例如通过管道输出到文件)下则不进行着色。`--color` 这个参数还可以设置为 `always` 或`never`。
* `alias cp='cp -i'``-i` 参数代表“<ruby>交互<rt>interactive</rt></ruby>”。在使用 `cp` 命令复制文件的时候,可能会无意中覆盖现有的文件,在使用了 `-i` 参数之后,`cp` 命令会在一些关键操作前向用户发出询问。
* `alias free='free -m'`:在 `free` 命令后面加上 `-m` 参数,就可以将输出的内存信息以 MiB 这个更方面阅读和计算的单位输出,而不是默认的 Byte 单位。
你使用的发行版自带的命令别名可能多多少少和上面有些差别。但你都可以在命令前面加上 `\` 修饰符来使用命令的最基本形式。例如:
```
\free
```
就是直接执行 `free`,而不是 `free -m`。还有:
```
\ls
```
执行的就是不带有`--color=auto` 参数的 `ls`
如果想要持久地保存命令别名,可以在 `.bashrc` 文件中进行修改。
### 使用命令别名纠正错误
各种发行版的设计者都会尽量设置用户可能需要用到的命令别名。但是不同的用户的习惯各不相同,一些用户可能刚从其它操作系统迁移到 Linux而不同操作系统的基本命令又因 shell 而异。因此,对于刚从 Windows/MS-DOS 系统迁移到 Linux 系统的用户,不妨使用
```
alias dir='ls'
```
这个命令别名来列出目录内容。
类似地,
```
alias copy='cp'
alias move='mv'
```
也可以在尚未完全熟悉 Linux 的时候用得顺手。
还有一种情况,就是在经常出现输入错误的场合中做出容错,例如 Administration 这个单词就很难快速正确地输入,因此很多用户都会设置
```
alias sl='ls'
```
以及
```
alias gerp='echo "You did it *again*!"; grep'
```
`grep` 命令最基本的用途就是在文件中查找字符串,在熟悉这个命令之后,它一定是最常用的命令之一,因此输入错误导致不得不重输命令就很令人抓狂。
在上面 `gerp` 的例子中,包含的不只是一条命令,而是两条。第一条命令 `echo "You did it *again*!"` 输出了一条提醒用户拼写错误的消息,然后使用分号(``)把两条命令隔开,再往后才是 `grep` 这一条正确的命令。
在我的系统上使用 `gerp` 来搜索 `/etc/skel/.bashrc` 中包含“alias”这个单词的行就会输出以下内容
```
$ gerp -R alias /etc/skel/.bashrc
You did it *again*!
alias ls='ls --color=auto'
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'
alias cp="cp -i"
alias df='df -h'
alias free='free -m'
alias np='nano -w PKGBUILD'
alias more=less
shopt -s expand_aliases
```
在命令别名中以固定的顺序执行多个命令,甚至更进一步,把多个命令串连起来,让后面的命令可以使用到前面的命令的执行结果。这样的做法已经非常接近 bash 脚本了。这篇文章已经接近尾声,我们将在下一篇文章中详细介绍。
如果想要删除在终端中临时设置的别名,可以使用 `unalias` 命令。
```
unalias gerp
```
如果想要持久保存命令别名,可以将命令别名放在用户主目录的 `.bashrc` 文件中,具体的方法在[上一篇文章][2]中已经介绍过。
--------------------------------------------------------------------------------
via: https://www.linux.com/blog/learn/2018/12/aliases-diy-shell-commands
作者:[Paul Brown][a]
选题:[lujun9972][b]
译者:[HankChow](https://github.com/HankChow)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/bro66
[b]: https://github.com/lujun9972
[1]: https://www.linux.com/learn/intro-to-linux/2018/7/users-groups-and-other-linux-beasts
[2]: https://www.linux.com/blog/learn/2018/12/bash-variables-environmental-and-otherwise