[Translation] & #160; Linux & #160; user's & #160; 3 & #160; command line tips and linux tips

Source: Internet
Author: User

3 command line tips and Linux tips for linux Users

The Linux world is full of fun. The deeper we get into it, the more interesting things we will find. We will try to provide you with some tips to make you different from others. Below are three tips we have prepared.

 

1. How to schedule tasks in Linux without using Cron

 

In Linux, scheduling a task/command is called Cron. When we need to schedule a task, we will use Cron, but do you know that we can schedule a task that runs in the future without Cron? You can follow the suggestions below ......

 

Run a command every five seconds (data.txt ). To achieve this, we can directly run the following single-line script at the command prompt.

 

$ While true; do date date.txt; sleep 5; done &

 

Explanation of the above script:

 

While true: Let the script enter a true loop, that is, to create an endless loop, and run the commands in it repeatedly.

 

Do: do is the keyword in the while statement. Its Subsequent commands will be executed, and one or more commands can be placed behind it.

 

Date date.txt: run the datecommand and write its output to the data.txt file. Note that we use "instead of">.

 

": Append the file (date.txt), so that after each command is run, the output content will be appended to the file. If> is used, the previous content will be overwritten over and over again.

 

Sleep 5: put the script in sleep state for 5 seconds, and then run the following command. Note that the time unit can only be seconds. That is to say, if you want to run the command every 6 minutes, you should use sleep 360.

 

Done: Mark of the end of the while LOOP statement block.

 

&: Run the entire process in the background.

 

Similarly, we can run any script like this. In the following example, a script named script_name.sh is executed every 100 seconds.

 

It is also worth mentioning that the script file mentioned above must be in the current directory; otherwise, the full path (/home/$ USER /... /Script_name.sh ). The single-line script that implements the preceding functions is as follows:

 

$ While true; do/bin/sh script_name.sh; sleep 100; done &

 

Summary: The preceding single-line script is not a substitute for Cron, because the Cron tool supports many options, which are more flexible and customizable. However, if we want to run some tests, such as I/O evaluation, the above single-line script also works.

 

For more information, see 11 scheduled scheduling tasks in Linux.

 

2. How to clear terminal content without using the clear command

 

How do you clear terminal content? You may think this is a silly question. All right, you know you can use the clear command. If we develop the habit of using the ctrl + l shortcut, we will save a lot of time.

 

The effect of the Ctrl + l shortcut key is the same as that of the clear command. So next time you can use ctrl + l to clear the terminal content.

 

Conclusion: ctrl + l is a shortcut key and cannot be used in scripts. Therefore, if we need to clear the Screen Content in the script, we still need to use the clear command. However, in all other cases that I can think of, ctrl + l is more effective.

 

3. Run a command in another directory and then return to the current working directory automatically.

 

This is an amazing technique that many people may not know. You may want to run any command in another directory and then return to the current directory. To achieve this goal, you only need to put the command in a parentheses.

 

Let's look at an example:

 

Avi @ deb :~ $ (Cd/home/avi/Downloads /)

 

Sample output:

 

Avi @ deb :~

 

It will first cd to the Downloads directory, and then return to the previous home directory. Maybe you think that the command in it is not executed at all, or there is a certain error, because there is no change from the command prompt. Let's simply modify this command:

 

Avi @ deb :~ $ (Cd/home/avi/Downloads/& ls-l)

 

Sample output:

 

-Rw-r ----- 1 avi 54272 May 3 text1.txt-rw-r ----- 1 avi 54272 May 3 text2.txt-rw-r ----- 1 avi 54272 May 3 text3.txt avi @ deb: ~ $

 

In the above command, it first enters the Downloads directory, then lists the file content, and finally returns to the current directory. It proves that the command is successfully executed. You can include any command in brackets. After the command is executed, it will be returned to the current directory smoothly.

 

Via: http://www.tecmint.com/useful-linux-hacks-commands/

 

Author: Avishek Kumar

 

Translator: goreliu

 

Proofread: wxy

 

This article was originally translated by LCTT and launched with the Linux honor in China

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.