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 are the common commands and environment variables of linux operating system

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what are the common commands and environment variables of the linux operating system?" in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

1. Introduction to Linux and program development environment 1.1.What is Linux?

Linux is a freely released UNIX-like kernel implementation, which is the underlying core of an operating system. Linux was developed by Linus of the University of Helsinki in Finland with the help of a large number of UNIX programmers online. It was originally developed as a personal hobby inspired by Minix (a small UNIX-like system) taught by it, but then it gradually developed into a complete system.

To understand what Linux is, you 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, then a member of the telecommunications giant AT&T. 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 kinds of hardware platforms, ranging from PCs to multiprocessor 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 and more complex systems are bound to contain larger and more complex errors.

2) concentration: it is better for a program to perform a task well than to stack all the functions together. Bloated programs are difficult to use and maintain, and single-objective programs are more likely to be improved as better algorithms are developed. In UNIX, when users have new requirements, we usually combine gadgets to accomplish more complex tasks, rather than trying to put all the functions that a user expects into one 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 technologies to new applications.

1.1.2 GNU Project and Free Software Foundation

It is not enough to have an operating system kernel. The development of Linux today 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 distributed 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 a recursive abbreviation for GNU's Not UNIX) General Public license (GPL).

The Free Software Foundation (Free Software Foundation) was founded by Richard Stallman to try to create an operating system and development environment that is compatible with UNIX systems but is not subject to private ownership of UNIX names and source code.

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

The GCC:GNU compiler set, which includes the GNU C compiler.

Glossary is a part of GCC.

GDB: debugger at the source code level.

GNU make

Bash: command interpreter (a type of shell)

With this free software available, coupled with the Linux kernel, we can say that the goal of creating a GNU, free UNIX-like system has been achieved through the Linux system. Because of the contribution made by GNU software, many people now refer to Linux systems as GNU/Linux.

1.2 Program development environment

If you want to do a good job, you must first sharpen its tools, and if you want to develop Linux programs, you need to build a development environment.

1.2.1 Linux environment installation

Linux development requires the installation of a linux system environment, generally using VMWare to install the Ubuntu system for development. Specific installation process you can refer to the following post https://zhuanlan.zhihu.com/p/38797088 here I will not introduce too much.

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 clear text on the network, so they are 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 log in to the system using the ssh client.

1) install ssh

Sudo apt update

Sudo apt install openssh-server

2) start ssh

Sudo service ssh start

3) use the ip addr show command to view the ubuntu system ip

As you can see, the current ubuntu IP address is 192.168.176.148

4) Test ssh login

After installing the ssh server on Ubuntu, then use MobaXterm to connect under the Windows system to test whether the ssh server is installed successfully. The steps are as follows:

Run the Xterm software and select "Session", as shown in the following figure:

Click "SSH" and set it, as shown in the following figure:

In the dialog box, select the IP address of the "protocol" as "SSH", the "host name" as the ubuntu, and the user name as the current user name. The port number uses the default value of 22. When the setup is complete, click "OK", and the following command line terminal appears, indicating that the ubuntu system is connected to the virtual machine.

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, which has three versions: Windows, Linux and macOS. It is a cross-platform editor. During the installation process, you can search on your own. Here is an introduction to the extension package used for the development of the CumberCraft + program. The installation steps are shown in the following figure:

Commonly used plug-ins need to be installed:

C _ (b) C ~ (+).

C _ blank + Snippets, that is, _ reusing code block.

C _ pact + Advanced Lint, I. E., C _ pact + static detection.

Code Runner, that is, the code runs.

Include AutoComplete, that is, automatic header file contains.

Rainbow Brackets, rainbow curly braces, help to read the code.

One Dark Pro, the theme of VSCode.

GBKtoUTF8 to convert GBK to UTF8.

ARM, which supports ARM assembly syntax highlighting.

Chinese (Simplified), that is, Chinese environment.

Vscode-icons, VSCode icon plug-in, mainly the icon of each folder under the explorer.

Compareit, the 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 the .md file to another format.

