Home > Operation and Maintenance > Linux Operation and Maintenance > Let's talk about common commands and environment variables of Linux operating system

Let's talk about common commands and environment variables of Linux operating system

WBOY
Release: 2021-12-31 18:07:18
forward
3645 people have browsed it

This article brings you the basic knowledge of the Linux operating system, including knowledge about the program development environment, common commands and environment variables. I hope it will be helpful to everyone.

Let's talk about common commands and environment variables of Linux operating system

1. Introduction to Linux and program development environment

1.1 What is Linux?

Linux is a UNIX-like kernel implementation that can be freely released. It is the underlying core of an operating system. Linux was developed by Linus from the University of Helsinki in Finland, with help from a large number of UNIX programmers on the Internet. It was originally a program inspired by his professor's Minix (a small UNIX-like system). It was purely a personal hobby, but later it gradually developed into a complete system in its own right.

To understand what Linux is , we must first understand the systems and applications that were born before Linux-UNIX and GNU software.

1.1.1 What is UNIX?

The UNIX operating system was originally developed by Bell Labs, which at the time was a member of the telecommunications giant AT&T (American Telegraph and Telephone Company). Today, it has become a very popular multi-user, multi-tasking operating system. The UNIX operating system can run on a large number of different hardware platforms, ranging from PCs to multi-processor servers and supercomputers.

There are some typical styles and characteristics of program development on UNIX:

1) Simplicity: Many useful UNIX tools are very simple. "Small and simple" is a technique worth learning. Larger, more complex systems are bound to contain larger, more complex bugs.

2) Centralization: It is better to have a program perform one task well than to pile all functions together. Programs with bloated functionality are difficult to use and maintain, and single-objective programs are more likely to be improved as better algorithms are developed. In UNIX, when new needs arise from users, we usually combine small tools to complete more complex tasks, rather than trying to put all the functions expected by a user into a large program.

3) Reusable components: Implement the core of the application as a library. Libraries with simple and flexible programming interfaces can help others develop similar programs or apply these techniques to new application areas.

1.1.2 GNU Project and Free Software Foundation

Having an operating system kernel is not enough. The development of Linux to this day is the result of the joint efforts of countless people. The operating system kernel itself is only a small part of the available development systems. General UNIX systems also include applications that provide system services and tools. For Linux systems, these programs are written and freely released by many programmers.

The Linux community supports the concept of free software, that is, the software itself should not be restricted and they comply with the GNU (GNU is the recursive abbreviation of GNU’s Not UNIX) General Public License (GPL).

The Free Software Foundation was founded by Richard Stallman. Its purpose is to try to create an operating system and development system that is compatible with UNIX systems but is not restricted by the UNIX name and source code privacy rights. environment.

The GNU Project has provided the software community with many imitations of applications on UNIX systems. For example, the famous one:

GCC: GNU Compiler Collection, which includes the GNU C compiler.

G: G compiler, part of GCC.

GDB: Source code level debugger.

GNU make

bash: command interpreter (a type of shell)

With these available free software, coupled with the Linux kernel, we can say: create The goal of a GNU, free UNIX-like system has been achieved through the Linux system. Due to the contributions made by GNU software, many people now usually refer to the Linux system as GNU/Linux.

1.2 Program Development Environment

If you want to do your job well, you must first sharpen your tools. If you want to develop Linux programs, you need to build a development environment first.

1.2.1 Linux environment installation

Linux development requires the installation of a Linux system environment. Generally, VMWare is used to install the Ubuntu system for development. For the specific installation process, you can refer to the following post https://zhuanlan.zhihu.com/p/38797088. I won’t go into too much introduction here.

1.2.2 SSH

Secure Shell (SSH) is a secure network protocol based on the application layer developed by IETF (The Internet Engineering Task Force). Traditional network service programs, such as FTP, Pop, and Telnet, are inherently insecure; because they transmit data, user accounts, and user passwords in plain text on the network, they are easily vulnerable to man-in-the-middle attacks.
SSH is divided into client openssh-client and server openssh-server. We need to install the ssh server under the ubuntu system in order to use the ssh client to log in to the system.

