mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
[Translated] 20150527 3 Useful Hacks Every Linux User Must Know.md
This commit is contained in:
parent
9e0e5e4a48
commit
5568776359
@ -1,81 +0,0 @@
|
||||
Translating by goreliu ...
|
||||
|
||||
3 Useful Hacks Every Linux User Must Know
|
||||
================================================================================
|
||||
The world of Linux is filled with so much fun and interesting stuffs, the more we go in, the more we find stuffs. In our efforts to bring those little hacks and tips for you that makes you different from others, here we have came up with three little tricks.
|
||||
|
||||
### 1. How to Schedule a Linux Job Without Cron ###
|
||||
|
||||
Scheduling a job/command in Linux is acronym to cron. Whenever we need to schedule a job, we call cron, but do you know we can schedule a job at a later time without corn? You can do it as suggested below..
|
||||
|
||||
Run a command (say date) every 5 sec and write the output to a file (say date.txt). To achieve this scenario, we need to run the below one liner script directly on the command prompt.
|
||||
|
||||
$ while true; do date >> date.txt ; sleep 5 ; done &
|
||||
|
||||
Anatomy of the above one liner script:
|
||||
|
||||
- while true – Ask script to run while the condition is true, it acts as a loop which makes the command to run again-and-again or say in a loop.
|
||||
- do – do perform what follows, ie., execute command or set of commands that lies ahead of do statement.
|
||||
- date >> date.txt – here the output of date command is being written to a file date.txt. Also note that we have used >> and not >.
|
||||
- >> ensures that the file (date.txt) is not overwritten every time the script execute. It just append the changes. Whereas > overwrite the file again and again.
|
||||
- sleep 5 – It ask the shell to keep a time difference of 5 seconds before it executed again. Note the time here is always measured in seconds. Say if you want to execute the command every 6 minutes, you should use (6*60) 360, in succession of sleep.
|
||||
- done – marks the end of while loop.
|
||||
- & – Put the whole process in loop to background.
|
||||
|
||||
Similarly, we can execute any script in the same manner. Here is the command to call a script after certain interval (say 100 sec) and the name of script is `script_name.sh`.
|
||||
|
||||
Also worth mentioning that the script above should be run in the directory where the script to be called lies, else you need to provide full path (`/home/$USER/…/script_name.sh`). The syntax for calling script at above described interval is:
|
||||
|
||||
$ while true; do /bin/sh script_name.sh ; sleep 100 ; done &
|
||||
|
||||
**Conclusion**: The above one liner is not a replacement of Cron, because Cron utility supports a whole lots of options, as compared and is very flexible as well as customizable. However if we want to run certain test cases or I/O benchmark, then the above singe command will serve the purpose.
|
||||
|
||||
Read Also: [11 Linux Cron Job Scheduling Examples][1]
|
||||
|
||||
### 2. How to Clear Terminal without Using ‘clear’ Command ###
|
||||
|
||||
What we do to clear the screen? Well you may think how silly it is to ask such question. Well, we all know it’s ‘clear‘ command. However if we make a habit of using key combination ‘ctrl+l‘ to clear terminal, we will save a lot of time of ours.
|
||||
|
||||
Key combination ‘Ctrl+l‘ has the same effect as ‘clear‘ command. So from next time use ctrl+l to clear your Linux Command Line Interface.
|
||||
|
||||
**Conclusion**: Since ctrl+l is a key combination, so we can not use it inside a script. If we need to clear screen inside a shell script, call command ‘clear’, for all other cases I could think of now, ctrl+l is more than sufficient.
|
||||
|
||||
### 3. Run a command and come back to the current working directory automatically. ###
|
||||
|
||||
Well this is an amazing hack not many people know. You may run a command no matter what it return back to the current directory. All you need to do is to run the command in parentheses i.e., in between `( and )`.
|
||||
|
||||
Let see the example,
|
||||
|
||||
avi@deb:~$ (cd /home/avi/Downloads/)
|
||||
|
||||
#### Sample Output ####
|
||||
|
||||
avi@deb:~
|
||||
|
||||
First it cd to directory Downloads and then again return back to home directory in one go. May be you believe that the command didn’t executed and for some reason one or another it is not throwing error, since there is no change in prompt. Lets do a little more tweak..
|
||||
|
||||
avi@deb:~$ (cd /home/avi/Downloads/ && ls -l)
|
||||
|
||||
#### Sample Output ####
|
||||
|
||||
-rw-r----- 1 avi avi 54272 May 3 18:37 text1.txt
|
||||
-rw-r----- 1 avi avi 54272 May 3 18:37 text2.txt
|
||||
-rw-r----- 1 avi avi 54272 May 3 18:37 text3.txt
|
||||
avi@deb:~$
|
||||
|
||||
So in the above command it first changed the current directory to Downloads and then list the content of that directory before returning back to current directory. Also, it proves that command executed successfully. You may run any sort of command in the parentheses and return back to your current working directory without a hitch.
|
||||
|
||||
That’s all for now, if you know any such Linux hacks or tricks you may share with us via our comment section and don’t forget to share this article with your friends….
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/useful-linux-hacks-commands/
|
||||
|
||||
作者:[Avishek Kumar][a]
|
||||
译者:[goreliu](https://github.com/goreliu)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/avishek/
|
||||
[1]:http://www.tecmint.com/11-cron-scheduling-task-examples-in-linux/
|
@ -0,0 +1,78 @@
|
||||
每个Linux用户都应该知道的3个有用技巧
|
||||
================================================================================
|
||||
Linux世界充满了乐趣,我们越深入进去,就会发现越多有趣的事物。我们会努力给你提供一些小技巧,让你和其他人有所不同,下面就是我们准备的3个小技巧。
|
||||
|
||||
### 1. 如何在不使用Cron的情况调度Linux下的作业 ###
|
||||
在Linux下,调度一个作业/命令可以缩写为Cron。当我们需要调度一个作业时,我们会使用Cron,但你知道我们在不使用Cron的情况也可以调度一个在将来时间运行的作业吗?你可以按照如下建议操作……
|
||||
|
||||
每5秒钟运行一个命令(date)然后将结果写入到一个文件(data.txt)。为了实现这一点,我们可以直接在命令提示符运行如下单行脚本。
|
||||
|
||||
$ while true; do date >> date.txt ; sleep 5 ; done &
|
||||
|
||||
上述脚本的解释:
|
||||
|
||||
- `while true` :让脚本进入一个条件总为真的循环中,也就是制造一个死循环,将里边的命令一遍遍地重复运行。
|
||||
- `do` :`do`是`while`语句中的关键字,它之后的命令会被执行,在它后边可以放置一个或一系列命令。
|
||||
- `date >> date.txt` :运行date命令,并将其输出写入到data.txt文件中。注意我们使用`>>`,而不是`>`。
|
||||
- `>>` :对文件(date.txt)进行追加写的操作,这样每次运行命令后,输出内容会被追加到文件中。如果使用`>`的话,则会一遍遍地覆盖之前的内容。
|
||||
- `sleep 5` :让脚本处于5秒睡眠状态,然后再运行之后的命令。注意这里的时间单位只能用秒。也就是说如果你想让命令每6分钟运行一次,你应该使用`sleep 360`。
|
||||
- `done` :`while`循环语句块结束的标记。
|
||||
- `&` :将整个进程放到后台运行。
|
||||
|
||||
类似地,我们可以这样运行任何脚本。下边的例子是每100秒运行一个名为`script_name.sh`的脚本。
|
||||
|
||||
另外值得一提的是上边的脚本文件必须处于当前目录中,否则需要使用完整路径(`/home/$USER/…/script_name.sh`)。实现如上功能的单行脚本如下:
|
||||
|
||||
$ while true; do /bin/sh script_name.sh ; sleep 100 ; done &
|
||||
|
||||
**总结**:上述的单行脚本并不是Cron的替代品,因为Cron工具支持众多选项,更加灵活,可定制性也更高。然而如果我们想运行某些测试,比如I/O评测,上述的单行脚本也管用。
|
||||
|
||||
还可以参考:[11 Linux Cron Job Scheduling Examples][1]
|
||||
|
||||
### 2. 如何不使用clear命令清空终端的内容 ###
|
||||
|
||||
你如何清空终端的内容?你可能会认为这是一个傻问题。好吧,大家都清楚可以使用`clear`命令。如果养成使用`ctrl + l`快捷键的习惯,我们会节省大量时间。
|
||||
|
||||
`Ctrl + l`快捷键的效果和`clear`命令一样。所以下一次你就可以使用`ctrl + l`来清空终端的内容了。
|
||||
|
||||
**总结**:因为`ctrl + l`是一个快捷键,我们不可以在脚本中使用。所以如果我们需要在脚本中清空屏幕内容,还是需要使用`clear`命令。但我能想到的所有其他情况,`ctrl + l`都更加有效。
|
||||
|
||||
### 3. 运行一个命令,然后自动回到当前的工作目录 ###
|
||||
|
||||
这是一个很多人可能不知道的令人吃惊的技巧。你可能想运行任何一个命令,然后再回到当前目录。你只需要将命令放在一个圆括号里。
|
||||
|
||||
我们来看一个例子:
|
||||
|
||||
avi@deb:~$ (cd /home/avi/Downloads/)
|
||||
|
||||
#### 示例输出 ####
|
||||
|
||||
avi@deb:~
|
||||
|
||||
它首先会cd到Downloads目录,然后又回到了之前的家目录。也许你认为里边的命令根本没有执行,或者是出了某种错误,因为从命令提示符看不出任何变化。让我们简单修改一下这个命令:
|
||||
|
||||
avi@deb:~$ (cd /home/avi/Downloads/ && ls -l)
|
||||
|
||||
#### 示例输出 ####
|
||||
|
||||
-rw-r----- 1 avi avi 54272 May 3 18:37 text1.txt
|
||||
-rw-r----- 1 avi avi 54272 May 3 18:37 text2.txt
|
||||
-rw-r----- 1 avi avi 54272 May 3 18:37 text3.txt
|
||||
avi@deb:~$
|
||||
|
||||
在上述命令中,它首先进入Downloads目录,然后列出文件内容,最后又回到了当前目录。并且它证明了命令成功执行了。你可以在括号中包含任何命令,执行完都会顺利返回到当前目录。
|
||||
|
||||
这就是全部内容了,如果你知道任何类似的Linux技巧,你可以在文章下面的评论框中分享给我们,不要忘记将本文和朋友分享 :)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/useful-linux-hacks-commands/
|
||||
|
||||
作者:[Avishek Kumar][a]
|
||||
译者:[goreliu](https://github.com/goreliu)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/avishek/
|
||||
[1]:http://www.tecmint.com/11-cron-scheduling-task-examples-in-linux/
|
Loading…
Reference in New Issue
Block a user