Use Linux system developers, many people have their own favorite system commands, the following several commands I usually use more, share.

I will not list the detailed usage of each command as a textbook, but I will demonstrate the common usage of common commands in some scenarios during the daily development process.

Hope to have a little help to you ~~ also welcome to share your private room order in the message area.





Grep, awk, sed, as the three magic weapon of Chinese text processing in Linux system, MY favorite and most commonly used is grep command, none of them!

Its basic usage is:

grep [OPTIONS] PATTERN [FILE...]  grep [OPTIONS] [-e PATTERN]... [-f FILE]... [FILE...]Copy the code

It seems that there are so many options, and I most often use these two scenarios:

1. Find the specified string in a file or folder:

grep -rni "pthread" *
Copy the code

-r: recursive search. -n: Prints the line number. -i: case insensitive.

2. View information about a process, for example, process ID

$ps - aux | grep bash root 4681 0.0 0.1 24892 5912 PTS / 3 Ss 10:10 0:00 bash root 18052 0.0 15968 0.0 960 PTS / 3 S + thy life 0:00 grep --color=auto bashCopy the code

Grep process information is displayed in the result, which can be filtered by the -v option:

$ps - aux | grep bash | grep -v grep root 4681 0.0 0.1 24892 5912 PTS / 3 Ss 10:10 0:00 bashCopy the code

Finally, with the awk command, we can extract process ID 4681:

$ ps -aux | grep bash | grep -v grep | awk  '{print $2}'
4681
Copy the code

Such usage is common in some scripting tools.

For example, in some daemon startup scripts, this command is used to determine whether an instance is already running on the system.





Looking at this command, you must be wondering: What is this with just the letter Q?

This command does not exist on Linux!

Yes, the letter is simply an alias.

I have a strong obsessive-compulsive disorder. When I execute a command in the terminal window, I often need to make sure that the command is correctly executed.

On Linux, $? Indicates the exit status of the last command: 0 indicates that there is no error, and other indicates that there is an error.

Therefore, after executing a command, you can execute the following command to confirm whether the command just executed was successful.

echo $?
Copy the code

The obsessive-compulsive problem is fixed, but because this command is used so often, it requires typing so many characters, combined with the Shift key.

So I gave it an alias.

Bashrc: alias = bashrc: alias = bashrc: alias = bashrc: alias = bashrc: alias = bashrc: alias = bashrc: alias

My alias Settings are as follows:

alias ll='ls -lF' alias la='ls -A' alias l='ls -CF' alias q='echo $? 'Copy the code

In this way, after each execution of a system command, with a letter Q can check the execution results, save time and effort!





Some people may wonder: why is PWD a common command? Its function is to print the current path, in the command line window, the path is always displayed ah!

By default, the current path information is displayed directly, as follows:

Root @ ubuntu: ~ / OpenSource/Linux - 4.15 / samples/watchdogCopy the code

However, there is a slight problem: if the size of the terminal window is not full screen, if the directory level is deep, the display path information will be extremely long, so that the terminal window, which is not too wide, will be crowded, and the command will have to go back to the next line.

Therefore, I like to shorten the display path to show only the last file directory, as follows:

root@ubuntu:watchdog$
Copy the code

That is, the ~/OpenSource/ Linux-4.15 /samples path information was dropped so that there would be plenty of room in the terminal window for input.

If, at some point, I want to see the full path to the current directory, I can simply execute the PWD command.

This is why I often use the PWD command.

So, how to remove the directory information from the display path?

Change the.bashrc file in your home directory:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Copy the code

Just go to the above lines and change the last \w to \w, that is, lowercase w to uppercase W.

If you are testing, don’t forget to use the source. bashrc or.. bashrc command to reload.





Using the find command, you can find the files that meet the specified conditions.

My most common scenario is to find a file with a specified name or type.

Especially when writing makefiles, it’s common to get an error that you can’t find the end of the file, so you can do this:

find ./ -name xxx.h
Copy the code

Or find the file by suffix:

find ./ -name *.txt
Copy the code





History is used to record commands executed. If you rarely use this command, you have a good memory!