1) Install ssh

sudo apt update

sudo apt install openssh-server

2) Start ssh

sudo service ssh start

3) Use ip addr showCommand to view ubuntu system ip

Lets talk about common commands and environment variables of Linux operating system

It can be seen that the current ubuntu IP address is 192.168.176.148

4) Test ssh login

After installing the ssh server on Ubuntu, next Use MobaXterm to connect under Windows system and test whether the ssh server is installed successfully. The operation steps are as follows:
Run the Xterm software, select "Session", as shown in the figure below:
Lets talk about common commands and environment variables of Linux operating system

Click "SSH" and set it, as shown in the figure below:
Lets talk about common commands and environment variables of Linux operating system

In this dialog box, select "Protocol" as "SSH", "Host Name" as the IP address of ubuntu, and the user name as the current user name. Use the default value of 22 for the port number. After the settings are completed, click "OK" and the following command line terminal will appear, indicating that the virtual machine's Ubuntu system has been connected.
Lets talk about common commands and environment variables of Linux operating system

Through this method, after confirming that the network communication between the host and the development board is normal, you can log in to the development board.

5) Install Visual Studio Code

Visual Studio Code (VSCode for short) is a free editor from Microsoft, available for Windows, Linux and macOS version, is a cross-platform editor. You can search for the installation process by yourself. Here we mainly introduce the expansion packages used for C/C program development. The installation steps are shown in the figure below:
Lets talk about common commands and environment variables of Linux operating system

The following plug-ins are commonly used and need to be installed:

C/C.

C/C Snippets, C/C reused code blocks.

C/C Advanced Lint, that is, C/C static detection.

Code Runner, that is, code running.

Include AutoComplete, that is, automatic header file inclusion.

Rainbow Brackets, rainbow curly braces, help reading code.

One Dark Pro, the theme for VSCode.

GBKtoUTF8, convert GBK to UTF8.

ARM, which supports ARM assembly syntax highlighting.

Chinese(Simplified), that is, Chinese environment.

vscode-icons, VSCode icon plug-in, mainly icons for each folder under the resource manager.

compareit, comparison plug-in, can be used to compare the differences between two files

DeviceTree, device tree syntax plug-in.

Markdown Preview Enhanced, markdown preview plug-in.

Maridown pdf, convert .md files to other formats.

2. Common Linux commands

There are hundreds of Linux commands. For beginners, there are not many commonly used ones, and they do not need to be mastered. In the daily study and work process, if you encounter unfamiliar Linux commands, you will naturally remember them after checking more information over time. The following is an introduction to the 17 most basic commands in Linux.

1. Clear the screen

#clear

##2. Check the server ip address

ip addr

3. View time

date

##4 , directories and files

The file system is like a tree. The trunk is the / (root) directory, the branches are subdirectories, and there are branches behind the branches (there are subdirectories in the subdirectories). The directories Finally there are files.


Lets talk about common commands and environment variables of Linux operating systemThe paths of directories and files are divided into absolute paths and relative paths. Absolute paths are calculated from the root (/), such as /usr/etc/readme.txt. The relative path is calculated from the current working directory. If the current working directory is /usr, etc/readme.txt is equivalent to /usr/etc/readme.txt. In addition, it should be noted that: one dot. indicates the current working directory; two dots... indicates the upper-level directory of the current working directory.

The following is a brief introduction to the Linux system directory structure. In order to prevent many users from having wild ideas about the Linux system directory structure, the Linux Foundation released the FHS standard. Most Linux distribution systems follow this standard. Note: FHS (Filesystem Hierarchy Standard), file system hierarchy standard, which specifies the use of all first-level directories and some second-level directories (/usr and /var) in Linux systems. The main purpose of publishing this standard is to let users clearly understand what types of files each directory should store.


