Linux entry development: introduction to basic commands

Linux entry development: introduction to basic commands

1. Linux system introduction

Linux is a free-to-use and freely disseminated Unix operating system. It is a multi-user, multi-tasking, multi-thread and multi-CPU operating system based on POSIX and UNIX. Supports 32-bit and 64-bit hardware. Linux inherits Unix s network-centric design and is a multi-user network operating system with stable performance. There are many different versions of Linux, but they all use the Linux kernel. Linux can be installed in various computer hardware devices, such as mobile phones, tablet computers, routers, video game consoles, desktop computers, mainframes, and supercomputers, with strong portability.

Linux directory structure

/bin This directory stores commonly used Linux commands. In some versions, it is the same directory as the root directory.

/boot This directory stores the programs that are used when the system starts. When you use lilo to boot Linux, you will use some of the information here.

The/dev directory contains all the external devices used in the Linux system. It is actually the port for accessing these external devices. You can access these external devices, which is no different from accessing a file or a directory. For example, type "cd/dev/cdrom" in the system and you can see the files in the CD-ROM drive; type "cd/dev/mouse" to see the files related to the mouse.

The/etc directory stores various configuration files and subdirectories used in system management, such as network configuration files, file systems, X system configuration files, device configuration information, and setting user information.

/sbin This directory is used to store the system management program of the system administrator.

/home If a user named "xx" is created, there will be a corresponding "/home/xx" path in the/home directory to store the user's home directory.

/lib This directory is used to store system dynamic link shared libraries, and almost all applications will use the shared libraries in this directory.

/lost+found This directory is empty in most cases. But after a sudden power failure or abnormal shutdown, some files are temporarily stored here.

The/mnt directory is also empty under normal circumstances. You can temporarily mount other file systems in this directory.

/proc can obtain system information in this directory, which is generated by the system itself in memory

/root If you are logged in as a super user, this is the home directory of the super user

/tmp is used to store temporary files generated during the execution of different programs.

Many applications and files of the /usr user are stored in this directory.

/usr/X11R6: X-Window directory;

/usr/src: Linux source code;

/usr/include: system header file;

/usr/lib: store commonly used dynamic link shared libraries and static archives;

2. Introduction to Linux basic commands

2.1 su command: switch users.

Syntax: su [user name] [] means optional

There are two accounts under linux:

1. root-super user account (system administrator), use this account to do anything in the system.

2. Ordinary user--This account is for ordinary users and can perform limited operations.

The common usage of the su command is to become a super user. If an ordinary user issues the su command without a user name, the system prompts for the root password. After entering it, you can switch to the root user.

useradd testuser Create user testuserpasswd testuser Set password for the created user testuser Note: The newly created user will create a user directory testuserusermod --help under/home to modify the relevant parameters of this command userdel testuser delete user testuserrm -rf testuser delete user Directory where testuser is located

2.2 ls command: traverse directories

Function: ls is an abbreviation of the English word list, and its function is to list the contents of the directory. This is one of the most commonly used commands by users, because users need to view the contents of a directory from time to time. This command is similar to the dir command under DOS.

Syntax: ls [options] [directory or file]

Description: For each directory, this command will list all the subdirectories and files in it. For each file, ls will output its file name and other information requested. By default, the output entries are sorted alphabetically. When the directory name or file name is not given, the information of the current directory is displayed.

Common options:

-aShow all subdirectories and files in the specified directory, including hidden files.
-AShow all subdirectories and files in the specified directory, including hidden files. But "." and ".." are not listed.
-iThe i-node number of the file is displayed in the first column of the output.
-lDisplay the detailed information of the file in long format. This option is most commonly used.
-pAdd a "/" after the directory.
-RRecursively display the files in each subdirectory of the specified directory.
-tSort by time
-SSort by file size
-sDisplay the file size when listing files. If the -h parameter is added, the size unit can be displayed reasonably. For example: ls -sh
-FAdd a character at the end of the file to represent the type of the file. For example: * means executable,/means directory, = means socket

In the information displayed with the ls-l command, the beginning is a string of 10 characters, where the first character indicates the file type:

-Ordinary file, d directory, l symbolic link, b block device file, c character device file, p named pipe (FIFO), s socket file

The following 9 characters represent the access authority of the file, divided into 3 groups, each with 3 digits.

The first group represents the permissions of the file owner, the second group represents the permissions of the same group of users, and the third group represents the permissions of other users. The three characters in each group respectively represent the read, write and execute permissions for the file.

