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

Shell commands commonly used in linux system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Concept

Linux, whose full name is GNU/Linux, is a set of Unix-like operating system that is free to use and spread freely. It is a multi-user, multi-task, multi-thread and multi-CPU operating system based on POSIX and Unix. With the development of the Internet, Linux has received support from software enthusiasts, organizations and companies all over the world. In addition to maintaining a strong momentum of development in servers, it has made great progress in personal computers and embedded systems. Users can not only intuitively obtain the implementation mechanism of the operating system, but also modify and improve Linux according to their own needs to maximize meet the needs of users.

Shell is the user interface of the system, which provides an interface for users to interact with the kernel. It receives the command entered by the user and sends it to the kernel for execution.

Shell is actually a command interpreter that interprets commands entered by the user and sends them to the kernel. Not only that, Shell has its own programming language for editing commands, which allows users to write programs made up of shell commands. Shell programming language has many characteristics of common programming language, for example, it also has loop structure and branch control structure. Shell programs written in this programming language have the same effect as other applications.

Linux provides a visual command input interface like MicrosoftWindows-the graphical user interface (GUI) of X Window. It provides a lot of desktop environment systems, which operate like Windows, with windows, icons and menus, all of which are controlled by the mouse. GNOME .

Users of each Linux system can have their own user interface or Shell to meet their own specialized Shell needs.

1. Control of experimental environment.

As a learner of linux system, it is likely to cause irreversible damage to the system due to some improper operation. So I hope you can practice in the virtual machine first. The following command is for the user to start a virtual machine on the command line and display the command to reset if necessary.

Rht-vmctl start workstation # start a workstation

Rth (redhat)-vm (virtual mahine) ctl (control) is the virtual machine control command of the Red Hat system as its name implies.

Start start

Workstation workstation

Rht-vmview view workstation # displays the workstation

View view

Rht-vmctl stop workstation # shut down the virtual machine

Rht-vmctl poweroff workstation # Power off immediately

Unlike stop, poweroff does not shut down the running program normally and then shuts down, but shuts down immediately.

Rht-vmctl reset workstation # reset the virtual machine

When the operator makes an irreversible error, such as deleting the boot file mistakenly so that the system file cannot be loaded, the virtual machine can be reset, which is equivalent to the formatting of the mobile phone.

At this point, everyone has a virtual machine.

2. Command line operation

After opening the terminal on the virtual machine, we can use the computer by entering commands. What we open here is a shell window that we use to interact with the linux kernel.

Keyboard shortcuts for shell:

Ctrl+shift+n # starts a new shell

Ctrl shift t # opens a new page in the current shell

Ctrl+d\ Ctrl shift d # turn off shell

Keyboard shortcuts for command line operations:

Ctrl+c # terminates the command when there is a problem with the execution of the current operation and wants to shut down immediately.

Ctrl a # cursor moves to the beginning of the command line

Ctrl e # cursor moves to the end of the command line

Ctrl u # command before deleting the cursor on this line

Ctrl t # command after deleting the cursor on this line

Ctrl shift c # copy the selected command

Ctrl shift v # paste the selected command

3. Command format a) Line prompt

[kiosk@foundation0 Desktop] $

Kiosk # user with Shell enabled @ # delimiter foundation0 # host short name (Note: the operator can connect to other hosts, so the information displayed here is necessary) Desktop # the name of the current working directory $# identity prompt, representing the current operation permission information b) command specification

Kiosk@foundation0 Desktop] $whitespace

Commands must be entered after the line prompt. Invalid commands and parameters and targets are separated by spaces between the command and the target without the line prompt. "-" indicates the abbreviation of the word in the parameter, and the full spelling parameter of the word is used to specify some functions of the command. The release line prompt (CTRL+C) is used to complete the command line with or without parameters-a-b-c =-abc =-cab =-bac 4. Common command categories

This is a practical command that needs to be used frequently in our basic operation, and needs to be mastered and memorized.

1) Historical command record call

History # View history command! + number # execute the line command of the number in history! + letter # execute the most recent command beginning with this initial letter ctrl r + keyword # search for the history with this keyword | # display the command history directly history-c # empty the history command record

2) the establishment of a file or directory (a directory is what we call a folder)