1) Linux root directory (/)
FHS believes that the root directory (/) of the Linux system is the most important for the following two reasons:
1. All directories are derived from the root directory;

2. The root directory is closely related to system startup, repair, and restoration;
Therefore, the root directory must contain startup software, core files, and programs required for startup. , function libraries, repair system programs and other files, as shown in the following table.


2) Linux /usr directory

usr (note not user), the full name is Unix Software Resource. This directory is used to store system software resources. FHS recommends that developers place software product data in subdirectories of the /usr directory rather than creating separate directories for their products.
In the Linux system, all system default software is stored in the /usr directory. The /usr directory is similar to the combination of C:\Windows\ C:\Program files\ in the Windows system.
The FHS recommends that the /usr directory should have subdirectories as shown in the following table.
Lets talk about common commands and environment variables of Linux operating system

3) Linux /var directory

/var directory is used to store dynamic data, such as cache, log files, files generated during software running, etc. Generally, it is recommended that this directory contain these subdirectories as shown in Table 4.
Lets talk about common commands and environment variables of Linux operating system

5. View the current directory

pwd

6. Change the current job Directory

cd directory name

Example:

1) Enter the /tmp directory

cd /tmp

2) Enter the upper level directory

cd ..

3) Enter the user’s home directory

cd

7. List directory and file information

ls is the abbreviation of list. Through the ls command, you can not only view directory and file information, but also view the permissions, size, owner, group and other information of directories and files.
Options -l List directory and file details.
Example:

1) List all directory and file name information in the current working directory

ls
Lets talk about common commands and environment variables of Linux operating system

2) List detailed information about all directories and file names in the current working directory

ls -l
Lets talk about common commands and environment variables of Linux operating system

3) Regular expressions

Regular expressions are also called regular expressions and wildcards. Both directory and file names support regular expressions and regular expression rules. There are many, here we only need to master the two most commonly used ones: asterisk "*".

Asterisk "*": Match any number of characters.

For example: ls open*.zip
Lets talk about common commands and environment variables of Linux operating system

##8. Create directory

1) Create the aaa directory in the current working directory

mkdir aaa

2) Create the aaa directory in the current working directory, And create the bbb directory in the aaa directory

mkdir aaa/bbb -p

9, delete the directory and files

rm [-rf]Directory or file

Option

-r can delete the directory. Without -r, only the file can be deleted.

Option

-f indicates forced deletion and does not require confirmation.

1) Delete the aaa directory

rm aaa -r

2) Force delete the current working directory ccc.c file

rm -f ccc.c

##9. Move directories and files

mv old directory or file name new directory or file name

1) Rename the book.c file in the current working directory to book1.c
mv book.c book1.c

2) If OPC/test3 is an existing directory, the following command will move the book.c file in the current working directory to in the OPC/test3 directory.
mv book.c OPC/test3

3) If the OPC/test3 directory does not exist, the following command will The book.c file is renamed OPC/test3.
mv book.c OPC/test3

10. Copy directories and files

cp [-r] Old directory or file name New directory or file name

Note: -r means recursion, English recursion


option

-r

Directories can be copied, if there is no option -r can only copy files. Example:

1) Copy the book1.c file in the current working directory to book2.c
cp book1.c book2 .c

2) Copy the aaa directory in the current working directory to bbb
cp aaa bbb -r

3) Copy the book1.c file in the current working directory to aaa/book1.c
cp book1.c aaa/book1.c

cp book1.c aaa/.

The above two commands have the same effect.

4) Copy the aaa directory in the current working directory to /tmp/aaa
cp -r aaa /tmp/aaa

<p><code>cp -r aaa /tmp/.

The above two commands have the same effect.

11. Determine whether the network is connected

ping -c Number of packets ip address or domain name

ping is used Determine whether the local host can successfully exchange data packets with another host and determine whether the network is smooth.

1) Ping five packets to the local host (127.0.0.1)

ping -c 5 127.0.0.1
Lets talk about common commands and environment variables of Linux operating system

