3 command-line tips for Linux users

Source: Internet
Author: User

3 command-line tips for Linux users

Original: http://www.tecmint.com/useful-linux-hacks-commands/ Avishek Kumar
translation: Lctt https://linux.cn/article-5635-1.html Translator: Goreliu

The Linux world is full of fun, and the more we dive in, the more interesting things we'll find. We'll try to provide you with some tips that will make you different from others, and here are 3 tips we've prepared.

1. How to schedule tasks under Linux without using cron

Under Linux, scheduling a task/command is called Cron. When we need to dispatch a task, we will use cron, but do you know that we can dispatch a task that runs in the future without using cron? You can follow the suggestions as follows ...

Runs a command (date) every 5 seconds and then writes the result to a file (Data.txt). To achieve this, we can run the following single-line script directly at the command prompt.

$ while true; Do date >> date.txt; Sleep 5; Done &

Explanation of the above script:

    • while true: Allow the script to enter a loop in which the condition is always true, that is, to create a dead loop that repeats the commands inside again and again.

    • do: do is the while keyword in the statement, after which the command is executed, and a sequence or series of commands can be placed behind it.

    • date >> date.txt: Runs the date command and writes its output to the Data.txt file. Note that we use >> instead of > .

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

    • sleep 5: Leave the script in a 5-second sleep state before running the command. Note that the time unit here can only be in seconds. This means that if you want the command to run every 6 minutes, you should use it sleep 360 .

    • done: while a tag that loops over a block of statements.

    • &: The entire process is run in the background.

Similarly, we can run any script in this way. The example below is running a script named every 100 seconds script_name.sh .

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

$ while true; Do/bin/sh script_name.sh; Sleep 100; Done &

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

can also refer to: Linux under the 11 scheduled scheduling task example

2. How to clear the contents of the terminal without using the clear command

How do you clear the contents of the terminal? You might think this is a silly question. Well, everyone knows you can use the clear command. If you ctrl + l get into the habit of using shortcut keys, we save a lot of time.

Ctrl + lThe effect of the shortcut is the clear same as the command. So next time you can use it ctrl + l to empty the contents of the terminal.

Summary : Because ctrl + l it is a shortcut, we can not use it in the script. So if we need to clear the contents of the screen in the script, we still need to use the clear command. But all the other things that I can think of ctrl + l are more effective.

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

This is a surprising technique that many people may not know about. You might want to run any command in another directory, and then go back to the current directory. To accomplish this, you just need to put the command in a parenthesis.

Let's look at an example:

[Email protected]:~$ (cd/home/avi/downloads/)

Example output:

[Email protected]:~

It first CD to the downloads directory, and then back to the previous home directory. You might think that the commands inside are not executed at all, or that there is some kind of error, because there is no change from the command prompt. Let's simply modify this command:

[Email protected]:~$ (cd/home/avi/downloads/&& ls-l)

Example 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 [email protected]:~$

In the above command, it first enters the downloads directory, then lists the contents of the file, and finally goes back to the current directory. Also it proved that the order succeeded carried out. You can include any command in parentheses, and you will be able to return to the current directory after execution.

This is the whole story, if you know any similar Linux tricks, you can share them in the comment box below the article, and don't forget to share this article with friends:)

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

Avishek Kumar Translator: Goreliu proofreading: Wxy

This article by LCTT original translation, Linux China honors launch

Source: https://linux.cn/article-5635-1.html


3 command-line tips for Linux users

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.