Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's good under the Linux command line?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what is good under the Linux command line. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Tmux

Although the order is random, this one must be the first one. Many people use Linux status is their own machine is Windows, use putty to connect to a Linux server where to work. In this way, the program you run will be killed every time you disconnect. You may want to keep the open program there when you exit, and automatically return to the previous working state the next time you use the connection. Tmux is the ultimate solution to this problem. On the one hand, they can create multiple "windows" in a Terminal, on the other hand, if you close the connection or accidentally drop the line, they will not be turned off by default, and you can use tmux attach to restore your previous working state next time.

If you haven't heard of this or similar screen, you should give me some cash.

Tree

Ls is probably one of your most commonly used commands. Tree can recursively list all the files in the directory and display them in a tree:

$tree. ├── b ├── c │ └── d └── what

There is also a pstree that can display the process tree in this way.

Ack

I remember being asked during the interview, "Please write how to call grep to list the lines containing a certain string in all the files in the current directory." My answer to this question is "just use ack". The domain name of ack's official website is betterthangrep.com. Since it is so common to look for a string in all the files downloaded in the current directory, execute ack foo to find foo in all the files in the current directory. Ack skips a lot of useless directories and files by default, making the search faster and the output more accurate. In addition, the color display which is turned on by default also makes people very comfortable to use.

Ack is currently not available in most distributions, and if you are using Ubuntu, the package name is ack-grep, and so is the name of the execution file. You can view the document here.

Rsync

For example, you have a computer locally and a server remotely. You want to copy all your folders to somewhere on the other side. Your folder needs to be updated frequently, and you hope to synchronize the updated parts quickly in some way. How should this be done? If you don't know rsync, you might want to use git, build a ftp, or use scp or something. But in fact, rsync is the real software that is designed to solve this problem accurately. The best thing about rsync is the differential update, which only passes what is missing on the other side, and you don't need any extra configuration to fly fast. An example:

Rsync-arvuzp-- chmod=g+rx. / built/ me@example.com:/var/www/site

Aspell

As a programmer, you will have to write English documents one day. It is too normal for me to make spelling mistakes if my mother tongue is not English. An underscore will prompt you if you use Word, but there seems to be nothing you can do if you write comments in your code or write markdown under Linux. In fact, this is also a problem that has been solved, and that's what aspell is here to do. Aspell can spell check any plain text, and as a programmer-oriented tool, it can detect file types. For example, for C++ programs, it only checks words in comments.

Tee

Sometimes the results of certain commands are so long that you may use less to look up and down. Or you might use > to redirect to the file. But sometimes the program may require you to type y to confirm, or some programs run for a long time, just redirect it is not sure whether it is running properly. Tee can output to the screen and redirect to the file at the same time. A simple example:

$echo waht | tee out.txt

The waht will be output to the screen and written to the out.txt file. In addition, tee actually means T, pulling out another one in the middle of the input and output, which is quite vivid.

It is worth mentioning that vim can also be used in pipe. Examples are as follows:

$echo waht | vim-

You must know that you can find your history back and forth with the up and down arrow keys of the keyboard, so for example, there is a command you typed a long time ago that you may need to press to find it. In fact, at this time, just type a part and press bash to help you search back. You can search forward in order in a row. (or press re-enter directly). Following the example above, enter echo and press as follows:

(reverse-i-search) `echo': echo waht | vim-

If there is a forward search, there is usually a backward search. Unfortunately, the shortcut key to search later is, if you try to press it, you will find. Looks like the machine's not responding. This is because in most cases the default is XOFF, which means pausing to receive input. Press it to recover. Of course, you can bind forward search to other keys, please search by yourself.

Cloc

Although the number of lines of code doesn't mean anything, sometimes I just want to know for some reason. Cloc can accurately calculate the number of lines of code and separate comments from spaces. If you are a legendary project manager, start using cloc to pay your programmers from today. Screenshots are attached below:

$cloc / usr/include/ 9628 text files. 9308 unique files. 434 files ignored. Tipped 39.0 s (227.6 files/s) 39948.2 lines/s)-Language files blank comment code-Citrus + Header 8875 217366 287013 1053368 Teamcenter Def 1 48 0186-SUM: 8876 217414 287013 1053554-

Printenv

You can see all the Shell variables with set, including the Shell function, but some of them only work in the current shell. Often what you need to look for is the environment variable defined by export. That's what printenv is for.

Set-o vi

Bash or perhaps most common shell actually supports command line editing in vi. For example, after setting set-o vi, you can use the familiar hjkl to move, use w, b to skip words, and so on.

Similarly, if you set the EDITOR environment variable, type fc to edit the previously entered command line in the editor, as long as it is saved, it will be executed. On the contrary, to give up saving is to give up.

Find

After carefully reading the book mentioned above, one of the biggest gains is that I finally learned to use find. This is now a command I use almost every day. For example, if I want to add all the png files in the directory to this git commit, I can use:

Find. -name'* .png'- exec git add {}';'

If you are familiar with find, you will know the last one; it would be better to switch to +, but it is purposeful to use it. Although this command looks simple, it will fail to omit single quotation marks or replace it with double quotation marks. These escape rules, which involve "shell variable expansion" and quotation marks, are annoying, but there are only a few simple rules, and they are very consistent. So this simple problem can be easily solved as long as you take the time to figure it out.

Type

If foo is a program that can be run directly from the command line, you should know where the executable path to foo can be found with which foo. However, the commands that can be executed on the command line do not necessarily correspond to an executable file, it can be alias, shell's own functions, user's own functions, and so on. So sometimes it's confusing when you can't find something with which. In fact, you can use type foo to see what kind of foo is.

Help

The "builtin command" mentioned above are built-in commands, which are basic commands provided by shell or that cannot be done by external programs. Normally you can use man to view documents, but for the built-in command man will jump to shell's own manpage, in some systems that is a huge page where you have to find what you want to see, and in some systems there is no relevant information at all. In this case, you can solve this problem with the built-in command help: for example, to see the accepted options of set, you can easily find it with help set.

Env

You should know in #! The function of (shebang) in the first line of the script is to specify its' runtime'. For example, you want to write a script for Python, but you don't really care about its version, or you're not sure where its executable files are on different machines. Then env can come in handy here. It can be written as #! / usr/bin/env python so that the python found in the current PATH will be used for execution. On the other hand, it also gives you an opportunity to re-select 'runtime' without modifying the code.

File

If you want to know what type of file is on a certain path, file is perfect. It gives a meaningful explanation for everything, and lists a lot of important information about binaries.

Strings

"I put my secret in this program written in C++. Run it and enter the correct password to see it." In fact, in case of this situation, you only have to run strings program-written-in-cxx and nine times out of ten you will be able to see it. It can accurately list the C-style strings contained in the binary file. It doesn't seem to make much sense, but in fact, it's entirely up to you to play the role, such as knowing which version of a program was compiled by GCC. There may be results in strings.

Od

The whole process should be "object dump", and the file can be displayed in octal, hexadecimal or other ways. I think in most cases, people are using od-c to dump the files according to the ASCII code. A use case is used to see what the line ending of a file looks like. For example, executing od-c foo.txt displays the following result:

$od-c foo.txt 0000000 h e l l o\ t w o r l d\ r\ n y e a 0000020 h 0000021

You can clearly see that\ t is a tab character and\ r\ n is a newline character in Windows style.

This is the end of the article on "what's good under the Linux command line". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report