2) Ping five packages to the server of Baidu www.baidu.com.cn

ping -c 5 www.baidu.com.cn
Lets talk about common commands and environment variables of Linux operating system

It can be seen that Baidu’s server can ping.

3) Ping five packets to Google www.google.com server.

ping -c 5 www.google.com
Lets talk about common commands and environment variables of Linux operating system

You can see that Google’s server is ping It doesn’t make sense.

12. Display the contents of a text file

There are three commands to display the contents of a text file: cat, more and tail.

1) cat command

cat file name

cat command displays the contents of the entire file at one time.

cat main.cpp

2) more command

more file name

In order to facilitate reading, the more command displays the contents of the file in pages. Press the space bar to display the next page, press the b key to display the previous page, and press the q key to exit.

3) tail command

tail -f file name

tail -f is used for display The last few lines of the text file will be refreshed in real time if the content of the file increases. For programmers, tail -f is extremely important. It can dynamically display the log of the background service program and be used to debug and track the running of the program.

13. Count the number of lines, words and bytes of text files

wc(word count) function is to count the number of bytes and words in the specified file , number of lines, and output the statistical results

wc file name
Lets talk about common commands and environment variables of Linux operating system

14. Search the content in the file

grep "Content" file name

Note that if there are no special characters such as spaces in the content, it does not need to be enclosed in double quotes.

Example:

1) Search signal

grep signal main.cpp in the main.cpp file
Lets talk about common commands and environment variables of Linux operating system

15. Search for files

1) find

find directory name-name file name

Parameter description:
Directory name: The directory to be searched. When searching for files, in addition to this directory name, it also includes its subdirectories at all levels.
File name: Rules matching the file name to be searched.
Example:
Start searching from the OPC directory and display all *.cpp files.

find OPC -name *.cpp

2) locate

locate file name

Example :
Search open62541.c file

locate open62541.c
Lets talk about common commands and environment variables of Linux operating system

Note: When using this command for the first time, you may need to update the database. Just execute the command according to the prompts.

find is mostly used for fuzzy search in a certain directory, while locate is for accurate positioning of the whole area.

16. The chmod command

is used to change the access permissions of Linux files or directories. This command has two uses. One is a text setting method that includes letters and operator expressions; the other is a numeric setting method that includes numbers.

Each file or directory has three groups of access permissions, each group is represented by three digits, which are the read, write and execute permissions of the file owner; the read, write and execution permissions of users in the same group as the owner. Execute permissions; read, write, and execute permissions for other users in the system.

Example: ls -l example
Lets talk about common commands and environment variables of Linux operating system

There are 10 positions in the first column, and the first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a dash, it means it is a non-directory file. If it is d, it means a directory. There are a total of 9 characters starting from the second character to the tenth. A group of 3 characters represents the permissions of 3 groups of users on the file or directory. The permission characters use a horizontal line to represent empty permission, r represents read-only, w represents write, and x represents executable.

Commonly used parameters:

  • -c When changes occur, report processing information

  • -R Process the specified directory and its subdirectories All files

Permission scope:

  • u: The current user of the directory or file

  • g: The current group of the directory or file

  • o: A user or group other than the current user or group of the directory or file

  • a: All users and groups

Permission code:

  • r: Read permission, represented by the number 4

  • w: Write permission, represented by the number 2

  • x: Execution permission, represented by the number 1

  • -: Delete permissions, represented by the number 0

  • s: Special permissions

Example:

1) Delete files example All users have executable permissions

chmod a-x example
Lets talk about common commands and environment variables of Linux operating system

2) Assign read, write, and execute (7) to the owner of example Permissions, assign read and execute (5) permissions to the group where example is located, and assign execution (1) permissions to other users

chmod 751 example -c
Lets talk about common commands and environment variables of Linux operating system

3) Add readable permissions to all files in the aaa directory and its subdirectories

chmod u r,g r,or -R text/ -c
Lets talk about common commands and environment variables of Linux operating system

