People who have used Linux know that there are many commands in Linux. However, learning so many Linux commands is unnecessary if you master the most commonly used ones. Everyone uses Linux for different purposes, so their commonly used commands are very different. I mainly use Linux for C, C++, and Shell programming. Therefore, my commonly used commands are different from the commands of Linux system administrators. I’d like to summarize them here for your convenience. Now, let’s talk about my most commonly used Linux commands.
This is a very basic command that people often need to use. It is used to switch the current directory. Its parameter is the path of the directory to be switched to, which can be an absolute path or a relative path. For example, cd /root/Documents # switches to the directory /root/Documents. cd ./path # switches to the path directory under the current directory, and “.” indicates the current directory. cd ../path # switches to the path directory in the upper-layer directory, and “..” indicates the directory at the upper layer.
This is a very useful command to view files and directories. It means list. It has many parameters. Some of my commonly used parameters are listed below:
Note: These parameters can also be used in combination. Here are two examples:
This command is often used to analyze the information in one line. If there is any information we need, the line is displayed. This command is usually used together with the pipeline command to filter and process the output of some commands. Its simple syntax is grep [-acinv] [-color=auto] ‘search string’ filename. Its common parameters are listed below:
Display the line that does not have the content of the ‘search string’
For example, retrieve the line that contains MANPATH from the file /etc/man.config and add the color to the found keyword through grep -color = auto ‘MANPATH’ /etc/man.config
ls -l | grep -i file
find
is a very powerful command based on search. Its use is relatively complicated, and it has many parameters. So, they are classified and listed here. The basic syntax is listed below:
find [PATH] [option] [action]
Time-Related Parameters:
Parameters related to the user or user group name:
Parameters related to file permissions and name:
The values of TYPE mainly include general files (f), device files (b, c), directories (d), connection files (l), socket (s), and FIFO pipeline files (p).
This command is used to copy files. Multiple files can be copied to the same directory at one time. The following part lists its common parameters:
For example: cp -a file1 file2 copies file1 together with its all attributes to file2.
cp file1 file2 file3 dir copies file1, file2, and file3 to the dir directory.
This command is used to move or rename files and directories. The common parameters are listed below:
Note: This command can move one or more files into one folder at a time, but the last target file must be "directory."
For example, mv file1 file2 file3 dir moves file1, file2, and file3 to the dir directory.
mv file1 file2 renames file1 to file2.
This command is used to remove files or directories. Its common parameters are listed below:
rm -fr dir forcibly removes all files in the dir directory.
This command is used to select and output the running status of the process at a certain point in time. It means process. Its commonly used parameters are listed below:
We only need to remember the common command parameter collocation that the ps command uses. They are not many:
This command is used to send a signal to a job (%jobnumber) or a PID (number). It is usually used together with the ps and jobs commands. Its basic syntax is listed below:
kill -signal PID signal
Common parameters of signal are listed below:
Note: The first number is the signal code, and the code can be used to replace the corresponding signal. 1: SIGHUP. Start the terminated process. 2: SIGINT. It is equivalent to ctrl + c, interrupting the running of a program. 9: SIGKILL. Force the interruption for the running of a process. 15: SIGTERM. Terminate the process in normal termination process mode. 17: SIGSTOP. It is equivalent to ctrl + z, pausing the running of a process. For example: If you want to terminate the first background process in a normal termination process mode, run the jobs command to check the first worker process that runs in the background. Run kill -SIGTERM %1 to change the process with an ID of PID again. PID can be filtered out using the ps command, the pipeline command, and the grep command.
kill -SIGHUP PID
This command is used to send a signal to a process started by a command. Its general syntax is listed below:
killall [-iIe] [command name]
Its parameters are listed below:
This command is used to determine the basic data of the file after the file command because the type of files in Linux is not based on the suffix. So, this command is very useful and easy to use. The basic syntax is listed:
file filename, for example: file ./test
This command is used to package the file instead of compressing it by default. If the corresponding parameters are specified, it also calls the corresponding compression program, such as gzip and bzip, to perform compression and decompression. Its common parameters are listed below:
Note: -c, -t, and -x cannot appear in the same command at the same time.
The explanations above can be confusing, but usually, we only need to remember the following three commands:
Note: The file name does not have to end with tar.bz2. It is mainly to explain that the compression program used here is bzip2.
This command is used to view the content of a text file. It is followed by the name of the file to be viewed. Generally, it can be used with the parameter pipeline (more or less) to view data page by page. For example: cat text | less views the content of the text file. Note: This command can also be replaced by less text.
This command is used to change the user group to which a file belongs. It is easy to use and works like this:
chgrp [-R] dirname/filename
This command is used to change the owner of the file. It is used the same way as the chgrp command, except that the modified file attributes are different. It is no longer described in detail.
This command is used to change the permissions of files. The general usage is listed below:
chmod [-R] xyz file or directory
That means all the files in the subdirectory are also changed. chmod can also use u (user), g (group), o (other), a (all), + (addition), - (deletion), and = (set) paired with rwx to change the permissions of the file.
For example, the chmod 0755 file changes the file permission of the file to -rxwr-xr-x.
The chmod g+w file adds the write permission for the user group to the file permission of the file.
This command is mainly used for text editing. It takes one or more file names as parameters. It opens the file if the file exists and creates a file with the file name if the file does not exist.
This command is very important for a person that develops C programs with Linux. It is used to compile the source program file of the C language into an executable program. Since many parameters of g++ are very similar to gcc, only the gcc parameters are introduced here. The common parameters are listed below:
For Example: gcc -o test test.c -lm -std=c99 compiles the source file test.c into an executable program test according to the c99 standard. gcc -S test.c converts the source file test.c into the corresponding assembly program source file test.s.
This command is used to estimate the execution time of a command (program). It is very simple to use. The word time needs to be added before the command, for example: time ./process time ps aux. At the end of the program or command, it outputs three times: user for user CPU time. It means the user CPU time taken to complete command execution, which is the total command execution time the in user state and system for system CPU time. It means the system CPU time taken to complete command execution, which is the total command execution time in core state and real for real-time. It means the time from the execution of a command line to the termination of the command.
Note: The sum of the user CPU time and the system CPU time is the CPU time, which is the total time that the command occupies CPU for execution. The real-time is greater than the CPU time because Linux is a multi-task operating system. In other words, when executing a command, the system also has to deal with other tasks. Another problem that needs attention is that even if the same command is executed each time, the time spent is also different, which is related to the system operation.
This article was originally published on the Programmer Bai Nannan official WeChat Account.
Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.
1,042 posts | 256 followers
FollowAlibaba Clouder - March 11, 2019
Alibaba Clouder - March 11, 2019
OpenAnolis - July 8, 2022
Alibaba Clouder - June 11, 2020
Alibaba Clouder - January 24, 2019
Alibaba Clouder - May 24, 2019
1,042 posts | 256 followers
FollowAlibaba Cloud Linux is a free-to-use, native operating system that provides a stable, reliable, and high-performance environment for your applications.
Learn MoreExplore Web Hosting solutions that can power your personal website or empower your online business.
Learn MoreA low-code development platform to make work easier
Learn MoreExplore how our Web Hosting solutions help small and medium sized companies power their websites and online businesses.
Learn MoreMore Posts by Alibaba Cloud Community