The permissions are as follows:

r Read (4)

w write (2)

x Execution (1) For directories, it means access authority.

-No permission is set.

2.3 man command: view help

Function: Used to view the help information of commands, functions, and header files.

**Usage: **man [page number] <command, function name, header file name>

The page number is generally 1-7 pages. If you cannot see the information you want on the current page, you can switch to another page to view it.

2.4 cd command: switch directory

Function: switch directory

Syntax: cd [directory path]

Description: This command switches the current working directory to the specified directory. If the directory path is not specified, it will return to the user's home directory~. In order to change to the specified directory, the user must have execute and read permissions on the specified directory.

Commonly used directory symbols:

cd .. to the parent directory, the upper-level directory, which is equivalent to "up"

cd-Go to the previous directory, equivalent to "back"

cd/to the root directory

cd ~ or just write cd back to the user's home directory

2.5 mkdir command: create a directory

Function: Create a directory

Syntax: mkdir [options] dirname

Description: This command creates a directory named by dirname. The user who created the directory is required to have write permission in the current directory (in the parent directory of dirname), and dirname cannot be an existing directory or file name in the current directory.

parameter:

-m Set access permissions for the newly created directory. You can also use the chmod command to set.

-p can be a path name. If some directories in the path do not yet exist at this time, after adding this option, the system will automatically create those directories that do not exist yet, that is, multiple directories can be created at one time.

Example of creating a multi-level directory: mkdir ./dir1/dir2/dir3 -p

2.6 touch command: create ordinary files

Function: Create a file.

Syntax: touch [file name]

2.7 rm command: delete files/directories

Function: It is easy to create files in linux, there will be files in the system that become outdated and useless at any time. The user can delete it with the rm command. The function of this command is to delete one or more files or directories in a directory. It can also delete a directory and all files and subdirectories under it. For linked files, only the link is deleted, and the original files remain unchanged.

Syntax: rm [options] <file or directory>

Note: If the -r option is not used, rm will not delete the directory.

parameter:

-f Ignore files that do not exist, and never give a prompt.

-r instructs rm to delete all directories and subdirectories listed in the parameter recursively.

-i for interactive deletion.

-v output deleted files

Use the rm command with extreme caution. Because once a file is deleted, it cannot be recovered. In order to prevent this from happening, you can use the -i option in the rm command to confirm each file to be deleted. If the user enters y, the file will be deleted. If you enter anything else, the file will be kept.

2.8 cat command: view file content

Function: View file content

Syntax: cat [options] [file]

parameter:

-b Number of non-empty output lines

-E displays $ at the end of each line

-n number all lines of output

-s do not output multiple blank lines

2.9 pwd command: display working directory

Function: In the Linux hierarchical directory structure, the user can use the mkdir command to create a new directory in any authorized directory, or use the cd command to switch from one directory to another. However, there is no prompt to tell the user which directory the user is currently in. To know the current directory, you can use the pwd command, which displays the entire path name.

Syntax: pwd

Description: This command displays the absolute path of the current working directory.

Example: pwd

The root directory is represented by a "/" at the beginning. If there is nothing behind pwd, the current location is displayed. If there is a lot of information on the screen, use clear to clear it.

2.10 cp command: copy files/directories

Function: Copy the given file or directory to another file or directory.

Syntax: cp [options] <source file or directory> <target file or directory>

Description: This command copies the specified source file to the target file or copies multiple source files to the target directory.

parameter:

-a This option is usually used when copying directories. It preserves links, file attributes, and copies directories recursively.

-d Keep the link when copying.

-f Delete the existing target file without prompting.

-The i and f options are opposite, a prompt will be given for the user to confirm before overwriting the target file. When you answer y, the target file will be overwritten, which is an interactive copy.

-r If the given source file is a directory file, cp will recursively copy all subdirectories and files in the directory. At this time, the target file must be a directory name. Generally use the -a parameter.

For example:

# cp -a mjpeg/socket/Recursively copy all files in the mjpeg directory to the socket directory # cp -a 123.c 456.c Copy the content of the 123.c file to the 456.c file.

2.11 mv command: rename, move, file/directory

Function: Rename files or directories or move files from one directory to another.

Syntax: mv [options] <source file or directory> <target file or directory>

