Using the command line seems rugged and unpleasant, but Linux has a way to ease things up and help you get things done with the command line by allowing you to use aliases to customize how you type commands.
使用命令行看起来很粗糙且令人不愉快,但是Linux允许您使用别名来自定义键入命令的方式,从而简化了工作并帮助您完成了命令行操作。
设置别名 (Setting up Aliases)
Aliases are a way for you to customize the commands by giving them aliases (nicknames). You can use them to remember hard commands or make short names to long commands you just hate to type. To setup aliases, right-click and create an empty file in your home directory and name it “.bash_aliases”. Notice the period at the beginning of the name that will make the file hidden. Press “Ctrl+H” to show the hidden files.
别名是一种通过给它们提供别名(昵称)来自定义命令的方法。 您可以使用它们来记住硬命令或为您不喜欢键入的长命令取短名。 要设置别名,请右键单击并在您的主目录中创建一个空文件,并将其命名为“ .bash_aliases”。 请注意名称开头的句点,它将使文件隐藏。 按“ Ctrl + H”显示隐藏的文件。
别名语法 (Aliases Syntax)
Using your favorite text editor open the file you just created and start making your aliases. But there are few things you should keep in mind, the syntax should be:
使用您喜欢的文本编辑器打开您刚刚创建的文件,然后开始创建别名。 但是您应该牢记一些事情,语法应该是:
alias new_name='old_command'
alias new_name='old_command'
Where “new_name” is the alias, and “old_command” is the command you want to change and is put between quotes. Example:
其中“ new_name”是别名,“ old_command”是要更改的命令,并放在引号之间。 例:
alias agi='sudo apt-get install'
alias agi='sudo apt-get install'
This will make typing “agi” the same as typing “sudo apt-get install”. This way if you have a dozen of packages to install, your task just got easier. Keep in mind that if you create an alias that looks the same as a command, then the command or alias will not work. Example:
这将使键入“ agi”与键入“ sudo apt-get install”相同。 这样,如果您要安装许多软件包,您的任务就会变得更加容易。 请记住,如果创建的别名与命令相同,则该命令或别名将不起作用。 例:
alias install='sudo apt-get install'
alias install='sudo apt-get install'
The alias in the above example won’t work because there is already a command by the name “install”.
上面的示例中的别名无效,因为已经有一个名为“ install”的命令。
And remember that creating aliases from two words won’t work unless you connect them with a dash. Example:
请记住,除非您用破折号连接它们,否则不能使用两个单词创建别名。 例:
alias apt install='sudo apt-get install'
alias apt install='sudo apt-get install'
alias apt-install='sudo apt-get install'
alias apt-install='sudo apt-get install'
In the above example, the first alias is invalid as the alias consists of two separate words while the second alias is good to use because the two words are connected with a dash. And the last thing that you shouldn’t do is put any space at the starting of any line. So that’s everything about creating the aliases, but what aliases would you use? Keep reading!
在上面的示例中,第一个别名无效,因为该别名由两个单独的单词组成,而第二个别名很好用,因为这两个单词用破折号相连。 最后,您不应该做的是在任何行的开头放置任何空格。 这就是创建别名的全部内容,但是您将使用什么别名? 继续阅读!
使用什么别名 (What aliases to use)
Now that you know how to setup aliases and create your own. Let’s see what aliases you can use to make the most out of it.
现在,您知道如何设置别名并创建自己的别名。 让我们看看可以使用哪些别名来充分利用它。
-
Manage packages
管理包裹
If you have to install and remove packages too often then you really are going to like this.
如果您必须过于频繁地安装和删除软件包,那么您真的会很喜欢这样。
alias agi='sudo apt-get install'
alias agi='sudo apt-get install'
alias agr='sudo apt-get remove'
alias agr='sudo apt-get remove'
alias agu='sudo apt-get update'
alias agu='sudo apt-get update'
alias acs='apt-cache search'
alias acs='apt-cache search'
The aliases above are made of the first letters of each word in the command. These are just examples of what you can use and you can use them, modify them or create your own.
上面的别名由命令中每个单词的前几个字母组成。 这些只是您可以使用的示例,您可以使用,修改或创建自己的示例。
-
Manage files and folders
管理文件和文件夹
alias cp='cp -iv'
alias cp='cp -iv'
alias mv='mv -iv'
alias mv='mv -iv'
alias rm='rm -i'
alias rm='rm -i'
alias la='ls -alh'
alias la='ls -alh'
These aliases will make the command line ask you for confirmation on deleting files or overwriting them (if there were duplicates) when copying or moving files as well as give you more information on what is being done. This can prevent you from accidentally overwriting your files or sending them to somewhere you shouldn’t send them to.
这些别名将使命令行在复制或移动文件时要求您确认删除文件或覆盖文件(如果有重复项),以及有关正在执行的操作的更多信息。 这样可以防止您意外覆盖文件或将其发送到不应发送到的位置。
-
Navigating the system
导航系统
alias documents='cd ~/Documents'
alias documents='cd ~/Documents'
alias downloads='cd ~/Downloads'
alias downloads='cd ~/Downloads'
alias desktop='cd ~/Desktop'
alias desktop='cd ~/Desktop'
alias music='cd ~/Music'
alias music='cd ~/Music'
alias videos='cd ~/Videos'
alias videos='cd ~/Videos'
alias ..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias ....='cd ../../..'
Now navigating your files and folders can be no easier. Type the directory you want to go to and type dots to go up.
现在浏览文件和文件夹再简单不过了。 键入您要转到的目录,然后键入点以向上。
-
Other useful aliases
其他有用的别名
alias e='exit'
alias e='exit'
alias s='sudo'
alias s='sudo'
alias shutdown='sudo shutdown –h now' #requires root password, disable it by "sudo chmod u+s /sbin/shutdown"
alias shutdown='sudo shutdown –h now' #requires root password, disable it by "sudo chmod u+s /sbin/shutdown"
alias restart='sudo shutdown –r now' #requires root password, disable it by "sudo chmod u+s /sbin/shutdown"
alias restart='sudo shutdown –r now' #requires root password, disable it by "sudo chmod u+s /sbin/shutdown"
alias suspend='sudo pm-suspend'
alias suspend='sudo pm-suspend'
alias lock='gnome-screensaver-command --lock'
alias lock='gnome-screensaver-command --lock'
alias mounted='mount | column –t
alias mounted='mount | column –t
Got any more tips or great aliases? Share them in the comments.
还有更多提示或别名吗? 在评论中分享它们。
翻译自: https://www.howtogeek.com/73768/how-to-use-aliases-to-customize-ubuntu-commands/