17. Check the system disk space

df [-h] [-T]

option-h Display information in an easy-to-read format:

df -h
Lets talk about common commands and environment variables of Linux operating system

Options -TList files System type:

df -h -T
Lets talk about common commands and environment variables of Linux operating system

3. Explanation of Linux environment variables

3.1 Environment variables Concept

3.1.1 What are environment variables?

The execution of programs (operating system commands and applications) requires a running environment, which is composed of multiple environment variables.

3.1.2 Classification of environment variables

1) Classify according to the effective scope.

System environment variables: public, effective for all users.

User environment variables: User-private and customized personalized settings, which only take effect for this user.

2) Classification by life cycle.

Permanent environment variables: Configured in the environment variable script file, these scripts will be automatically executed every time the user logs in, which is equivalent to permanent effect.

Temporary environment variables: temporarily defined in the Shell when used, and will become invalid after exiting the Shell.

3) Linux environment variables

Linux environment variables are also called Shell environment variables. They start with an underscore and a letter and consist of an underscore, letters (case-sensitive) and numbers. They are customarily used Capital letters, such as PATH, HOSTNAME, LANG, etc.

3.2 Commonly used environment variables

3.2.1 View environment variables

1) env command

In Shell, use the env command to view all environment variables of the current user.
Lets talk about common commands and environment variables of Linux operating system

The above picture only captures some of the environment variables, not all.

Note: When using the env command, many environment variables are displayed on the screen, which is inconvenient to view. You can use grep to filter them.

env|grep Environment variable name

For example, check the environment variable whose name contains PATH.

env|grep PATH
Lets talk about common commands and environment variables of Linux operating system

2) echo command

echo $environment variable name

For example: echo $LANG

3.2.2 Commonly used environment variables

1) PATH

Search directory for executable programs. Executable programs include Linux system commands and user applications. The specific usage of the PATH variable is described in detail in later chapters of this article.

2) LANG

Language, region, and character set of the Linux system.

echo $LANG
Lets talk about common commands and environment variables of Linux operating system

3) HOSTNAME

The host name of the server.

echo $HOSTNAME

4) SHELL

The Shell parser currently used by the user.

echo $SHELL
Lets talk about common commands and environment variables of Linux operating system

5) HISTSIZE

The number of historical commands saved.

6) USER

The username of the currently logged in user.

echo $USER

7) HOME

The home directory of the currently logged in user.

echo $HOME

8) PWD

Current working directory.

echo $PWD
Lets talk about common commands and environment variables of Linux operating system

9) LD_LIBRARY_PATH

C/C language dynamic link library file search directory, it is not Linux default environment variables, but they are very important for C/C programmers. The specific usage is described in detail in the later chapters of this article.

3.3 Set the environment quantity

export variable name='value'

If the value of the environment variable does not have spaces or other special Symbols can be included without single quotes.

Example:

export PATH=$PATH:.

The above command adds .directory to the current shell’s environment variable PATH (yes.)

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

The above command adds the .directory to the environment variable LD_LIBRARY_PATH of the current shell

Environment variables set using export will become invalid after exiting the Shell and need to be reset the next time you log in. If you want the environment variables to take effect permanently, you need to configure them in the login script file.

3.3.1 System environment variables

System environment variables are effective for all users. There are three ways to set system environment variables.

1) Set in the /etc/profile file.

When the user logs in, execute the /etc/profile file to set the system environment variables. However, Linux does not recommend setting system environment variables in the /etc/profile file.

2) Add the environment variable script file in the /etc/profile.d directory. This is the recommended method for Linux.

/etc/profile will execute all script files under /etc/profile.d every time it is started. /etc/profile.d is easier to maintain than /etc/profile. If you don’t want any variables, just delete the corresponding shell script under /etc/profile.d. There are many script files in the

/etc/profile.d directory, for example:

ls /etc/profile.d
Lets talk about common commands and environment variables of Linux operating system

