mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated (#10352)
This commit is contained in:
parent
6d4b83d471
commit
9c982a4d75
@ -1,171 +0,0 @@
|
|||||||
HankChow translating
|
|
||||||
|
|
||||||
Linux tricks that can save you time and trouble
|
|
||||||
======
|
|
||||||
Some command line tricks can make you even more productive on the Linux command line.
|
|
||||||
|
|
||||||
![](https://images.idgesg.net/images/article/2018/09/boy-jumping-off-swing-100772498-large.jpg)
|
|
||||||
|
|
||||||
Good Linux command line tricks don’t only save you time and trouble. They also help you remember and reuse complex commands, making it easier for you to focus on what you need to do, not how you should go about doing it. In this post, we’ll look at some handy command line tricks that you might come to appreciate.
|
|
||||||
|
|
||||||
### Editing your commands
|
|
||||||
|
|
||||||
When making changes to a command that you're about to run on the command line, you can move your cursor to the beginning or the end of the command line to facilitate your changes using the ^a (control key plus “a”) and ^e (control key plus “e”) sequences.
|
|
||||||
|
|
||||||
You can also fix and rerun a previously entered command with an easy text substitution by putting your before and after strings between **^** characters -- as in ^before^after^.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ eho hello world <== oops!
|
|
||||||
|
|
||||||
Command 'eho' not found, did you mean:
|
|
||||||
|
|
||||||
command 'echo' from deb coreutils
|
|
||||||
command 'who' from deb coreutils
|
|
||||||
|
|
||||||
Try: sudo apt install <deb name>
|
|
||||||
|
|
||||||
$ ^e^ec^ <== replace text
|
|
||||||
echo hello world
|
|
||||||
hello world
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### Logging into a remote system with just its name
|
|
||||||
|
|
||||||
If you log into other systems from the command line (I do this all the time), you might consider adding some aliases to your system to supply the details. Your alias can provide the username you want to use (which may or may not be the same as your username on your local system) and the identity of the remote server. Use an alias server_name=’ssh -v -l username IP-address' type of command like this:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ alias butterfly=”ssh -v -l jdoe 192.168.0.11”
|
|
||||||
```
|
|
||||||
|
|
||||||
You can use the system name in place of the IP address if it’s listed in your /etc/hosts file or available through your DNS server.
|
|
||||||
|
|
||||||
And remember you can list your aliases with the **alias** command.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ alias
|
|
||||||
alias butterfly='ssh -v -l jdoe 192.168.0.11'
|
|
||||||
alias c='clear'
|
|
||||||
alias egrep='egrep --color=auto'
|
|
||||||
alias fgrep='fgrep --color=auto'
|
|
||||||
alias grep='grep --color=auto'
|
|
||||||
alias l='ls -CF'
|
|
||||||
alias la='ls -A'
|
|
||||||
alias list_repos='grep ^[^#] /etc/apt/sources.list /etc/apt/sources.list.d/*'
|
|
||||||
alias ll='ls -alF'
|
|
||||||
alias ls='ls --color=auto'
|
|
||||||
alias show_dimensions='xdpyinfo | grep '\''dimensions:'\'''
|
|
||||||
```
|
|
||||||
|
|
||||||
It's good practice to test new aliases and then add them to your ~/.bashrc or similar file to be sure they will be available any time you log in.
|
|
||||||
|
|
||||||
### Freezing and thawing out your terminal window
|
|
||||||
|
|
||||||
The ^s (control key plus “s”) sequence will stop a terminal from providing output by running an XOFF (transmit off) flow control. This affects PuTTY sessions, as well as terminal windows on your desktop. Sometimes typed by mistake, however, the way to make the terminal window responsive again is to enter ^q (control key plus “q”). The only real trick here is remembering ^q since you aren't very likely run into this situation very often.
|
|
||||||
|
|
||||||
### Repeating commands
|
|
||||||
|
|
||||||
Linux provides many ways to reuse commands. The key to command reuse is your history buffer and the commands it collects for you. The easiest way to repeat a command is to type an ! followed by the beginning letters of a recently used command. Another is to press the up-arrow on your keyboard until you see the command you want to reuse and then press enter. You can also display previously entered commands and then type ! followed by the number shown next to the command you want to reuse in the displayed command history entries.
|
|
||||||
|
|
||||||
```
|
|
||||||
!! <== repeat previous command
|
|
||||||
!ec <== repeat last command that started with "ec"
|
|
||||||
!76 <== repeat command #76 from command history
|
|
||||||
```
|
|
||||||
|
|
||||||
### Watching a log file for updates
|
|
||||||
|
|
||||||
Commands such as tail -f /var/log/syslog will show you lines as they are being added to the specified log file — very useful if you are waiting for some particular activity or want to track what’s happening right now. The command will show the end of the file and then additional lines as they are added.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ tail -f /var/log/auth.log
|
|
||||||
Sep 17 09:41:01 fly CRON[8071]: pam_unix(cron:session): session closed for user smmsp
|
|
||||||
Sep 17 09:45:01 fly CRON[8115]: pam_unix(cron:session): session opened for user root
|
|
||||||
Sep 17 09:45:01 fly CRON[8115]: pam_unix(cron:session): session closed for user root
|
|
||||||
Sep 17 09:47:00 fly sshd[8124]: Accepted password for shs from 192.168.0.22 port 47792
|
|
||||||
Sep 17 09:47:00 fly sshd[8124]: pam_unix(sshd:session): session opened for user shs by
|
|
||||||
Sep 17 09:47:00 fly systemd-logind[776]: New session 215 of user shs.
|
|
||||||
Sep 17 09:55:01 fly CRON[8208]: pam_unix(cron:session): session opened for user root
|
|
||||||
Sep 17 09:55:01 fly CRON[8208]: pam_unix(cron:session): session closed for user root
|
|
||||||
<== waits for additional lines to be added
|
|
||||||
```
|
|
||||||
|
|
||||||
### Asking for help
|
|
||||||
|
|
||||||
For most Linux commands, you can enter the name of the command followed by the option **\--help** to get some fairly succinct information on what the command does and how to use it. Less extensive than the man command, the --help option often tells you just what you need to know without expanding on all of the options available.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ mkdir --help
|
|
||||||
Usage: mkdir [OPTION]... DIRECTORY...
|
|
||||||
Create the DIRECTORY(ies), if they do not already exist.
|
|
||||||
|
|
||||||
Mandatory arguments to long options are mandatory for short options too.
|
|
||||||
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
|
|
||||||
-p, --parents no error if existing, make parent directories as needed
|
|
||||||
-v, --verbose print a message for each created directory
|
|
||||||
-Z set SELinux security context of each created directory
|
|
||||||
to the default type
|
|
||||||
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
|
|
||||||
or SMACK security context to CTX
|
|
||||||
--help display this help and exit
|
|
||||||
--version output version information and exit
|
|
||||||
|
|
||||||
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
|
|
||||||
Full documentation at: <http://www.gnu.org/software/coreutils/mkdir>
|
|
||||||
or available locally via: info '(coreutils) mkdir invocation'
|
|
||||||
```
|
|
||||||
|
|
||||||
### Removing files with care
|
|
||||||
|
|
||||||
To add a little caution to your use of the rm command, you can set it up with an alias that asks you to confirm your request to delete files before it goes ahead and deletes them. Some sysadmins make this the default. In that case, you might like the next option even more.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ rm -i <== prompt for confirmation
|
|
||||||
```
|
|
||||||
|
|
||||||
### Turning off aliases
|
|
||||||
|
|
||||||
You can always disable an alias interactively by using the unalias command. It doesn’t change the configuration of the alias in question; it just disables it until the next time you log in or source the file in which the alias is set up.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ unalias rm
|
|
||||||
```
|
|
||||||
|
|
||||||
If the **rm -i** alias is set up as the default and you prefer to never have to provide confirmation before deleting files, you can put your **unalias** command in one of your startup files (e.g., ~/.bashrc).
|
|
||||||
|
|
||||||
### Remembering to use sudo
|
|
||||||
|
|
||||||
If you often forget to precede commands that only root can run with “sudo”, there are two things you can do. You can take advantage of your command history by using the “sudo !!” (use sudo to run your most recent command with sudo prepended to it), or you can turn some of these commands into aliases with the required "sudo" attached.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ alias update=’sudo apt update’
|
|
||||||
```
|
|
||||||
|
|
||||||
### More complex tricks
|
|
||||||
|
|
||||||
Some useful command line tricks require a little more than a clever alias. An alias, after all, replaces a command, often inserting options so you don't have to enter them and allowing you to tack on additional information. If you want something more complex than an alias can manage, you can write a simple script or add a function to your .bashrc or other start-up file. The function below, for example, creates a directory and moves you into it. Once it's been set up, source your .bashrc or other file and you can use commands such as "md temp" to set up a directory and cd into it.
|
|
||||||
|
|
||||||
```
|
|
||||||
md () { mkdir -p "$@" && cd "$1"; }
|
|
||||||
```
|
|
||||||
|
|
||||||
### Wrap-up
|
|
||||||
|
|
||||||
Working on the Linux command line remains one of the most productive and enjoyable ways to get work done on my Linux systems, but a group of command line tricks and clever aliases can make that experience even better.
|
|
||||||
|
|
||||||
Join the Network World communities on [Facebook][1] and [LinkedIn][2] to comment on topics that are top of mind.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.networkworld.com/article/3305811/linux/linux-tricks-that-even-you-can-love.html
|
|
||||||
|
|
||||||
作者:[Sandra Henry-Stocker][a]
|
|
||||||
选题:[lujun9972](https://github.com/lujun9972)
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
|
||||||
[1]: https://www.facebook.com/NetworkWorld/
|
|
||||||
[2]: https://www.linkedin.com/company/network-world
|
|
@ -0,0 +1,170 @@
|
|||||||
|
让你提高效率的 Linux 技巧
|
||||||
|
======
|
||||||
|
想要在 Linux 命令行工作中提高效率,你需要使用一些技巧。
|
||||||
|
|
||||||
|
![](https://images.idgesg.net/images/article/2018/09/boy-jumping-off-swing-100772498-large.jpg)
|
||||||
|
|
||||||
|
巧妙的 Linux 命令行技巧能让你节省时间、避免出错,还能让你记住和复用各种复杂的命令,专注在需要做的事情本身,而不是做事的方式。以下介绍一些好用的命令行技巧。
|
||||||
|
|
||||||
|
### 命令编辑
|
||||||
|
|
||||||
|
如果要对一个已输入的命令进行修改,可以使用 ^a(ctrl + a)或 ^e(ctrl + e)将光标快速移动到命令的开头或命令的末尾。
|
||||||
|
|
||||||
|
还可以使用 `^` 字符实现对上一个命令的文本替换并重新执行命令,例如 `^before^after^` 相当于把上一个命令中的 `before` 替换为 `after` 然后重新执行一次。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ eho hello world <== 错误的命令
|
||||||
|
|
||||||
|
Command 'eho' not found, did you mean:
|
||||||
|
|
||||||
|
command 'echo' from deb coreutils
|
||||||
|
command 'who' from deb coreutils
|
||||||
|
|
||||||
|
Try: sudo apt install <deb name>
|
||||||
|
|
||||||
|
$ ^e^ec^ <== 替换
|
||||||
|
echo hello world
|
||||||
|
hello world
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### 使用远程机器的名称登录到机器上
|
||||||
|
|
||||||
|
如果使用命令行登录其它机器上,可以考虑添加别名。在别名中,可以填入需要登录的用户名(与本地系统上的用户名可能相同,也可能不同)以及远程机器的登录信息。例如使用 `server_name ='ssh -v -l username IP-address'` 这样的别名命令:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ alias butterfly=”ssh -v -l jdoe 192.168.0.11”
|
||||||
|
```
|
||||||
|
|
||||||
|
也可以通过在 `/etc/hosts` 文件中添加记录或者在 DNS 服务器中加入解析记录来把 IP 地址替换成易记的机器名称。
|
||||||
|
|
||||||
|
执行 `alias` 命令可以列出机器上已有的别名。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ alias
|
||||||
|
alias butterfly='ssh -v -l jdoe 192.168.0.11'
|
||||||
|
alias c='clear'
|
||||||
|
alias egrep='egrep --color=auto'
|
||||||
|
alias fgrep='fgrep --color=auto'
|
||||||
|
alias grep='grep --color=auto'
|
||||||
|
alias l='ls -CF'
|
||||||
|
alias la='ls -A'
|
||||||
|
alias list_repos='grep ^[^#] /etc/apt/sources.list /etc/apt/sources.list.d/*'
|
||||||
|
alias ll='ls -alF'
|
||||||
|
alias ls='ls --color=auto'
|
||||||
|
alias show_dimensions='xdpyinfo | grep '\''dimensions:'\'''
|
||||||
|
```
|
||||||
|
|
||||||
|
只要将新的别名添加到 `~/.bashrc` 或类似的文件中,就可以让别名在每次登录后都能立即生效。
|
||||||
|
|
||||||
|
### 冻结、解冻终端界面
|
||||||
|
|
||||||
|
^s(ctrl + s)将通过执行流量控制命令 XOFF 来停止终端输出内容,这会对 PuTTY 会话和桌面终端窗口产生影响。如果误输入了这个命令,可以使用 ^q(ctrl + q)让终端重新响应。所以只需要记住^q 这个组合键就可以了,毕竟这种情况并不多见。
|
||||||
|
|
||||||
|
### 复用命令
|
||||||
|
|
||||||
|
Linux 提供了很多让用户复用命令的方法,其核心是通过历史缓冲区收集执行过的命令。复用命令的最简单方法是输入 `!` 然后接最近使用过的命令的开头字母;当然也可以按键盘上的向上箭头,直到看到要复用的命令,然后按 Enter 键。还可以先使用 `history` 显示命令历史,然后输入 `!` 后面再接命令历史记录中需要复用的命令旁边的数字。
|
||||||
|
|
||||||
|
```
|
||||||
|
!! <== 复用上一条命令
|
||||||
|
!ec <== 复用上一条以 “ec” 开头的命令
|
||||||
|
!76 <== 复用命令历史中的 76 号命令
|
||||||
|
```
|
||||||
|
|
||||||
|
### 查看日志文件并动态显示更新内容
|
||||||
|
|
||||||
|
使用形如 `tail -f /var/log/syslog` 的命令可以查看指定的日志文件,并动态显示文件中增加的内容,需要监控向日志文件中追加内容的的事件时相当有用。这个命令会输出文件内容的末尾部分,并逐渐显示新增的内容。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ tail -f /var/log/auth.log
|
||||||
|
Sep 17 09:41:01 fly CRON[8071]: pam_unix(cron:session): session closed for user smmsp
|
||||||
|
Sep 17 09:45:01 fly CRON[8115]: pam_unix(cron:session): session opened for user root
|
||||||
|
Sep 17 09:45:01 fly CRON[8115]: pam_unix(cron:session): session closed for user root
|
||||||
|
Sep 17 09:47:00 fly sshd[8124]: Accepted password for shs from 192.168.0.22 port 47792
|
||||||
|
Sep 17 09:47:00 fly sshd[8124]: pam_unix(sshd:session): session opened for user shs by
|
||||||
|
Sep 17 09:47:00 fly systemd-logind[776]: New session 215 of user shs.
|
||||||
|
Sep 17 09:55:01 fly CRON[8208]: pam_unix(cron:session): session opened for user root
|
||||||
|
Sep 17 09:55:01 fly CRON[8208]: pam_unix(cron:session): session closed for user root
|
||||||
|
<== 等待显示追加的内容
|
||||||
|
```
|
||||||
|
|
||||||
|
### 寻求帮助
|
||||||
|
|
||||||
|
对于大多数 Linux 命令,都可以通过在输入命令后加上选项 `--help` 来获得这个命令的作用、用法以及它的一些相关信息。除了 `man` 命令之外, `--help` 选项可以让你在不使用所有扩展选项的情况下获取到所需要的内容。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ mkdir --help
|
||||||
|
Usage: mkdir [OPTION]... DIRECTORY...
|
||||||
|
Create the DIRECTORY(ies), if they do not already exist.
|
||||||
|
|
||||||
|
Mandatory arguments to long options are mandatory for short options too.
|
||||||
|
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
|
||||||
|
-p, --parents no error if existing, make parent directories as needed
|
||||||
|
-v, --verbose print a message for each created directory
|
||||||
|
-Z set SELinux security context of each created directory
|
||||||
|
to the default type
|
||||||
|
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
|
||||||
|
or SMACK security context to CTX
|
||||||
|
--help display this help and exit
|
||||||
|
--version output version information and exit
|
||||||
|
|
||||||
|
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
|
||||||
|
Full documentation at: <http://www.gnu.org/software/coreutils/mkdir>
|
||||||
|
or available locally via: info '(coreutils) mkdir invocation'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 谨慎删除文件
|
||||||
|
|
||||||
|
如果要谨慎使用 `rm` 命令,可以为它设置一个别名,在删除文件之前需要进行确认才能删除。有些系统管理员会默认使用这个别名,对于这种情况,你可能需要看看下一个技巧。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ rm -i <== 请求确认
|
||||||
|
```
|
||||||
|
|
||||||
|
### 关闭别名
|
||||||
|
|
||||||
|
你可以使用 `unalias` 命令以交互方式禁用别名。它不会更改别名的配置,而仅仅是暂时禁用,直到下次登录或重新设置了这一个别名才会重新生效。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ unalias rm
|
||||||
|
```
|
||||||
|
|
||||||
|
如果已经将 `rm -i` 默认设置为 `rm` 的别名,但你希望在删除文件之前不必进行确认,则可以将 `unalias` 命令放在一个启动文件(例如 ~/.bashrc)中。
|
||||||
|
|
||||||
|
### 使用 sudo
|
||||||
|
|
||||||
|
如果你经常在只有 root 用户才能执行的命令前忘记使用 `sudo`,这里有两个方法可以解决。一是利用命令历史记录,可以使用 `sudo !!`(使用 `!!` 来运行最近的命令,并在前面添加 `sudo`)来重复执行,二是设置一些附加了所需 `sudo` 的命令别名。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ alias update=’sudo apt update’
|
||||||
|
```
|
||||||
|
|
||||||
|
### 更复杂的技巧
|
||||||
|
|
||||||
|
有时命令行技巧并不仅仅是一个别名。毕竟,别名能帮你做的只有替换命令以及增加一些命令参数,节省了输入的时间。但如果需要比别名更复杂功能,可以通过编写脚本、向 `.bashrc` 或其他启动文件添加函数来实现。例如,下面这个函数会在创建一个目录后进入到这个目录下。在设置完毕后,执行 `source .bashrc`,就可以使用 `md temp` 这样的命令来创建目录立即进入这个目录下。
|
||||||
|
|
||||||
|
```
|
||||||
|
md () { mkdir -p "$@" && cd "$1"; }
|
||||||
|
```
|
||||||
|
|
||||||
|
### 总结
|
||||||
|
|
||||||
|
使用 Linux 命令行是在 Linux 系统上工作最有效也最有趣的方法,但配合命令行技巧和巧妙的别名可以让你获得更好的体验。
|
||||||
|
|
||||||
|
加入 [Facebook][1] 和 [LinkedIn][2] 上的 Network World 社区可以和我们一起讨论。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.networkworld.com/article/3305811/linux/linux-tricks-that-even-you-can-love.html
|
||||||
|
|
||||||
|
作者:[Sandra Henry-Stocker][a]
|
||||||
|
选题:[lujun9972](https://github.com/lujun9972)
|
||||||
|
译者:[HankChow](https://github.com/HankChow)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||||
|
[1]: https://www.facebook.com/NetworkWorld/
|
||||||
|
[2]: https://www.linkedin.com/company/network-world
|
||||||
|
|
Loading…
Reference in New Issue
Block a user