20 commands that are useful to intermediate Linux users

Source: Internet
Author: User
Tags echo command disk usage ftp protocol

1. Command: Find

Searches for files under the specified directory, starting at the parent directory, and then searching subdirectories.

Note: -name‘选项是搜索大小写敏感。可以使用 -iname ' option so that the case can be ignored in the search. (* is a wildcard, you can search all the files; '. Sh ' You can use the file name or part of the file name to make the output)

Note: The above command looks for all files containing ' tar.gz ' under the root directory and all folders and subdirectories of the loaded device.

For more detailed information on the ' Find ' command, please refer to the "Find Commands" Examples in Linux

2. Command: grep

The ' grep ' command searches the specified file for the line containing the given string or Word. Example search ' Tecmint ' in '/etc/passwd ' file

Use the '-I ' option to ignore the case. Use the '-r ' option to recursively search all the rows from the directory that contain the string "127.0.0.1".

Note: You can also use the following options:

    1. -W Search for Words (egrep-w ' word1|word2 '/path/to/file).
    2. -C is used to count the rows that meet the requirements (i.e., total number of the times the pattern matched) (grep-c ' word '/path/to/file).
    3. –color Color Output (Grep–color server/etc/passwd).
3. Command: Man

' Man ' is the system help page. Man provides online documentation of all options and usage of the command. Almost all commands have their help page, for example:

Above is the system help page for the Man command, similar to the help page for cat and LS.

Note: The system help page is designed for use and learning of commands.

4. Command: PS

The PS command gives the status of a process that is running, and each process has a specific ID that becomes the PID.

Use the '-A ' option to list all processes and their PID.

Note: The PS command works well when you want to know which processes are running or need to know which process pid you want to kill. You can share it with ' grep ' to query the specified output, for example:

The PS command and the grep command are split with pipe lines to get the results we want.

5. Command: Kill

Perhaps you have guessed from the name of the command what it is, kill is used to kill a process that is irrelevant or unresponsive. It is a very useful command, not very very useful. You may be familiar with Windows to kill the process may require frequent restart of the machine because a running process cannot be killed in most cases, even if the process is killed, the operating system needs to be restarted to take effect. But in a Linux environment, It's not like that. You can kill a process and restart it instead of restarting the entire operating system.

Killing a process requires knowing the PID of the process.

Suppose you want to kill an already unresponsive ' apache2 ' process, run the following command:

Search ' apache2 ' process, find PID and kill it. For example, in this example, the PID of the ' apache2 ' process is 1285.

Note: Every time you rerun a process or start a system, each process will generate a new PID. You can use the PS command to get the PID of the current running process.

Another way to kill a process is to:

Note: Kill requires PID as a parameter, Pkill can choose how to apply it, such as specifying the owner of the process.

6. Command: Whereis

The role of Whereis is to locate the binary file \ Resource \ or help page of the command. For example, get the binary files/resources and help pages for the LS and kill commands:

Note: Useful when you need to know where to save the binary file.

7. Command: Service

The ' Service ' command controls the start, stop, and restart of the service, allowing you to have the configuration take effect to open, stop, or restart a service without restarting the entire system.

To start Apache2 server on Ubuntu:

Restart Apache2 server:

Stop Apache2 Server:

Note: To use the service command, the script for the process must be placed in '/etc/init.d ' and the path must be in the specified location.

If you want to run service apache2 start, actually do "service/etc/init.d/apache2 start".

8. Command: Alias

Alias is a system-built shell command that allows you to specify aliases for long-named or frequently used commands.

I often use the ' ls-l ' command, which has five characters (including spaces). So I created an alias ' L ' for it.

Try whether it can be used:

To remove the ' l ' Alias, use the Unalias command:

Try again:

Make a joke. Designate an alias for an important command as another important command:

Think about how interesting it is now if your friend typed in the ' CD ' command, when he saw a list of directory files instead of changing the directory, when he tried to use the ' su ' command, he would go into the current directory. You can then remove the alias and explain the situation to him.

9. Command: DF

Reports the disk usage of the system. It is useful for both normal users and system administrators to track disk usage. ' DF ' works by checking the directory size, but this value is updated only when the file is closed.

See DF Command Examples in Linux for more examples of ' df ' commands.

10. Command: Du