3) Set environment variables in the /etc/bashrc file.

The environment variables configured in this file will affect the bash shell used by all users. However, Linux also does not recommend setting system environment variables in the /etc/bashrc file.

3.3.2 User environment variables

User environment variables only take effect for the current user. There are many ways to set user environment variables.

In the user's home directory, there are several special files, which are invisible with ls but can be seen with ls .bash*.
Lets talk about common commands and environment variables of Linux operating system

1).bash_profile (recommended)

Executed when the user logs in, each user can use this file to configure their own environment variables.

2).bashrc

This file will be read when the user logs in and every time a new Shell is opened. It is not recommended to configure user-specific environment variables in it, because every time the user opens the For each shell, the file will be read once, and the efficiency will definitely be affected.

3).bash_logout

Execute this file every time you exit the system (exit the bash shell).

4).bash_history

saves the historical commands used by the current user.

3.3.3 The execution sequence of environment variable script files

The execution sequence of environment variable script files is as follows:

/etc/profile ->/etc/profile.d->/etc/bashrc->User's .bash_profile->User's .bashrc

Environment variables with the same name, if in multiple scripts If there is a configuration, the configuration in the last executed script shall prevail.

3.4 Detailed explanation of important environment variables

1. PATH environment variable

Search directory for executable programs, executable Programs include Linux system commands and user applications. If the directory of the executable program is not in the directory specified by PATH, the directory needs to be specified when executing.

1) The PATH environment variable stores a list of directories. The directories are separated by colon:, and the final dot indicates the current directory.

export PATH=Directory 1: Directory 2: Directory 3:… Directory n:.

2) PATH defaults to the directory where the Linux system commands are located (/usr/local/bin: /usr/bin:/usr/local/sbin:/usr/sbin), if these directories are not included, common Linux commands cannot be executed (the absolute path must be entered to execute).

Example:

ls /bin/ls You can see that the ls command is located in the /bin directory
Lets talk about common commands and environment variables of Linux operating system

At this time we execute the command export PATH= Clear the PATH variable and then execute the ls command. The system will prompt that the ls command cannot be found.
Lets talk about common commands and environment variables of Linux operating system

You can use the absolute path, /bin/ls
Lets talk about common commands and environment variables of Linux operating system

After adding the /bin directory to the environment variable PATH , ls can also be executed.
Lets talk about common commands and environment variables of Linux operating system

3) If the PATH variable does not contain a dot, you need to add ./ or use an absolute path to execute the program in the current directory.

Example:
When the example command is executed in the current directory, it prompts that it cannot be executed.
Lets talk about common commands and environment variables of Linux operating system

After adding the dot. to the environment variable PATH, you can execute it.

export PATH=$PATH:.
Lets talk about common commands and environment variables of Linux operating system

2. LD_LIBRARY_PATH environment variable

C/ The directory where C language dynamic link library files are searched. It is not a default environment variable in Linux, but it is very important for C/C programmers.

The LD_LIBRARY_PATH environment variable also stores a list of directories. The directories are separated by colon:, and the final dot . indicates the current directory, which has the same format as PATH.

export LD_LIBRARY_PATH=Directory 1: Directory 2: Directory 3:... Directory n:.

We will use this environment variable next time This lesson will introduce Linux static libraries and dynamic libraries in detail.

3.5 Validation of environment variables

1) Under Shell, the environment variables set with export take effect immediately on the current Shell and become invalid after the Shell exits.

2) The environment variables set in the script file will not take effect immediately. They will only take effect after exiting the Shell and logging in again, or use the source command to make it take effect immediately, for example:

source /etc/profile

3.6 Application experience

Although there are many ways to set environment variables, it is recommended that you set the system environment variables in /etc/profile Configure in the .d directory, and user environment variables are configured in the user's .bash_profile. It is not recommended to configure environment variables in other script files, as it will increase the trouble of system operation and maintenance and is prone to errors.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of Let's talk about common commands and environment variables of Linux operating system. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template