Description: Depending on the type of the second parameter in the mv command (whether it is the target file or the target directory), the mv command will rename the file or move it to a new directory. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (or the source directory name), and it will rename the given source file or directory to the given one. Target file name. When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv command will move the source files specified by each parameter to the target directory. When moving files across file systems, mv first copies and then deletes the original files, and the link to the file will also be lost.

parameter:

-i Interactive operation. If the mv operation will cause the overwriting of the existing target file, the system asks whether to overwrite and asks the user to answer y or n, which can avoid overwriting the file by mistake.

-f Disable interactive operations. When the mv operation wants to overwrite an existing target file, no instructions are given. After this option is specified, the i option will no longer work.

If the given target file (not the directory) already exists, the content of the file will be overwritten by the new file at this time. In order to prevent users from accidentally using the mv command to damage another file, it is recommended that users use the i option when using the mv command to move files.

2.12 chmod command: modify file/directory permissions

Function: Change the access authority of a file or directory

Syntax: chmod [permission] <file or directory>

You can view the detailed information of a directory or file through the ls -l command. The 2nd to 10th characters represent the access permissions of the file, and every 3 of them is a group. The left three characters represent the owner permission, and the middle 3 characters Represents the permissions of users in the same group as the owner, and the 3 characters on the right are the permissions of other users. These three groups have a total of 9 characters, and their meanings are as follows:

rFor files, the read permission has the right to read the contents of the file; for directories, it has the right to browse the directory
wFor files, the writable permission has the permission to add and modify the content of the file; for directories, it has the permission to delete and move files in the directory.
xFor files, executable permissions have the permissions to execute files; for directories, the user has the permissions to enter the directory.

The permissions can be represented by a combination of numbers:

rCorresponding value 4
wCorresponding value 2
xCorresponding value 1

The key to the number setting is the value. At the beginning, many beginners will be confused. In fact, it is very simple. We regard rwx as a binary number. It can be expressed as: 111 101 100

Then convert every three digits into a decimal number, which is 754.

For example, we want the permissions of the 123.txt file to be:

ownerUsers in the same groupOther users
Readable rReadable rReadable r
WritableWritable wno -
Executable xno -no -

We first get the permission string according to the above table: rwx-rw-rw--, then the binary number is 111 110 100, and then every three digits are converted to a decimal number, and we get 764, so we execute the command:

[root@xiaolong test_code]# chmod 764 123.txt

2.13 ln command: establish a symbolic link

ln connection files or directories are divided into soft links and hard links.

Soft link syntax: ln -s <source file> <target file> (After deleting the source file, the link becomes invalid), which is equivalent to a shortcut.

Hard link syntax: ln <source file> <target file> (after deleting the source file, the target has no effect)

For example:

#ln -s a.txt p After creating the soft link, through the ls -l command, you can see that p has an arrow pointing to a.txt.

2.14 eog command: view pictures

Function: Open the picture browser to view pictures.

Syntax: eog <picture file>

For example: eog 123.png

2.15 echo command: output debugging statement

Function: The function of the echo command is to display a paragraph of text on the display, which generally acts as a reminder.

Syntax: echo [parameter] <output data>

parameter:

-n: Means not to automatically wrap in the final output.

Note: When using echo to output a string, the double quotes will be omitted automatically.

(1) Example:

echo "1234567890"echo 1234567890

The output of the above two commands is the same.

(2) Example: If you want to display symbols, you need to use escape symbols.

Echo "\"1234567890"\" output result => "1234567890"

2.16 Redefine to file

The commonly used redefinition symbols in the terminal are:> and >>

Among them: > symbol means overwrite, >> symbol means append.

Example:

echo "12345"> 123.txt//Output 12345 to the 123.txt file. echo "12345" >> 123.txt//Append 12345 to the 123.txt file. ls/> 123.txt//Output the data listed in the ls command to the 123.txt file.

2.17 du command: view the size of the disk/file

Function: View file size information.

Usage: du [parameter] <file name>

Common parameters are as follows:

-k Calculate the number of blocks in units of 1024 bytes.

-h Choose an appropriate unit to calculate the size.

-b Select the byte unit to calculate the size.

3. Introduction to the related commands of the file editor

Commonly used text editor commands under linux are: vi, vim, gedit, etc.

The vi and vim commands are editors based on the command line. The gedit command is an editor based on the mouse and keyboard, similar to Notepad under Windows.

3.1 Introduction to gedit editor

Example of gedit editor usage:

# gedit 123.c//Create a 123.c file and open it for editing