2. Linux common commands

There are hundreds of Linux commands, which are not commonly used for beginners and do not need to be mastered. In the usual process of study and work, encountered strange Linux commands, check more information, accumulated over time, naturally remember. Here are the 17 most basic commands of Linux.

1. Clear the screen

Clear

2. Check the server ip address

Ip addr

3. Check the time

Date

4. Directories and files

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

The paths to directories and files are divided into absolute and relative paths, which are calculated from the root (/), for example, / usr/etc/readme.txt. The relative path is calculated from the current working directory, if the current working directory is / usr,etc/readme.txt equals / usr/etc/readme.txt. In addition, it is important to pay attention to: a dot. Represents the current working directory; two dots... Represents the directory one level above the current working directory.

The following is a brief introduction to the directory structure of the Linux system. In order to prevent many users from running wild on the directory structure of the Linux system, the Linux Foundation has issued the FHS standard. Most Linux distribution systems follow this standard. Note: FHS (Filesystem Hierarchy Standard), the file system hierarchical standard, which specifies the use of all primary directories and some secondary directories (/ usr and / var) in the Linux system. The main purpose of publishing this standard is to give users a clear understanding of what types of files should be stored in each directory.

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

two。 The root directory is closely related to the boot, repair and restore of the system.

Therefore, the root directory must contain boot software, core files, programs required to boot, function libraries, repair system programs, and other files, as shown in the following table.

2) Linux / usr directory

Usr (note that it is not user), known as Unix Software Resource, this directory is used to store system software resources. FHS suggests that developers should properly place the data of their software products in subdirectories under the / usr directory, rather than creating separate directories for their products.

In Linux systems, all system default software is stored in the / usr directory, and the / usr directory is similar to the combination of C:\ Windows\ + C:\ Program files\ directories in Windows systems.

FHS recommends that the / usr directory have the subdirectories shown in the following table.

3) Linux / var directory

The / var directory is used to store dynamic data, such as caches, log files, files generated during software operation, and so on. In general, it is recommended that you include these subdirectories as shown in Table 4.

5. View the current directory

Pwd

6. Change the current working directory

Cd directory name

Example:

1) enter the / tmp directory

Cd / tmp

2) enter the directory at the next level

Cd..

3) enter the user's home directory

Cd

7. List directory and file information

Ls is an acronym for list. Through the ls command, you can view not only directory and file information, but also directory and file permissions, sizes, owners and groups.

Option-l lists the details of directories and files.

Example:

1) list all the directory and file name information under the current working directory

Ls

2) list the detailed information of all directories and file names under the current working directory

Ls-l

3) regular expression

Regular expressions, also known as regular expressions, wildcards, directories and file names support regular expressions, there are many rules of regular expressions, here we only need to master the two most commonly used: the asterisk "*".

Asterisk "*": matches any number of characters.

For example: ls open*.zip

8. Create a directory 1) create an aaa directory under the current working directory

Mkdir aaa

2) create an aaa directory under the current working directory and a bbb directory under the aaa directory

Mkdir aaa/bbb-p

9. Delete directories and files

Rm [- rf] directory or file

The option-r can delete the directory, if not-r can only delete the file.

The option-f indicates a forced deletion and no confirmation is required.

1) delete the aaa directory

Rm aaa-r

2) forcibly delete ccc.c files in the current working directory

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 from the current working directory to the OPC/test3 directory.

Mv book.c OPC/test3

3) if the OPC/test3 directory does not exist, the following command will rename the book.c file in the current working directory to 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 can copy directories, 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 under 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 under the current working directory to / tmp/aaa

Cp-r aaa / tmp/aaa

Cp-r aaa / tmp/.

The above two commands have the same effect.

11. Judge whether the network is connected

Number of ping-c packets ip address or domain name

Ping is used to determine whether the local host can successfully exchange packets with another host and to 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

2) ping five packages to Baidu www.baidu.com.cn server

Ping-c 5 www.baidu.com.cn

It can be seen that Baidu's server can be connected with ping.

3) ping five packages to Google www.google.com 's server.

