Easily overlooked Linux command line: 15 super practical tips to know!

We use Linux command lines frequently every day. Although there are many kinds of information about its use skills on the Internet, many friends should not have actually tried them, and they may forget how to use them. So I decided to put those tips and tricks that you might forget. Therefore, I hope I can bring you a little help.

First of all, let's first understand why the command line is used?

Have you noticed that in the movie, a "super hacker" sits in front of a computer and can intrude into an ultra-secure military computer within 30 seconds without touching the mouse. This is because filmmakers realize that as human beings, instinctively know that the only way for a computer to complete its work satisfactorily is to use the keyboard to manipulate the computer.

Nowadays, most computer users are only familiar with the graphical user interface (GUI), and product vendors and scholars in this field will instill in users the idea that the command line interface (CLI) is a very scary thing used in the past.

This is unfortunate, because a good command line interface is a very effective way to communicate with computers, just like human society uses words to communicate information. People say, "The graphical user interface makes simple tasks easier to complete, while the command line interface makes it possible to complete complex tasks", and this sentence is still true.

Because Linux is written based on the Unix family of operating systems, it shares the rich command line tools of Unix.

Unix became prominent in the early 1980s (though, it was developed earlier), and as a result, before the widespread use of graphical interfaces, a wide range of command-line interfaces was developed. In fact, many people choose Linux (rather than other systems, such as Windows NT) because of its powerful command-line interface that makes "complex tasks possible".

Very practical Linux command line skills

1. Make a clear table for the output content

Sometimes, when you see the output of the command line execution, the output content is difficult to identify because the string is overcrowded (for example, the output of the mount command). What if the content we see is a form? Actually this is very easy to do!

In order to see more clearly, we can convert the output into a table, just add a few characters:

mount | column –t:

In this small example, we use spaces to divide the output into a table, and the entire interface is instantly clearer and more beautiful. If you don't like space as a separator, you can also change it to something else, such as a colon (:).

It's still very simple: just use the -s parameter at the end to specify the corresponding delimiter:

cat /etc/passwd | column -t -s:

2. Repeat a command until it runs successfully

If you search for this feature on Google, you will find that many people have asked how to repeat the command until the command returns successfully and runs normally. The suggestions on Google include ping the server until it becomes idle, check whether a file with a specific extension is uploaded to a specific directory, and check whether a specific URL already exists, and so on. .

But you still have other options, such as using a while true loop to get things done:

In the above example, >/dev/null 2>&1 will redirect the output of the program to /dev/null. Both standard error and standard output are included.

This is one of the coolest Linux command line tricks I think.

3. Sort the processes according to the usage of memory resources

ps to | sort -rnk 4:

4. Sort processes by CPU resource usage

ps to | sort -nk 3:

If you want to check your machine architecture, execute getconf LONG_BIT.

5. You can view multiple log files at the same time

Undoubtedly, you may already use the tail command to view log files, but you may sometimes think about viewing multiple log files at the same time, which is more troublesome for tail. At this time, you can use the multi-tail command, which not only supports text highlighting, but also supports content filtering and more functions you may need:

If this command is not available in the system, run the apt-get install multitail command to install it.

6. Go back to the last directory you operated

The operation of returning to the previous directory is very simple, cd-is fine.

Make non-interactive shell sessions interactive

To do this, change the setting from ~/.bashrc to ~/.bash_profile.

7. Timed monitoring command output

Using the watch command (watch df -h), you can view any output from any command. For example, you can view the available space and its usage growth.

By using the watch command to manipulate the changing data, you can imagine what you can do with this.

8. Continue to run the program after the session is closed

If you run a program in the background, and then you close the shell session, then the program running in the background will be killed by your shell. How can the program continue to run after closing the shell?

This can be done with the nohup command-this command means not to hang up:

nohup wget http://site.com/file.zip

Most people don't use this command, it's all thanks to the screen environment:

This will generate a file named nohup.out in the same directory, which contains the output of the running program:

Commands are cool, right?

9. Automatically answer Yes or No to any command

If you want to automate the process of saying yes to the user, you can use the yes command to achieve: yes | apt-get update.

Maybe what you want to do is say "No" automatically. This can be achieved with the yes no | command.

10. Create a file with a specified size

You can use the dd command to create a file with a specified size: dd if=/dev/zero of=out.txt bs=1M count=10.

This will create a 10 MB file with zeros as content:

11. Run the last command as the root user

Sometimes, you forget to type sudo before commands that require root privileges. You don't need to rewrite the command at this time; just type sudo !!

12. Record the command line session

If you want to record what you type on the shell screen, you can use the script command to save all the typing to a file called typescriptscript.

After you type the exit command, all commands will be written to the file so that you can look back afterwards.

13. Replace space characters with label symbols

You can use the tr command to replace any character, this is very convenient to use: cat geeks.txt | tr':[space]:' ''> out.txt.

14. Convert the contents of the file to uppercase or lowercase

It can be achieved like this: cat myfile | tr az AZ> output.txt.

15. Powerful Xargs command

The xargs command is one of the most important Linux command line tricks. You can use this command to pass the output of a command as a parameter to another command. For example, search for png files and then compress them or perform other operations:

find. -name *.png -type f -print | xargs tar -cvzf images.tar.gz

Or you have a list of URLs in your file, and what you want to do is to download or process these URLs in a different way. You can do this:

cat urls.txt | xargs wget

Please remember that the output of the first command will be passed at the end of the xargs command.

So what if the command needs the output of the intermediate process? It's easy!

Just use {} in combination with the -i parameter. As shown below, replace the parameters where the output of the first command should go:

ls /etc/*.conf | xargs -i cp {} /home/likegeeks/Desktop/out

These are just a few of the Linux command line tricks. You can use other commands to do more HAPPY things, such as the awk command and the sed command!

to sum up:

The command line has advantages and disadvantages

Where are the advantages?

1. Life is short, efficiency, efficiency, efficiency, or efficiency

2. The mouse is not suitable for quick operation. The command line is usually simpler, easier and more direct than clicking in the menu.

3. Stable, strong portability

4. Development saves worry and money

Where are the disadvantages?

1. The command line is not very friendly to novices, so being able to use the command line proficiently is a point where masters and novices can significantly widen the efficiency gap.

2. It’s not friendly when you’re entertaining. If you want to play games or chat, the graphical interface will prevail at this time. Of course, it’s also beautiful for users. Seeing someone say this-I want the photo to be on the face of the girl My small acne disappeared, how to use the command line to achieve it?


I think it’s good, you can "one-click three connections", or forward or leave a message

Official account: Programmer Erhei, get software testing resources (interview questions, PDF documents, video tutorials)

Share the wonderful content with your friends

Guess you like

Origin blog.csdn.net/m0_53918927/article/details/113614814