The opened interface is as follows:

Set the display line number:

When opening the file, if you need to enter the specified line position of the file, you can write the line number to be entered at the end of the command.

Example:

# gedit 123.c +88//means to jump directly to the 88th line of the 123.c file

3.2 Introduction to vim editor

Vim is divided into two states, command state and editing state. In the command state, the typed character system is processed as a command, such as: q stands for exit, and the edit state is used to edit text. When you enter vim, you will first enter the command state. In the command state, press "i" (insert) or "a" (add) to enter the edit state. In the edit state, press the ESC key to enter the command state.

Insert text:

a Add text from behind the cursor

A Add text from the end of the line where the cursor is

i Insert text from the front of the cursor

I Insert text from the beginning of the line where the cursor is

o Insert a new line at the next line where the cursor is currently located

O Insert a new line in the previous line where the cursor is currently located

s Delete the character where the cursor is located, and enter the edit mode

S Delete the line where the cursor is located, and enter the edit mode

r Enter a character to replace the character where the cursor is

R always replaces the character where the cursor is, until ESC is pressed

Delete and modify:

x delete the character at the cursor

dd delete the entire line where the cursor is

3dd delete the line where the cursor is and the two lines below

D or d$ delete the text from the cursor to the end of the line, and delete the comment sentence for common phrases

d^ or d0 delete the text from the cursor to the beginning of the line

The movement of the cursor:

h or the left arrow key ( ) move the cursor one character to the left

j or down arrow key ( ) move the cursor one character down

k or up arrow key ( ) the cursor moves up one character

l or the right arrow key ( ) the cursor moves one character to the right

w Move the cursor back one word

b Move the cursor one word forward

^ Move the cursor to the beginning of the line

$ Move the cursor to the end of the line

Ctrl+f turn down one page forward

Ctrl+b Turn up one page back

Ctrl+d turn down half a page down

Ctrl+u turn up half a page up

gg cursor to the head of the document

G cursor to the end of the document

H cursor to the top of the current page

L The cursor is positioned at the beginning of the last line of the current page

[n]+ Move the cursor back n lines, [n] represents an integer, such as 10+

[n]- Move the cursor forward n lines, [n] represents an integer, such as 10+

[n]G The cursor is positioned at the beginning of the nth line, [n] represents an integer, such as 10+

Find and replace:

/[str] Find the string str, [str] represents the string to be searched, all the found strings will be highlighted after pressing Enter, the command n will move to the next one found, and the command N will move to the previous one found String

Example:/hello

Block operation:  

        v Visualize the block selection status. After the block is selected, the block can be deleted (d), copied (y), cut (x)

        u Undo the last operation

        ctrl + r to restore the last operation

End editing:

        :q Quit without modifying the document

        :q! Abandon the modification of the document and forcibly exit

        :w save

        :wq save and exit

other:

        :help command View the help prompt of this command

        :%!xxd hexadecimal mode

        :%!xxd -r return to text mode

If you accidentally press Ctrl+s during the editing process, vi will be in a dead state, and press Ctrl+q to restore it.

        Execute vim +3 main.c//means to locate the third line of main.c

        Execute vim +/printf main.c//means to locate the first printf

        Enter in command mode: new 2.c//means to open another vi, which is horizontal and vnew 2.c means vertical

        To switch, use Ctrl+w and then press w to switch

Enter gg=G in the command mode to automatically align

Configure vim to display line numbers

Add the following code to the/etc/vimrc file:

set numberset tabstop=4

4. the basic use of gcc commands

Gcc is usually used as the main compiler under linux system. GCC was originally called GNU C Compiler (GNU C Compiler), because it could only handle C language. GCC quickly expanded and became able to handle C++. Later it was expanded to support more programming languages.

When using the GCC compiler, we must give a series of necessary call parameters and file names. There are about 100 call parameters of the GCC compiler, and only the most basic and most commonly used parameters are introduced here.

The most basic usage of GCC: gcc [parameter] [file name]

The commonly used parameters are as follows

-c Compile only: Do not link to become an executable file. The compiler only generates an object file with a .o suffix from the input source code files such as .c. It is usually used to compile subprogram files that do not contain the main program.

-o output_filename: Make sure that the name of the output file is output_filename, and this name cannot be the same as the source file. If this option is not given, gcc will give the default executable file a.out.

5. introduction to decompression commands