Ping-c 5 www.google.com

It can be seen that Google's server is ping impassable.

12. Display the contents of the text file

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

1) cat command

Cat file name

The cat command displays the contents of the entire file at once.

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 b to display the previous page, and press Q to exit.

3) tail command

Tail-f file name

Tail-f is used to display the last few lines of a text file and refresh it in real time if the content of the file increases. Tail-f is extremely important for programmers to dynamically display the logs of background service programs for debugging and tracking the running of the program.

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

The wc (word count) function counts the number of bytes, words and lines in the specified file, and outputs the statistical results.

Wc file name

14. Search for the contents of the file

Grep content file name

Note that if there are no special characters such as spaces in the content, you can enclose them without double quotation marks.

Example:

1) search for signal in the main.cpp file

Grep signal main.cpp

15. Search 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: the rule for matching the file name to be searched.

Example:

Start the search from the OPC directory and display all the * .cpp files.

Find OPC-name * .cpp

2) locate

Locate file name

Example:

Search for open62541.c files

Locate open62541.c

Note: the first time you use this command, you may need to update the database, just follow the prompted command.

Find is mostly used for fuzzy search in a certain directory, while locate is used for overall and accurate positioning.

16. Chmod command

Used to change access to linux files or directories. This command has two uses. One is a text setting method that contains letters and operator expressions; the other is a numeric setting method that contains numbers.

There are three groups of access rights for each file or directory, each group is represented by three digits, namely, the read, write and execute permissions of the file owner, the read, write and execute permissions of users in the same group as the master, and the read, write and execute permissions of other users in the system.

Example: ls-l example

The first column has 10 locations, and the first character specifies the file type. In a general sense, a directory is also a file. If the first character is a dash, it represents a file that is not a directory. If it's d, it means it's a directory. From the second character to the tenth, a total of 9 characters, a group of 3 characters, respectively represents the permissions of three groups of users to the file or directory. The permission character uses a dash for empty permission, r for read-only, w for write, and x for executable.

Common parameters:

-c report processing information when a change occurs

-R processes all files in the specified directory and its subdirectories

Scope of permissions:

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 a 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: execute permission, represented by the number 1

-: delete permissions, represented by the number 0

S: special permissions

Example:

1) delete all user executable permissions of the file example

Chmod Amurx example

2) assign read, write and execute (7) permissions to example owners, read and execute permissions to example groups, and execute permissions to other users

Chmod 751 example-c

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

Chmod upright r _ text/ _ c

17. View system disk space

Df [- h] [- T]

Option-h displays information in an easy-to-read way:

Df-h

Option-T lists the file system types:

Df-h-T

3. Linux environment variable explains the concept of environment variable 3.1.1 what is environment variable?

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

3.1.2 Classification of environmental variables 1) by the scope of entry into force.

System environment variables: public, valid for all users.

User environment variables: user-owned, custom personalization settings that take effect only for that user.

2) classified by life cycle.

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

Temporary environment variable: temporarily defined in Shell when in use and invalid after exiting Shell.

3) Linux environment variable

Linux environment variables, also known as Shell environment variables, underscore and start with letters, consisting of underscores, letters (case-sensitive), and numbers, traditionally using uppercase letters, such as PATH, HOSTNAME, LANG, and so on.

3.2Common environment variables 3.2.1 View environment variables 1) env command

Under Shell, use the env command to view all the environment variables for the current user.

The above image only intercepts some environment variables, not all of them.

Note: when using the env command, a lot of environment variables are displayed on the full screen, which is not easy to view and can be filtered with grep.

Env | grep environment variable name

For example, look at the environment variable that contains PATH in the environment variable name.

Env | grep PATH

2) echo command

Echo $environment variable name

For example: echo $LANG

3.2.2 commonly used environment variables 1) PATH

The search directory of executable programs, including Linux system commands and user applications. The specific use of PATH variables is described in detail in the later chapters of this article.

2) LANG

The language, region, character set of the Linux system.

Echo $LANG

3) HOSTNAME