Touch file name # create file touch file name 1 file name 2. # create multiple files touch-t year month day time file name # modify file time information (note: the touch command can not only create files, but also modify its information) mkdir directory name # create directory mkdir directory name 1\ directory name 2. # create multiple directories mkdir-p multi-level file names # create multi-level directories, if not, create

3) File deletion

Rm file # remove files rm-f file1 file2 # force removal of multiple files

4) Directory deletion

Rm-r dir #-r (recursion) recursive deletion, because there are multiple files under a directory by default, so you need to recursively delete rm-fr dir1 dir2 dir3 #-f (force) to force the recursive removal of multiple files

5) File editing

Gedit file name # graphical open file vim file name # command line open file # insert # push insert mode # Save and exit

6) File viewing

History > File name # put the history contents in the file (used to create a non-empty file to facilitate subsequent operations) cat file name # display all contents of the file cat-b file name # display all contents of the file and line number (no blank lines)

Cat-n file name # displays the full contents and line number of the file (displays blank lines)

Head file name # shows the first 10 lines of the file

Head-n file name # shows the first n lines of the file

Tail file name # 10 lines after the file is displayed tail-n file name # after the file is displayed n lines

Less filename # Open the file less with less (paging browsing tool, which opens the less environment. Less is a tool to help display the text of a file)

| | # View line by line | # View page by page / keyword # search keywords, n down match N up match v # enter vim from lee environment finish: wq return to lessq # exit stat file name # view the file status attribute

7) copy of files

Cp (copy) cp original file destination file # copy the source file to the destination file, if the destination file exists, it will be overwritten by the source file, if it does not exist, create a cp file 1 file 2 file 3 directory # copy file 1 file 2 file 3 to the directory cp-r directory 1 directory 2 # recursively copy directory 1 to directory 2

8) the movement of files

Mv (move)

Note: the same disk is only a change of file name, different disks have file creation process and file deletion process mv file name 1 file name 2 # overwrite if file name 2 exists, rename it to file name 1mv directory 1 directory 2 # move directory 1 to directory 2mv file name 1 file name 2 directory # move file name 1 file name 2 to directory df # disk information

9) View file types

File file name # find the file type of the file

For example: #! / bin/bash # shell script file

# include # C Source files

10) File content statistics

Wc (word count) wc-l # statistics lines linewc-w # statistics words (words) wordwc-m # statistics characters mwc-c # statistics bytes c (octet?) Number

11) File path

Pwd (pathway) # current path

Cd absolute path / equivalent path # move to a path under cd.. # return to the previous directory cd- # return to the previous path

12) list files

Ls (list) list- I # attributes list-d # attributes of the directory itself list-s # File size list- S # sort by size list- a # shows all files, including hidden files

13) File search match

* # wildcard? # wildcard? # fr f [[: alpha:]] ile # all deleted rm-fr f [[: digit:]] ile # all deleted rm-fr f [[: alnum:]] ile # all deleted rm-fr f [: punct:]] ile # all sandwiched characters rm-fr f [[: space:]] Delete all spaces in ile # rm-fr f [[: space:] [punct]] ile # Delete all characters or spaces in the middle rm-fr [[: space:] [punct]] # as long as all characters or spaces are deleted It's the same with others.

14) commands and actions for the vim editor

Personalized configuration for the vim editor

A) it can be temporarily configured, and the information is only stored in memory. If you enter ": set nu" in the vim command line, the line label will be displayed in the currently open vim editor. When the editor is closed and the command expires, the next time you open the editor line mark does not exist.

B) if you want the line tag to exist forever, you can configure it as follows.

For the current user (ordinary user, non-super user) to configure the vim editor in the current user's home directory, execute vim ~ / .vimrc to enter the vim configuration file

After entering the configuration file, enter the "set parameter" to configure the current user's vim.

For example: ": set nu" displays the line label in vim.

(note that if the super user enters vim at this time, the line mark will not be displayed, because the above setting is only valid for ordinary users)

C) the super user's configuration of the vim editor will work on all files, execute vim / etc/vimrc to enter the vim configuration file, and edit it at the beginning or end of the configuration file to operate on the superuser and each ordinary user.

(guess: the average user inherits the super user's profile, which can be read by the ordinary user, while the average user's profile is not readable to the superuser. )