But for someone like me, the history command is incredibly useful!

When I test a piece of code in a command line window, I often go through the process of modifying the code, compiling it, executing it, and viewing the result.

If the compilation instructions are long, I’m sure no one wants to type character by character, mostly using history to list the most recent commands, then copying and pasting them.

When viewing the history command, because the output is too much, you can combine the grep command to display only the command records we are interested in, for example:

history | grep gcc
Copy the code

Then, only those commands with the GCC word will be displayed in the result.

Another tip: Quickly type a command you’ve executed before without looking at history (one caveat: you have to remember the first few characters of that command).

For example, this command has been executed before:

gcc -m32 -Wl,--export-dynamic -o main main.c -ldl
Copy the code

After a few minutes, I want to execute the command again. I can do this:

When you press both control and R at the same time, the input cursor looks like this:

root@ubuntu:tmp$ 
(reverse-i-search)`': 
Copy the code

In this case, you can enter the first few letters of the command: gcc-m. At this time, the terminal will look up the historical command record. The more characters you enter, the more accurate the match will be.

If the input character accurately matches a command history, it immediately displays the command in its entirety.

This tip is really good to use, I recommend you try it!





Using the od command, you can output the content of a given file.

There are many commands to enter the contents of a file: cat, head, tail, and so on. However, the od command is mainly used to view the binary code of the file, which can be displayed in the specified base.

In a previous article debunking the ELF format: ELF files, the building blocks of compilation and linking in Linux: Peeling away its layers and exploring the granularity of bytecode, I made extensive use of the OD instructions to read bytecode of any length from any address in an ELF format file.

For example, read the first 52 bytes of the main file:

od -Ax -t x1 -N 52 main
Copy the code

Main is an executable program in Linux, in ELF format, of course.

The following options are used in the OD command:

-Ax: Displays the IP address in hexadecimal format. If -ad is used, the address is displayed in decimal notation.

-t-x1: displays the bytecode content in hexadecimal (x), one byte (1) at a time.

-n 52: reads only 52 bytes.

You can see the first four bytes of the main file: 7f is the magic number for the ELF file, and 45, 4c, and 46 are the letters “ELF”.

Therefore, using the OD command to analyze the contents of binary files is very powerful!





The for command, often found in script files, is used to handle loop situations such as traversing files and counting, for example:

#! /bin/bash for file in /tmp/*; do echo $file; doneCopy the code

When I use for, the most common scenario is to rename many files with the same suffix in order:

i=0; for x in *.mp4; do n=$(printf "%02d" "$i"); mv $x $n.mp4; let i=i+1; doneCopy the code

Here is a pure number to rename, you can also add prefixes and other strings as needed.

There is one more minor issue to note: the mv directive will give an error if there is a space in the file name:

mv: target 'xxx' is not a directory
Copy the code

The solution is to execute this command in a terminal window:

IFS='

'
Copy the code

Then, execute the batch rename command again and there will be no errors!

Of course, a better way is to write these commands as a script file, implement the batch renaming of files of any type, and put it in your own private bin directory, and use it at will.

Don’t worry, I’ve done it for you, as follows (file_rename. Sh) :

#! /bin/bash if [ $# -eq 0 ]; then sufix=mp4 else sufix=$1 fi IFS=' ' i=0; for x in *.$sufix; do n=$(printf "%02d" "$i"); mv $x $n.$sufix; let i=i+1; doneCopy the code

If you execute./file_rename. Sh, all mp4 files in the current directory will be renamed by default.

If it’s any other type of file, pass in a parameter.

For example, if you want to batch rename PNG images, execute./file_rename. Sh PNG, where the last PNG is the parameter passed in, corresponding to the $1 variable in the script file.





Look forward to sharing your own private room instructions in the message area, any technical problems can be discussed!

Recommended reading

[1] C language pointer – from the underlying principle to the tricks, with graphics and code to help you explain thoroughly [2] step by step analysis – how to use C to achieve object-oriented programming [3] The original GDB underlying debugging principle is so simple [4] inline assembly is terrible? Finish this article and end it! [5] It is said that software architecture should be layered and divided into modules. What should be done specifically