mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
commit
39a3592c29
@ -1,104 +0,0 @@
|
||||
ucasFL translating
|
||||
|
||||
Seven things about Linux you may not have known so far
|
||||
============================================================
|
||||
|
||||
![Hidden features of Linux](https://i0.wp.com/opensourceforu.com/wp-content/uploads/2015/12/Linux-Kernel-sys-visual5.jpg?resize=750%2C563)
|
||||
|
||||
One of the coolest parts about using Linux is the knowledge you gain over time. Each day, you’re likely to come across a new utility or maybe just an unfamiliar flag that does something helpful. These bits and pieces aren’t always life-changing, but they are the building blocks for expertise.
|
||||
|
||||
Even experts don’t know that all, though. No matter how much experience you might have, there is always more to learn, so we’ve put together this list of seven things about Linux you may not have known.
|
||||
|
||||
**There is an interactive mode for command history**
|
||||
|
||||
You’re probably familiar with `history`, which reads your bash history to `stdout`in a handy numbered list. However, if you find yourself searching for a specific URL in a sea of `curl` commands, this list isn’t always easy to read.
|
||||
|
||||
As an alternative, Linux comes with an interactive reverse search that helps you to avoid the headache. You can activate it with `ctrl+r`. This enables an interactive prompt that will search backwards through your bash history for a string you provide. You can cycle back through older commands by pressing `ctrl+r` again or cycle forward using `ctrl+s`.
|
||||
|
||||
Note that `ctrl+s` sometimes collides with XON/XOFF flow control, you can disable the same by running `stty –ixon`. This will usually be fine on your own personal machines, but as always, make sure you don’t need XON/XOFF before you disable it.
|
||||
|
||||
**Cron isn’t the only way to schedule jobs**
|
||||
|
||||
Cron jobs are incredibly useful for sysadmins of any level — from absolute beginner to seasoned expert. But if you need to schedule a one-off task, the `at` command provides a quick way to create jobs without touching your crontab.
|
||||
|
||||
Run the `at` command followed by the time you’d like to run your job. The time is flexible and accepts a variety of time formats. A few examples include:
|
||||
|
||||
“at 12:00 PM September 30 2017”
|
||||
“at now + 1 hour”
|
||||
“at 9:00 AM tomorrow”
|
||||
|
||||
Once you enter the `at` command with arguments, you’ll be prompted for the commands you would like to run on your Linux machine. This can be a backup script, a set of maintenance tasks or even a normal bash command. To finalise the job, enter `ctrl+d`.
|
||||
|
||||
Further, you can view all jobs for the current user with `atq` or for all users with `sudo atq`. This will display the scheduled jobs with an ID for each one. To cancel a scheduled job, use `atrm` with the job ID as an argument.
|
||||
|
||||
**You can search commands by function, not just name**
|
||||
|
||||
Remembering command names can be difficult, especially as a beginner. Luckily, Linux comes with a tool to search the man pages by both name and description.
|
||||
|
||||
Next time, if you would struggle to remember the name of the tool you want to use, you can try `apropos` with a description of what you are trying to achieve. For instance, `apropos build filesystem` will return a list of names and descriptions of utilities that include the words “build” or “filesystem.”
|
||||
|
||||
The `apropos` command accepts a string or multiple strings as arguments, but it also has options like `-r`, which allows you to search by regular expression.
|
||||
|
||||
**An alternatives system allows you to manage versions**
|
||||
|
||||
If you have ever been involved in software development, you know the importance of managing support for different versions of a language across projects. Many Linux distributions have tools to handle different versions built in.
|
||||
|
||||
Executables like `java` are often symbolically linked to `/etc/alternatives`. This directory, in turn, stores symlinks to binary files and offers an interface for managing those links. Java is probably the most commonly used language with alternatives, but with a bit of configuration, it can also act as a replacement for applications like NVM and RVM (version managers for NodeJS and Ruby, respectively).
|
||||
|
||||
In Debian-based systems, you can create and manage these links with `update-alternatives`. In CentOS, the tool is simply called `alternatives`. By changing the links in your alternatives file, you can install multiple versions of a language and use different binaries in different situations. Of course, this is not just limited to programming languages. The alternatives system also offers support for any executable that you might want to run from the command line.
|
||||
|
||||
**Shred command is a more secure way to delete files**
|
||||
|
||||
Most of us probably use `rm` to delete files. But where do those files go? The truth is that `rm` might not be doing what you think — it simply removes the hardlink between the filesystem and the data on disk. The 1s and 0s remain in storage until they are overwritten by another application. For extremely sensitive data, this can be a huge security concern.
|
||||
|
||||
The `shred` command is a hardcore version of `rm`. When you shred a file, its data is randomly overwritten multiple times. There is even an option to run a final pass and “zero out” the data after the random overwrites.
|
||||
|
||||
To securely delete a file and overwrite it with zeros:
|
||||
|
||||
`shred -u -z [file name]`
|
||||
|
||||
You can also add the `-n` option with a number as an argument. This allows you to specify how many iterations will be made when randomly overwriting the data.
|
||||
|
||||
**Autocorrect to avoid frustration when typing long filepaths**
|
||||
|
||||
How many times have you typed out an absolute file path only to see the “No such file or directory” message? Even the best of us know the pain of a typo in a long string. Fortunately, there is an easy fix.
|
||||
|
||||
The built-in `shopt` command allows you to set different options to change the behavior of your shell. Setting the `cdspell` option is a simple way to avoid the headache of one-letter mistakes when typing out file paths. You can turn it on by running `shopt -s cdspell`. Once it has been activated, file paths will autocorrect to the closest match when you try to change directories.
|
||||
|
||||
Shell options are a great way to save time (not to mention hassle), and there are plenty of others. To see a full list of options on your system, run `shopt` with no arguments. Be aware that this is a feature of bash, so if you’re running zsh or another alternative shell, you may not have access to it.
|
||||
|
||||
**Easily return to the current directory with subshells**
|
||||
|
||||
If you’ve ever had to configure a system with even a moderate level of complexity, you’ve probably found yourself changing directories so often that it can be hard to keep track of where you are. Wouldn’t it be great if there was a way to automatically return to your current location after running a command?
|
||||
|
||||
Linux actually offers a solution to this problem, and it’s dead simple. If you need to `cd` to another location to do something and then return to your current working directory, wrap the command in parentheses. Here’s an example you can try on your machine. Make a note of your current directory, then run:
|
||||
|
||||
`(cd /etc && ls -a)`
|
||||
|
||||
This will output the contents of your `/etc` directory. Now recheck your current working directory. It should be the same one you were in before, not `/etc`.
|
||||
|
||||
So how does it work? Running a command in parentheses spawns a subshell or a forked copy of the current shell process. The subshell has access to all of the parent’s variables, but not vice versa, so keep that in mind if you’re running an especially complex one-liner.
|
||||
|
||||
Subshells are often used in scripts for parallel processing, but you can bring that same power to the command line to make your life easier when navigating the filesystem.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
Phil Zona
|
||||
|
||||
The author is a technical writer for Linux Academy. He has written guides and tutorials on AWS, Microsoft Azure, and Linux systems administration. He also manages the Cloud Assessments blog, which aims to help individuals reach their career goals through technology.
|
||||
|
||||
-----------------
|
||||
|
||||
via: http://opensourceforu.com/2017/09/top-7-things-linux-may-not-known-far/
|
||||
|
||||
作者:[PHIL ZONA ][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://opensourceforu.com/author/phil-zona/
|
||||
[1]:http://opensourceforu.com/2017/09/top-7-things-linux-may-not-known-far/#disqus_thread
|
||||
[2]:http://opensourceforu.com/author/phil-zona/
|
@ -0,0 +1,104 @@
|
||||
关于 Linux 你可能不是非常了解的七件事
|
||||
===
|
||||
|
||||
![Hidden features of Linux](https://i0.wp.com/opensourceforu.com/wp-content/uploads/2015/12/Linux-Kernel-sys-visual5.jpg?resize=750%2C563)
|
||||
|
||||
使用 Linux 最酷的事情之一就是随着时间的推移,你可以不断获得新的知识。每天,你都可能会遇到一个新的实用工具,或者只是一个不太熟悉的奇技淫巧,但是却非常有用。这些零碎的东西并不总是能够改变生活,但是却是专业知识的基础。
|
||||
|
||||
即使是专家,也不可能事事皆知。无论你有多少经验,可能总会有更多的东西需要你去学习。所以,在这儿我列出了七件关于 Linux 你可能不知道的事情。
|
||||
|
||||
### 有一个查找命令历史的交互模式
|
||||
|
||||
你可能对 `history` 命令非常熟悉,它会读取 bash 历史,然后以编号列表的方式输出到标准输出(`stdout`)。然而,如果你在 `curl` 命令的海洋里寻找一个特定的链接(URL),那么这个列表并不总是非常容易阅读的。
|
||||
|
||||
你还可以有另一个选择,Linux 有一个交互式的反向搜索可以帮助你解决这个问题。你可以通过快捷键 `CTRL+R`启动交互模式,然后进入一个交互提示中,它将会根据你提供的字符串来向后搜索 bash 历史,你可以通过再次按下 `CTRL+R` 向后搜索更老的命令,或者按下 `CTRL+S` 向前搜索。
|
||||
|
||||
注意,`CTRL+S` 有时会与 XON/XOFF 流控制冲突,即 XON/OFF 流控制也会使用该快捷键。你可以通过运行 `stty -ixon` 命令来禁用该快捷键。在你的个人电脑上,这通常是有用的,但是在禁用前,确保你不需要 XON/XOFF 。
|
||||
|
||||
### Cron 不是安排任务的唯一方式
|
||||
|
||||
Cron jobs 对于任何水平的系统管理员,无论是毫无经验的初学者,还是经验丰富的专家来说,都是非常有用的。但是,如果你需要安排一个一次性的任务,那么 `at` 命令为你提供了一个快捷的方式来创建任务,从而你不需要接触 crontab 。
|
||||
|
||||
`at` 命令的运行方式是在后面紧跟着你想要运行任务的运行时间。时间是灵活的,因为它支持许多时间格式。包括下面这些例子:
|
||||
|
||||
“at 12:00 PM September 30 2017”
|
||||
“at now + 1 hour”
|
||||
“at 9:00 AM tomorrow”
|
||||
|
||||
当你以带参数的方式输入 `at` 命令以后,将会提示你该命令将在你的 Linux 系统上运行。这可能是一个备份脚本,一套维护任务,或者甚至是一个普通的 bash 命令。如果要结束任务,可以按 `CTRL+D` 。
|
||||
|
||||
另外,你可以使用 `atq` 命令查看当前用户的所有任务,或者使用 `sudo atq` 查看所有用户的任务。它将会展示出所有排定好的任务,并且每个任务都伴有一个 ID 。如果你想取消一个排定好的任务,可以使用 `atrm` 命令,并且以任务 ID 作为参数。
|
||||
|
||||
### 你可以通过函数搜索命令,而不仅仅是通过名字
|
||||
|
||||
记住命令的名字非常困难,特别是对于初学者来说。幸运的是,Linux 附带了一个通过名字和描述来搜索 man 页面的工具。
|
||||
|
||||
下次,如果你没有记住你想要使用的工具的名称,你可以尝试使用 `apropos` 命令加上你想要干的事情的描述。比如,`apropos build filesystem` 将会返回一系列工具的名字和描述,包括 build 和 filesystem 。
|
||||
|
||||
`apropos` 命令接受一个或多个字符串作为参数,但同时它也有其他参数,比如你可以使用 `-r` 参数,从而通过正则表达式来搜索。
|
||||
|
||||
### 有一个可供选择的系统允许你来管理系统版本
|
||||
|
||||
如果你曾进行过软件开发,你就会明白跨项目管理不同版本的语言的支持的重要性。许多 Linux 发行版都有工具可以来处理不同的内建版本。
|
||||
|
||||
可执行文件比如 `java` 往往象征性地链接到目录 `/etc/alternatives` 下。反过来,该目录会将符号链接存储为二进制文件并提供一个管理这些链接的接口。Java 可能是 alternatives 最常管理的语言,但是,经过一些配置,它也可以作为其他应用程序,比如 NVM 和 RVM 的替代品(NVM 和 RVM 分别是 NodeJS 和 Ruby 的版本管理器)。
|
||||
|
||||
在基于 Debian 的系统中,你可以使用 `update-alternatives` 命令创建和管理这些链接。在 CentOS 中,这个工具就叫做 `alternatives` 。通过更改你的 alternatives 文件中的链接,你便可以安装一个语言的多个版本并且在不同的情况下使用不同的二进制。该可供选择的系统也提供了对任何你可能在命令行运行的程序的支持。
|
||||
|
||||
### `shred` 命令是更加安全的删除文件方式
|
||||
|
||||
我们大多数时候总是使用 `rm` 命令来删除文件。但是文件去哪儿了呢?真相是 `rm` 命令所做的事情并不是像你所想像的那样,它仅仅删除了文件系统和硬盘上的数据的硬链接。硬盘上的数据依旧存在,直到被另一个应用重写覆盖。对于非常敏感的数据来说,这会带来一个很大的安全隐患。
|
||||
|
||||
`shred` 命令是 `rm` 命令的升级版。当你使用 `shred` 命令删除一个文件之后,文件中的数据会被随机多次重写。甚至有一个选项可以在随机重写之后对所有的数据进行清零。
|
||||
|
||||
如果你想安全的删除一个文件并且以零覆盖,那么可以使用下面的命令:
|
||||
|
||||
`shred -u -z [file name]`
|
||||
|
||||
同时,你也可以使用 `-n` 选项和一个数字作为参数,从而指定在随机覆盖数据的时候迭代多少次。
|
||||
|
||||
****
|
||||
|
||||
### 通过自动更正来避免输入很长的无效文件路径
|
||||
|
||||
有多少次,你输入一个文件的绝对路径,然而却看到“没有该文件或目录”的消息。任何人都会明白输入一个很长的字符串的痛苦。幸运的是,有一个很简单的解决办法。
|
||||
|
||||
内建的 `shopt` 命令允许你设置不同的选项来改变 shell 的行为。设置 `cdspell` 选项是避免输入文件路径时一个字母出错的头痛的一个简单方式。你可以通过运行 `shopt -s cdspell` 命令来启用该选项。启用该选项后,当你想要更改目录时,会自动更正为最匹配的目录。
|
||||
|
||||
Shell 选项是节省时间的一个好方法(更不用说减少麻烦),此外还有许许多多的其他选项。如果想查看你的系统中所有选项的完整列表,可以运行不带参数的 `shopt` 命令。需要注意的是,这是 bash 的特性,如果你运行 zsh 或者其他可供选择的 shell,可能无法访问。
|
||||
|
||||
****
|
||||
|
||||
### 通过子 shell 返回到当前目录
|
||||
|
||||
如果你曾经配置过一个中等难度的复杂系统,那么你可能会发现你需要频繁的更换目录,从而很难跟踪你所在的位置。如果在运行完一个命令后自动返回到当前位置,不是很好吗?
|
||||
|
||||
Linux 系统实际上提供了一个解决该问题的方法,并且非常简单。如果你想通过 `cd` 命令进入另一个目录完成一些任务,然后再返回当前工作目录,那么你可以将命令置于括号中。你可以在你的 Linux 系统上尝试下面这个命令。记住你当前的工作目录,然后运行:
|
||||
|
||||
`(cd /etc && ls -a)`
|
||||
|
||||
该命令会输出 `/etc` 目录的内容。现在,检查你的当前工作目录。它和执行该命令前的目录一样,而不是 `/etc` 目录。
|
||||
|
||||
它是如何工作的呢?运行一个括号中的命令会创建一个子 shell 或者一个当前 shell 进程的复刻。该子 shell 可以访问所有的父变量,反之亦然。所以请记住,你是在运行一个非常复杂的单命令。
|
||||
|
||||
在并行处理中经常使用子 shell ,但是在命令行中,它也能为你带来同样的力量,从而使你在浏览文件系统时更加容易。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
Phil Zona 是 Linux Academy 的技术作家。他编写了 AWS、Microsoft Azure 和 Linux 系统管理的指南和教程。他同时也管理着云评估博客,该博客旨在帮助个人通过技术实现他们的事业目标。
|
||||
|
||||
-----------------
|
||||
|
||||
via: http://opensourceforu.com/2017/09/top-7-things-linux-may-not-known-far/
|
||||
|
||||
作者:[PHIL ZONA][a]
|
||||
译者:[ucasFL](https://github.com/ucasFL)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://opensourceforu.com/author/phil-zona/
|
||||
[1]:http://opensourceforu.com/2017/09/top-7-things-linux-may-not-known-far/#disqus_thread
|
||||
[2]:http://opensourceforu.com/author/phil-zona/
|
Loading…
Reference in New Issue
Block a user