If you want to make more cool settings, you can search on your own!

Search keyword

Enter "/ keyword" on the command line and highlight the keyword entered after entering enter

: noh # Unhighlight

N # Down match

N # up match

Batch operation

Copy

Y (n) l # copy n letters

Y (n) w # copy n words

Y (n) y # copy n lines

Delete

D (n) l # delete n letters

D (n) w # delete n words

D (n) d # Delete n lines

Shear

C (n) l # cut n letters

C (n) w # cut n words

C (n) c # cut n lines

Note: after cutting, you will enter insert mode.

Paste

P

Revocation

U

Restore

Ctrl+r

Vim visualization mode for batch operation

Press [ctrl+v] in command mode to enter visualization mode

The selection area can be moved up and down the keyboard.

Add characters in batch in visual mode

Ctrl + v Select the column where you want to add characters

A) in uppercase [I]

B) add characters

C) Press [esc]

Character substitution in vim

:% s / original character / replaced character / g

(note:% s represents all the lines of the full text, g represents the columns of the original characters that appear in the full text)

So the above command replaces the target characters in all rows and rows in the document

Replacement example:

:% sswap: / @ / g replace the full text with: @

: 1recover5sCompact: / @ / g replace lines 1-5: @

: / hello/,/bye/s/: / @ / g replaces the: from hello to bye to @

(note: two replacement keywords need to be unique and cannot be included in each other. For example, abcde and abc cannot.) Split screen function of vim

Ctrl+w s is split up and down

(note: the split file is the same file, and the modification of one of the files is equivalent to the modification of all split files.)

Ctrl+w v left and right split screen

Ctrl+w c closes the screen where the cursor is located

Ctrl+w up and down the left and right cursor to the specified screen

Sp file2 edits the current file and file2 cursor movement in command mode at the same time

Gg # move the cursor to the beginning of the first line

The G # cursor moves to the end of the file

: the number # cursor moves to the writing mode of the specified line vim to enter

I # where the cursor is inserted

I # insert at the beginning of the line where the cursor is located

O # insert the next line of the cursor

Insert the previous line on the line of the O # cursor

Insert the next character of the character where the a # cursor is located

A # insert at the end of the line where the cursor is located

Delete and insert the character where the s # cursor is located

Delete the line of the S # cursor and insert the exit mode of vim

Q # exit, used when the contents of the file have not been changed

: wq # exit save

: Q! # forced exit does not save, used when you do not want to save after changing the file

: wq! # Force exit save is used when writing read-only files. Note that the edited read-only files must exit abnormally with vim

The file .swp is a temporary file generated when the vim file is edited, and if shell is turned off when the file is edited, the file will be left behind. And the data that has not been written is saved in this file. If the file is deleted manually, vim will resume normal work, but the changed content will not be saved.

When vim is forcibly closed before the authoring content is saved, an error will occur the next time it is opened. Here are the shortcut keys for handling error reports:

[O] pen Read-Only read-only open

[e] dit anyway continues editing

[r] ecover recovery data

[d] elete it deletes the [.swp] file

[Q] uit exits

[a] bort exits after recovery

(suggestion: r first, then wq, then D, and then open it to see the file that saved the last change.)

15) Home directory

The home directory, as its name implies, is the home of the current user, which is actually the place where the current user can perform the current permissions and operate.

For example, the superuser's home directory is in root, which means that the superuser can do all kinds of operations on the root directory. The average user's home directory is under the / home/ user name, which means that ordinary users can do all kinds of operations under this directory and can only access the parent directory of their own home directory. (it is equivalent to only visiting other people's homes but not decorating them. Understanding this is important for subsequent path learning and the operation of home commands and even an understanding of permissions. )

The ~ in the prompt line indicates the current user's home directory

Cd ~ will enter the current user's

~ username enters the specified user's home directory

~ + current directory (~ + is equivalent to., however. It is only used in shell, and ~ + can be used in Unix)

~-"-" in the directory (olddir) cd-before entering the current directory (Note:-in cd-can only represent old directories in cd, and old directories can be indicated in all commands of ~-Zia)

Knowing that the above commands basically perform daily simple operations on the linux system and its shell, you can also write in the vim editor.

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