Home > Article > Backend Development > Summary of Linux command line usage skills
In the world of software development, the operating system that web programmers deal with the most is probably the Linux system. As for the use of Linux systems, if you can master some tips, I believe that the efficiency of doing things will be improved during the process of program development and debugging. Let me share with you 3 user command line tips in Linux programming.
1. How to schedule tasks under Linux without using Cron
The so-called Cron is to schedule a task/command under Linux. Usually when we schedule tasks, we will use Cron, but in fact, tasks can be scheduled even when Cron is not applicable.
Run a command (date) every 5 seconds and write the results to a file (data.txt). To achieve this, we can run the following one-line script directly from the command prompt.
$ while true; dodate > while true: Let the script enter In a loop, the condition is always true, which means creating an infinite loop and running the commands inside it over and over again.
do: do is the keyword in the while statement. The command after it will be executed. One or a series of commands can be placed behind it.
date >>date.txt: Run the date command and write its output to the data.txt file. Note that we use >>, not >.
>>: Append to the file (date.txt), so that every time the command is run, the output content will be appended to the file. If you use >, the previous content will be overwritten over and over again.
sleep 5: Let the script sleep for 5 seconds before running subsequent commands. Note that the time unit here can only be seconds. That means if you want the command to run every 6 minutes, you should use sleep360.
done: mark the end of the while loop statement block.
&: Put the entire process to run in the background.
Similarly, we can run any script like this. The example below runs a script named script_name.sh every 100 seconds.
It’s also worth mentioning that the script file mentioned above must be in the current directory, otherwise you need to use the full path (/home/$USER/…/script_name.sh). The single-line script that implements the above function is as follows:
$ while true; do/bin/sh script_name.sh ; sleep 100 ; done &
Summary: The above-mentioned one-line script is not a replacement for Cron, because the Cron tool supports many options, is more flexible, and is more customizable. However, if we want to run certain tests, such as I/O benchmarks, the above one-line script will also work.
2. How to clear the terminal content without using the clear command
Usually we use the clear command directly to clear the terminal content on Linux, but if we use the ctrl + l shortcut key, we will Save a lot of time. Because ctrl + l is a shortcut key and cannot be used in scripts, except for the clear command that must be used to clear the screen content in a script, this shortcut key can be used in other situations to clear content.
3. Run a command in another directory and then automatically return to the current working directory
In programming, we often want to run a command in other directories and then return to the current directory. But this is often difficult to achieve. In fact, to achieve this requirement is simple, just put the command in parentheses.
Let’s give an example below:
avi@deb:~$ (cd/home/avi/Downloads/)
Example 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 inside is not executed at all, or there is some kind of error, because you can't see any changes 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 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:~$
In the above command, it first goes to the Downloads directory, then lists the file contents, and finally goes back to the current directory. And it proves that the command was executed successfully. You can include any command in parentheses and it will smoothly return to the current directory after execution.
The above are 3 super practical tips for Linux command line. Mastering and using them skillfully will make your software development on Linux system appear more professional and get twice the result with half the effort.
Recommended learning: PHP development practical tutorial http://www.maiziedu.com/course/php/
Article source: code
|