Efficient use of command line is an important part of the Linux System Administrator skills. In this article, we provide five cool or even slightly odd Linux Command Line techniques to help you manage your Linux system more effectively.
More = Less
Reading files on Linux usually involves more and less commands. The more command reads the file from the front and back, so the entire file is loaded at startup. The less command allows forward or backward movement of a file. Only the next part of the file is read during loading. To save time, a useful technique is to alias the less command as more:
$ alias more=less
Add this entry to your. bashrc or. bash_profile file to make it a permanent alias.
The less command is easy to use. Just like the more command, you can scroll forward by hitting the Space key or f key. You can use the B key to scroll back to a screen. (You can also see more Command Options on the less command help page .)
Another command that outputs the file content is the cat command, which displays the entire file content from top to bottom on the screen. Many people do not know the cat command and a sister command tac. As you can imagine, the tac command will also display the content of the entire file, but the order is the opposite, from bottom to top. This command is useful when reading a log file or other important output at the bottom of the file.
The shuf command is not so useful, but interesting. It can disrupt the order of input data. Try to run:
$ shuf filename
It returns the file content in random order.
No one can see your screen
How many SSH sessions are established on the same server? You shouldn't. Opening multiple SSH sessions is not only a waste of computer resources, but also inconvenient to manage. This is especially true when you use tools such as Window Manager or Putty that contain multiple terminals and can quickly occupy independent screen positions. A better choice is to use tools such as screen or tmux, allowing you to establish multiple terminals in an SSH session.
The screen command is easy to use. First, make sure that you have installed the screen program. On Red Hat and Ubuntu hosts, you need to install the screen package. The command is:
$ sudo yum install screen
Then, in the terminal session, type:
$ screen
It looks like nothing happened, right? Then you are wrong. Type a command, such as top, and you will see the processes running in the system. Press Ctrl-a-c (press Ctrl, press a, and then press c). a new terminal window will pop up. Your old window is still available. In fact, you can return to the previous window through Ctrl-a-p. You can also use Ctrl-a to loop between windows. Or you can also select the window by serial number, according to the order of creation, each window serial number is different, the first window is the Ctrl-a-0, then the Ctrl-a-1, increase in turn. You can press Ctrl-a-c to create more windows, and enter exit in the command line to exit the window. When you exit the last window, the entire session also exits.
The most interesting thing about the screen is that you can keep the current screen and return to this status at any time. Type Ctrl-a-d to keep the screen, and then you can return to the window where the screen command was initially run. Then you can exit the host. When you log on again, you can run:
$ screen -r
Your session will start again and all the previous terminals will be available (provided that no one shuts down the screen process or restarts the host ). You can find more useful information on the Screen help page.
The alternative tool for screen is Tmux, which is more powerful and more complex and useful for sharing screens among multiple users. A common application is remote mutual programming.
HTTP server: When you need a Web server immediately!
When you need to quickly transfer files, one of the simplest methods is to use the HTTP server. I often use the command line to quickly create a Ruby Web Server:
$ ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 3125, :DocumentRoot => Dir.pwd).start'
This will enable the HTTP service for the current directory on port 3125:
http://your.server.com:3125
You can also use Python to do the same:
$ python -m SimpleHTTPServer
This will enable the HTTP service for the current directory on port 8000. (You need to open these two ports through the firewall .)
It is important to remember this. Anyone can download files from your server, so make sure that you know what files you have provided externally, don't contribute anything you don't want to share with others.
Better process display tool: pgrep
You may often find yourself typing the following command:
$ ps -A | grep ‘ruby’1680 ? 00:05:45 ruby30250 ? 00:06:39 ruby
However, there is a simpler way to display processes without calling the ps command output pipeline: pgrep.
$ pgrep -l ruby1680 ruby30250 ruby
You can also query the processes run by a specific user, such:
$ pgrep -u root
This will display all processes run by the root user.
Finally, you can use some grep-type Query Techniques:
$ pgrep -lv `whoami`
This command lists all user processes that do not belong to you (just like in the grep command, the-v option indicates exclusion ).
Spelling error
You may know that the aspell (or ispell) Library allows you to perform spelling checks on Linux. But you can also run the aspell command independently or integrate it into other applications. For example, to check the spelling of a file from the command line, you can run aspell as follows:
$ aspell -c filename.txt
This command starts an interactive spelling check program. 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 handy spelling check tool in the command line. Type look and a part of the word you want to spell, for example:
$ look vendo
A list of words starting with vendo is returned.