The most commonly used packaging program under Linux is the tar command. The package produced by tar is often called a tar package. The commands for tar package files usually end with .tar. After the tar package is generated, other programs can be used. Compressed.

5.1 Introduction to the tar command        

Function: tar is a compression and decompression tool. With tar, users can create archives (backup files) for a particular file, change files in the archive, or add new files to the archive. Tar was originally used to create archives on tape. Now, users can create archives on any device, such as floppy disks. With the tar command, a large number of files and directories can be packaged into one file, which is very useful for backing up files or combining several files into one file for network transmission. The tar on Linux is the GNU version.

syntax : tar [main option + auxiliary option] <target document> <source file or directory>

When using this command, the main option is required, it tells tar what to do, and the auxiliary option is for auxiliary use and can be used.

parameter:

c Create a new archive file. If the user wants to back up a directory or some files, he must select this option.

r Append the file to be archived to the end of the archive file. For example, the user has made a backup file, and found that there is still a directory or some files that have forgotten to back up, then you can use this option to append the forgotten directory or file to the backup file.

t List the contents of the archive files and check which files have been backed up.

u Update files. That is to say, replace the original backup file with the newly added file, and if the file to be updated is not found in the backup file, it will be appended to the end of the backup file.

x Release the file from the archive file.

Note: Only one c/x/t can exist! Cannot exist at the same time!

Auxiliary options:

b This option is set for the tape drive. It is followed by a number to indicate the size of the block, the system default value is 20 (20*512 bytes).

f Use archive files or equipment, this option is usually required. Please note that you must immediately pick up the file name after f! No more parameters!

k Save the existing file. For example, when we restore a certain file, if we encounter the same file during the restoration process, it will not be overwritten.

m When restoring files, set the modification time of all files to now.

M Create multi-volume archive files for storage in several disks.

v Report the file information processed by tar in detail. Without this option, tar does not report file information.

w Confirmation is required at each step.

z Use gzip to compress/decompress files with the suffix .gz. After adding this option, the archive file can be compressed, but you must also use this option to decompress when restoring.

j Use bzip2 to compress/decompress files with the suffix .bz2. After adding this option, the archive file can be compressed, but you must also use this option to decompress when restoring.

5.2 tar command decompression/compression example

Pack all files in the/test directory as test.tar files.

# tar -cvf test.tar/test

Note: If the packaged file or directory is an absolute path, a prompt may appear: tar: delete the beginning "/" from the member name

Add -P to the parameter to eliminate it.

Example:

# tar -cvPf test.tar/test

Unzip the packaged .tar file

# tar -xvf test.tar

Update file

That is to say, replace the original backup file with the newly added file, and if the file to be updated is not found in the backup file, it will be appended to the end of the backup file.

# tar -uf test.tar 123.c

List the files that have been packaged, which can be used to view which files have been backed up.

# tar -tf test.tar

Use gzip to compress/decompress files

# tar -czf test.tar.gz test///Compress# tar -xzf test.tar.gz//Unzip

Use bzip2 to compress/decompress files

# tar -cjf test.tar.bz2 test///Compress# tar -xjf test.tar.bz2//Unzip 

5.3 ZIP format compression/decompression

The zip and unzip programs are provided under linux to process ZIP format compressed packages, zip is a compression program, and unzip is a decompression program. There are many options for their parameters, and only a brief introduction is given below.

Compress all .jpg files into a zip package

# zip all.zip *.jpg

Unzip all the files in all.zip

# unzip all.zip

Common parameters:

-r Recursive processing, processing all files and subdirectories in the specified directory together.

Compress all files in the specified directory

# zip -r 123.zip/test/

-g: Generate the symbol information necessary for the symbolic debugging tool (GNU's gdb). If we want to debug the source code, we must add this option.

-O: The program is optimized for compilation and linking. With this option, the entire source code will be optimized during the compilation and linking process, so that the execution efficiency of the generated executable file can be improved, but the speed of compilation and linking is corresponding The ground is slower.

-O2: Better optimized compilation and linking than -O. Of course, the entire compilation and linking process will be slower.

-E: Only perform compilation preprocessing;

-S: Convert C code to assembly code;

Example:

# gcc test.c -o app

Specify the library and header file path when compiling

-L: Specify the dynamic library path. Example: gcc test.c -o app -L/usr/lib

-I: Specify the path where the header file is stored. Example: gcc test.c -o app -I/usr/include

-l: Specify the library name. Example: Example: gcc test.c -o app -lpthread