Three command line 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.
$ whiletrue;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 again and again.
do:doYeswhileThe keyword in the statement. The command after the statement is executed, and one or a series of 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>.
>>: Append the file (date.txt), so that after each command is run, the output content will be appended to the file. If you use>Then, 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 subsequent 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 usesleep 360.
done:whileThe end mark of the loop statement block.
&: Run the entire process in the background.
Similarly, we can run any script like this. The following example shows how to runscript_name.sh.
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:
$ whiletrue;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.
You can also refer:
Use crontab in Linux to create scheduled tasks
Routine scheduling of crontab in Linux
Linux crontab does not run troubleshooting
Ubuntu uses crontab for scheduled tasks
Linux scheduled task (at batch crontab anacron)
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. Okay, you know you can use it.clearCommand. If you usectrl + lWe will save a lot of time on the habit of using shortcuts.
Ctrl + lEffects andclearCommand. So you can usectrl + lTo clear the terminal content.
Summary: Becausectrl + lIs a shortcut key, which cannot be used in scripts. So if we need to clear the Screen Content in the script, we still need to useclearCommand. But all other situations that I can think,ctrl + lAre 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 avi 54272May318:37 text1.txt
-rw-r-----1 avi avi 54272May318:37 text2.txt
-rw-r-----1 avi avi 54272May318:37 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.
This is all about the content. If you know any similar Linux skills, you can share them with us in the comment box below. Don't forget to share this article with your friends :)
Via: http://www.tecmint.com/useful-linux-hacks-commands/
Author: Avishek Kumar Translator: goreliu Proofreader: wxy
This article was originally translated by LCTT and launched with the Linux honor in China
This article permanently updates the link address: