More effective management Five cool Linux command line tricks

  


Efficient use of the command line is an important part of the Linux system administrator skills. In this article, we've provided five cool and even weird Linux command line tricks to help you manage your Linux system more efficiently.

More=Less

Reading files on Linux usually involves the more and less commands. The more command reads the file from front to back, so the entire file is loaded at startup. The less command allows forward or backward movement in the file, and only reads the next part of the file when loading. To save time, a useful trick is to alias the less command to more:

$ alias more=less

Add this to your .bashrc or .bash_profile file so that Become a permanent alias.

The less command is easy to use. Scrolling forward is like the more command, which can be done by hitting the space bar or the f key. Use the b key once to scroll back one screen. (You can also see more command options on the help page of the less command.)

Another command to output the contents of the file is the cat command, which displays the contents of the entire file from top to bottom on the screen. on. Many people don't know that the cat command has a sister command tac. As you can imagine, the tac command also displays the contents of the entire file, but in reverse order, from bottom to top. This command is useful when reading a log file or other things that are of interest to the bottom of the file.
The shuf command is not very useful, but it is very interesting and can upset the order of the input data. Try running:

$ shuf filename

It will return the contents of the file in a random order.

No one can see your screen

Have multiple SSH sessions established on the same server? You should not do this. Opening multiple SSH sessions not only wastes computer resources, but is also inconvenient to manage. This is especially true when you use a window manager or a tool like Putty that contains multiple terminals and can quickly occupy a separate screen position. A better option is to use tools like screen or tmux that allow you to set up multiple endpoints in an SSH session.

The screen command is easy to use. First, you need to make sure you have the screen program installed. On Red Hat and Ubuntu hosts you need to install the screen package, the command is:

$ sudo yum install screen

Then type in the terminal session:

$ screen < Br>

It doesn't seem like anything happened, right? Then you are wrong. Type a command, such as top, and you will see the processes running inside the system. Now type Ctrl-a-c (hold down the Ctrl key, then type a, then c) and a new terminal window will pop up. Your old window is still available. In fact, you can go back to the previous window with Ctrl-a-p, and you can also loop between windows by Ctrl-a-a. Or you can select the window by serial number. According to the order of creation, each window has a different serial number. The first window is Ctrl-a-0, then Ctrl-a-1, which is added in turn. You can create more windows by constantly pressing Ctrl-a-c, and you can exit the window by typing exit on the command line. When you exit the last window, the entire session will also exit.

The most fun thing about the screen is that you can keep the current screen and return to this state at any time. Type Ctrl-a-d to keep the screen, then you can return to the window where the screen command was originally run. Then you can exit the host. When you log in again, you can run:

$ screen -r

Your session will start again and all previous terminals will be available (provided no one closes the screen process or Restart the host). You can find more useful information on the Screen help page.

Another alternative to screen is Tmux, which is more powerful and more complex, and is very useful for multi-user shared screens. A common application is remote co-programming.

HTTP Server: When you need a web server right away!

One of the easiest ways to pass a file quickly is through an HTTP server. I often quickly build a Ruby web server from the command line:

$ ruby ​​-rwebrick -e'WEBrick::HTTPServer.new(:Port => 3125, :DocumentRoot => Dir.pwd). Start'

This will open the HTTP service for the current directory on port 3125:

http://your.server.com:3125

You can also do the same with Python Things:

$ python -m SimpleHTTPServer

This will open the HTTP service for the current directory on port 8000. (You need to open both ports through the firewall.)

It's important to remember that anyone can download files from your server, so make sure you know which files you have provided. Don't contribute anything you don't want to share with others.

Better Process Display Tool: pgrep

You may often find yourself typing this command:

$ ps -A grep 'ruby'

1680 ? 00:05:45 ruby ​​

30250 ? 00:06:39 ruby ​​

But there is an easier way to display the process and there is no need to call the output pipeline of the ps command :pgrep.

$ pgrep -l ruby ​​

1680 ruby ​​

30250 ruby ​​

You can also query the progress of a particular user, such as:

$ pgrep -u root

This will show all processes running by root.

Finally, you can also use some grep-style query techniques:

$ pgrep -lv `whoami`

This command will list all user processes that are not yours. (Like the grep command, the -v option indicates exclusion).

Spelling Errors

You may know that the aspell (or ispell) library allows you to perform spell checking on Linux. But you can also run aspell commands independently or integrate them into other applications. For example, to spell check a file from the command line, you can run aspell like this:

$ aspell -c filename.txt

This command will launch an interactive spell checker. You can check and update any spelling errors in the file.

But sometimes you just want to know how to spell a word, then you need the look command, which is a useful spell checker on the command line. Type look and part of the word you want to spell, for example:

$ look vendo

will return a list of words starting with vendo.

Author: James Turnbull Translator: Dan

Copyright © Windows knowledge All Rights Reserved