The host name of the server.

Echo $HOSTNAME

4) SHELL

The Shell parser currently in use by the user.

Echo $SHELL

5) HISTSIZE

The number of commands that save history.

6) USER

The user name 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

9) LD_LIBRARY_PATH

This is not the default environment variable for Linux, but it is very important for Cpicard + programmers. The specific usage is described in detail in the later chapters of this article.

3.3 set the environmental quantity

Export variable name = 'value'

If the value of an environment variable does not have special symbols such as spaces, it can be contained without single quotation marks.

Example:

Export PATH=$PATH:.

The above command is added to the current shell environment variable PATH. Catalogue (yes.)

Export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

The above command is added to the current shell environment variable LD_LIBRARY_PATH. Catalogue

The environment variable set by export will expire after exiting Shell and need to be reset the next time you log in. If you want the environment variable to take effect permanently, you need to configure it 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, the environment variable of the system is set in the / etc/profile file. However, Linux does not recommend setting system environment variables in the / etc/profile file.

2) add the environment variable script file to the / etc/profile.d directory, which is recommended by Linux.

/ etc/profile executes all the script files under / etc/profile.d each time it starts. / 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, such as:

Ls / etc/profile.d

3) set the environment variable in the / etc/bashrc file.

The environment variables configured by 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, and there are many ways to set user environment variables.

In the user's home directory, there are several special files that are invisible with ls and visible with ls .bash *.

1) .bash_profile (recommended preferred)

When a user logs in, each user can use this file to configure their own environment variables.

2). Bashrc

The 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 a Shell is opened, the file will be read once, and the efficiency will definitely be affected.

3) .bash_logout

This file is executed each time you exit the system (exit bash shell).

4). Bash_history

The history commands used by the current user are saved.

3.3.3 execution order of environment variable script files

The execution order of the environment variable script file is as follows:

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

If the environment variable with the same name is configured in multiple scripts, the configuration in the last executed script shall prevail.

3.4. detailed explanation of important environmental variables 1. PATH environmental variables

A search directory for executable programs that include Linux system commands and user applications. If the directory of the executable program is not in the directory specified by PATH, you need to specify the directory when executing.

1) the PATH environment variable stores a list of directories, separated by colons, and the last dot. Represents the current directory.

Export PATH= directory 1: directory 2: directory 3: … Catalog nRO.

2) by default, PATH contains the directory where the Linux system commands are located (/ usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin). If you do not include these directories, the common Linux commands cannot be executed (you must enter an absolute path to execute them).

Example:

Ls / bin/ls can see that the ls command is located in the / bin directory

At this point, we execute the command export PATH= to clear the PATH variable, and then execute the ls command, and the system will prompt that the ls command cannot be found.

It can be used with absolute path, / bin/ls

After adding the / bin directory to the environment variable PATH, ls can also be executed.

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:

The prompt cannot be executed by executing the example command in the current directory.

Turn the dots. After you add it to the environment variable PATH, you can execute it.

Export PATH=$PATH:.

2. LD_LIBRARY_PATH environment variable

The directory where the language dynamic link library files are searched, which is not the default environment variable for Linux, but is very important for Cpicard + programmers.

The LD_LIBRARY_PATH environment variable also holds a list of directories, separated by colons, and the last dot. Represents the current directory, in the same format as PATH.

Export LD_LIBRARY_PATH= directory 1: directory 2: directory 3. Catalog nRO.

We will cover this environment variable in more detail when we talk about Linux static libraries and dynamic libraries next time.

3.5 entry into force of environmental variables

1) under Shell, the environment variable set with export takes effect immediately for the current Shell, and becomes invalid after Shell exits.

2) the environment variable set in the script file will not take effect immediately, it will not take effect until you log in again after you exit Shell, 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 configure the system environment variables in the / etc/profile.d directory and the user environment variables in the user. bash_profile, it is not recommended to configure environment variables in other script files, which will increase the trouble of system operation and maintenance and is prone to errors.

This is the end of the content of "what are the common commands and environment variables of the linux operating system". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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