Estimate the space occupied by the file. Level-by-layer statistics files (for example, recursively) and output summaries.

Note: ' DF ' only shows file system usage statistics, but ' du ' statistics catalog contents. For more detailed information on the ' du ' command, please refer to the Ten du (Disk Usage) Commands.

11. Command: RM

The ' RM ' standard removal command. RM can be used to delete files and directories.

When you delete a directory directly, you get an error:

' RM ' cannot delete the directory directly, it needs to add the corresponding '-rf ' parameter.

Warning: the "rm-rf" command is a destructive command, if you accidentally delete an incorrect directory. Once you use ' rm-rf ' to delete a directory, all files in the directory including the directory itself will be permanently deleted, so use this command with great care.

12. Command: Echo

Echo's function, as its name, is to print a piece of text based on standard output. It has nothing to do with the shell, nor does the shell read what is printed through the echo command. In an interactive script, however, Echo passes the information to the user through the terminal. It is a command that is often used in scripting languages, interactive scripting languages.

Create a small piece of interactive script

1. Create a new file on the desktop named ' interactive_shell.sh ' (remember to have the '. Sh ' extension).
2. Copy and paste the following script code to ensure consistency with the following.

Next, set the Execute permissions and run the script.

Note: ' #!/bin/bash ' tells the shell that this is a script, and it is a good practice to write this sentence on the first line of the script. ' read ' reads the given output.

13. Command: passwd

This is a very important command, which is useful for changing your password in the terminal. Obviously, because of the security reasons, you need to know the current password.

14. Command: LPR

This command is used to print the specified file on the specified printer on the command line.

Note: the "lpq" command lets you view the status of the printer (open or closed) and the status of the work (file) that is waiting to be printed.

15. Command: CMP

Compares two files of any type and outputs the results to standard output. If two files are the same, ' CMP ' returns 0 by default, and if different, the number of bytes and the first different location will be displayed.

Take the following two files as an example:

File1.txt

File2.txt

Compare these two files to see the output of the command.

16. Command: wget

Wget is a free tool for non-interactive (for example, background) download files. Supports HTTP, HTTPS, FTP protocol, and HTTP proxy.

Download FFmpeg using wget

17 Command: Mount

Mount is an important command to mount a file system that cannot be mounted automatically. You need root permission to mount the device.

After inserting your file system, first run the "lsblk" command, identify your device, and then write down the assigned device name.

From this output, it is clear that I inserted a 4GB USB flash drive, so "SDB1" is to mount the file system. Operate as root, and then switch to the/dev directory, which is where all file systems are mounted.

Create a directory of any name, but preferably related to a reference.

Now mount the "sdb1" file system to the "USB" directory.

You can now access the files from the terminal to the/dev/usb or from the Mount directory via the X Window System.

It's time for the program ape to see how rich the Linux environment is! 18. Command: GCC

GCC is a built-in compiler for the C language in Linux environments. Here is a simple C program that is saved as hello.c on the desktop (remember that you must have the '. C ' extension).

Compile

Run

Note: When compiling a C program, the output is automatically saved to a new file called "A.out", so the C program "A.out" will be modified every time you compile it. It is therefore better to define the output file name during compilation, so there is no risk of overwriting the output files.

Compile in this way

Here '-o ' writes the output to the ' Hello ' file instead of ' a.out '. Run again.

19. Command: g++

G++ is the built-in compiler for C + +. Here is a simple C + + program that is saved as Add.cpp on the desktop (remember that you must have the '. cpp ' extension).

Compile and run

Note: When compiling a C + + program, the output is automatically saved to a new file called "A.out", so the C + + program "A.out" is modified every time it is compiled. It is therefore better to define the output file name during compilation, so there is no risk of overwriting the output files.

Compile and run in this way

20. Command: Java

Java is one of the most widely used programming languages in the world. It is also considered to be an efficient, safe and reliable programming language. Most web-based services now use Java implementations.

Copy the following code to a file to create a simple Java program. You might want to name the file Tecmint.java (remember: '. java ' extension is required).

Compile Tecmint.java with Javac and run

Note: Almost all Linux distributions have a GCC compiler, and most distributions have g++ and Java compilers built in, and some may not. You can install the required packages with apt or yum.

20 commands that are useful to intermediate Linux users

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.