编程知识 cdmana.com

One 、 sketch

​ The record is simple Linux Basic operation 、 Commonly used shell command 、vi Editor common commands 、man The use of the help book .

Two 、Linux Basic operation

​ 1、 Press Ctrl+Alt+t Shortcut keys or double-click on the desktop “Terminal” Call out pseudo terminal ( Command line window ).

​ hyper terminal :

​ Ctrl+Alt+F6: tty6

img

​ Ctrl+Alt+F5 tty5

​ Ctrl+Alt+F4 Shared path between host and virtual machine

img

​ Ctrl+Alt+F3 tty3

​ Ctrl+Alt+F1 Return to the normal interface

​ 2、Liang@ubuntu:~$ ls

​ Liang: user name ;

​ ubuntu: Host name ;

​ ~: Home directory ( The home directory of the currently logged in user ),/home/Liang

​ $: Command prompt ( A sign )

​ ls: List the files in the current directory

​ 3、 Make the virtual machine get the mouse :Ctrl+G; Let the host computer get the mouse :Ctrl+Alt

​ 4、 List all file information in the current directory ls -la .-l The options are listed in a detailed list ;-a The option is to represent all files (all), Including hidden files .

img

5、 In the terminal window , Press Ctrl+C Forcibly stop a command or program that is currently being executed .

6、 Shutdown command : In the terminal window , Enter the command :poweroff

3、 ... and 、shell Command Introduction

 Common file operations :



 



1、 Edit the file     vi  file name         ( Or create a new file and use vi edit )



 



2、 Copy file      cp a file  b file       ( take a Make a copy of the document ,b It's copying files ( copy ).( Both files are in the current path , You can specify paths separately )



 



3、 Copy file directory   cp a Catalog  b Catalog  -r    take a Catalog ( Including all the files in it ) Content   Copied to the b Under the table of contents ,(-r  Recursive replication )



 



4、 New file      touch  file name         ( Create a new file if it doesn't exist , Update the latest modification time when it exists )



 



5、 Moving files      mv a file  b Catalog     ( take a File move to b Under the table of contents )



 



6、 Rename file    mv a file  b file    ( take a The file is named b file , notes : It's all in the current path )



 



7、 Delete file      rm a file           ( Delete a file )



 



8、 Delete file directory   rm a Catalog  -r       ( Delete a Catalog , Including the documents inside )
command function Example remarks
alias Alias the command alias c=’clear’ take clear Its alias is c
cat Show text content cat file Show file The content of
cd Change the current path cd /etc go to /etc In the middle
chmod Modify file access rights chmod 644 file Change file The authority of is 644
chown Modify file owner chown foo file Change file Owned by foo
clear Clear the screen clear Clear the screen
cp Copy files 1: cp file1 file22: cp dir1.0/ dir2.0/ -r 1: Copy file1 by file22: Copy dir1.0/ by dir2.0/
df View file system information df -h Display file system information
diff Compare the similarities and differences between the two documents 1: diff file1 file2 -uN 2: diff dir1/ dir2/ -urN 1: Compare file1 and file22: Compare dir1/ and dir2/
dpkg Install the package manually dpkg -i example.deb install example.deb
echo display string echo “hello!” Show “hello!”
find Find files find / -name “*.c” find / All under .c file
grep Find string grep “abc” ./* -rwHn ./* Find string in “abc”
ifconfig View or modify the network ifconfig eth0 see eth0 Internet information
kill Sending signal kill -s SIGKILL 1234 To a process 1234 Sending signal SIGKILL
ln Create link file 1: ln apple a2: ln apple a -s 1: take apple An alias for a2: Create a symbolic link a Point to apple
ls List file information ls List ( Current directory ) file information
man Find help man ls Find out about ls Aid of command
mount Mount or unload partitions mount /dev/x /mnt take /dev/x Hang on to /mnt Next
more Split screen display information ps -ef | more Split screen display ps -ef Information about
less And more similar less a.txt Show a.txt The contents of the document
head Show the The first few lines more a.txt Show a.txt Before 10 Row content
tail Show several lines at the end of the file tail a.txt Show a.txt Last 10 Row content
mkdir Create directory mkdir dir/ Create a new directory dir/
mv Move or rename files 1: mv file1 file2 2: mv file dir/ 1: Change file1 The name is file22: take file Move to dir/ Go to
pwd Show current path pwd Show current path
ps View system process information ps -ef perhaps ps ajx View system process information
rm Delete file 1: rm file2: rm dir/ -r 1: Delete file2: Delete recursively dir/
sort Sort sort file Yes file Sort and print to the screen
tar Archive or release Compress or decompress 1: tar cjf a.tar.bz2 *2: tar xjf a.tar.bz2 3: tar czf a.tar.gz *4: tar xzf a.tar.gz 1: Compress all files into .bz22: decompression .bz2 file 3: Compress all files into .gz4: decompression .gz file
uniq Remove adjacent duplicate lines uniq file Remove file The adjacent duplicate lines in
wc Counter wc a Calculation a The line of 、 Words and characters
which Find the path which ls Show command ls Location path
touch Change the timestamp of the file , Or create a file touch a.txt a.txt Modify the timestamp if it exists , Create... If it doesn't exist

Additional explanation :

(1)ls command :ls List all files in the current directory You can directly specify the directory ( route ) ls route

​ ls -l List all the files in the current directory in more detail You can directly specify the directory ( route ) ls -l route

(2)clear command : actually clear The command just pulls up the screen , There's no clear screen . Realize a similar windos Clear screen command of :cls

​ stay /usr/bin/ Under the table of contents . Use root user , Create a new one called cls The file of , Plus Execution Authority , Write in the content :

#/bin/bash



printf "\033c"

img

img

notes : Ordinary users switch to root user , Enter the command sudo -s, And then go back , Input password ( The password is not echoed ). And then directly vi cls, Edit content , Save and exit . Add permission command :chmod 777 cls . sign out root state , Return to previous user status , Enter the command :exit. And then you can use it cls As a clear screen command .

(3)tree command , List the file structure of the directory , It can be followed by a directory ( route ), The default is the current directory path .

If there are too many catalog files , It can be executed with parameters , For example, there are many files in the root directory , Commands available tree / -d -L 1 (-d List directories only. -L level,Max display depth of the directory tree. Numbers 1 Indicates that only one level of directory is listed ), have access to man tree Query detailed usage .

img

(4)mount command . Mount the hard disk 、U disc , Or remote shared directory, etc .

​ For example, mount U disc :

​ a) Look for what you just identified U Disk partition :sudo fdisk -l ( This is to view the partition identified by the current system , For example, the identified U The disk partition is :/dev/sdb )

​ b) take U Disk partition mount to /mnt Under the table of contents :sudo mount /dev/sdb /mnt ( Previous /mnt The files will be covered up , Until you unload U Disk partition )

​ c) uninstall U Disk partition :sudo umount /mnt

​ View the device hanging on the path information :df -h

(5)more command : Split screen ( Pagination ) display information , For example, the content of a file you need to view is relatively large , And don't want to open it , Commands available more test.txt Browse page by page . Press space or enter to page down , Press b Turn up , Press q sign out .

(6)<、> Redirect : For example, command ps -ef >1.txt , Will command ps -efd The output content of is output to 1.txt file ( If it doesn't exist, it will create , Overwrite if it already exists );

Use >>:ps -ef >>1.txt ( Will command ps -efd The output of is appended to 1.txt file ). Empty 1.txt The file can be used :> 1.txt command .< Redirect input .

(7)mkdir command , If you need to create a multi-level directory , add to -p Options . Such as creating a/b/c This kind of continuous empty directory , Use command :mkdir a/b/c -p

img

(8) tar command , Compressed files .

a) Compress it into .tar file 



 



         tar -cxf test.tar a.txt b.txt        ( take a.txt and b.txt Compress it into test.tar)



 



b) decompression .tar Compressed package of format 



 



         tar xvf test.tar                 ( decompression test.tar File to current path )



 



c) decompression .tar Format to the specified path 



 



         tar xvf test.tar -C /tmp            ( decompression test.tar File to /tmp)

Command options :

-c Create a compressed file

-x Release compressed file ( decompression )

-f Specify the name of the file to be compressed and decompressed

-v Show the process of command execution ( Such as which file to compress to and so on )

-z Use gzip The compression tool compresses 、 decompression , The file suffix is .gz

-j Use bzip2 The compression tool compresses 、 decompression , The file suffix is .bz2

    a) Compressed into .gz Compressed package of format 







        tar -czvf test.tar.gz a.txt b.txt        ( take a.txt and b.txt Compress it into test.tar.gz)







    b) decompression .gz Compressed package of format 







        tar -xzvf test.tar.gz                    ( take test.tar.gz Decompress the compressed package to the current path )







        tar -xzvf test.tar.gz -C /tmp            ( take test.tar.gz Decompress the package to /tmp)







 



    c) Compressed into .bz2 Compressed package of format 







        tar -cjvf test.tar.bz2 a.txt b.txt        ( take a.txt and b.txt Compress it into test.tar.bz2)







    d) decompression .bz2 Compressed package of format 







        tar -xjvf test.tar.bz2                    ( take test.tar.bz2 Decompress the compressed package to the current path )







        tar -xjvf test.tar.bz2 -C /tmp            ( take test.tar.bz2 Decompress the package to /tmp)

(9)wc command , Count the number of lines in the file (l)、 Number of words (w)、 Number of characters (c).

img

(10)which command , for example which ls, Show view ls Command path , Actually ls A command is an executable file .

(11)diff command : Compare the similarities and differences between the two files . for example :diff file1 file2, Compare file1 And file2 And output . You can use this command to generate patches

​ for example :diff dir1/ dir2/ > -urNB dir.patch ( Compare dir1 All the files in the directory are related to dir2 And the output is dir.patch Patch )

​ Patch using command :patch -p1 < Path to patch file ( Need to enter the path to be patched , such as patch -p1< ../dir.patch Need to enter into dir1 Catalog , After patching ,dir1 The content of is related to dir2 It's the same thing .)

(12)find command ,find The command recursively looks up all the subdirectories under the directory you are executing .find In addition to the command can be found by the name of the file , You can also press file permissions 、 Type of file 、 The search for documents Reference node number and so on to find .

​ Like by name ,find route -name " file name " . For example, in ./Desktop/code/ Find all of the .c Final document .

img

(13)grep command , Used to find strings in a file . Command format :grep What to look for The path to find - Parameters

For example, in hello.c Find string in ”hello”

img

grep Common options :

-r: If there are subdirectories , Then recursively find .

-w: Match the specified words exactly ( For example, specify the search ”apple”,”appletree” Will be eliminated )

-n: Print line number

-H: Print file name

for example , Recursively in /project/src Find all the words that contain ”hello” Of .c file , And print out the The name of the file and the line number of the word in the file , The order is :Liang@ubuntu:~$ grep “hello” /project/src/*.c -rwnH

Find a certain kind of file , Then look up the string or word we need in these files , Can be find and grep The command goes through the pipe ‘|’ Connect them and use them together , Make it more powerful , such as : To be in /project/src Find words in all the normal files in ”hello”, Ask for rejection of ”helloworld” Such a word :

Liang@ubuntu:~$ find /project/src -type f | xargs grep “hello” -wnH

xargs representative find The output of as grep Parameters of ( otherwise grep Only in find The name of the file list found Search for ”hello”, Instead of looking inside the file ).

(14)date command , Displays the current date without parameters / Time . Set time and date / Time ( Super permissions are required )

​ Set time command :sudo date -s "20180723 19:00" ( Set the time to 2018 year 7 month 23 Japan 19:00 when )

img

​ Format output :date "+%Y-%m-%d %H:%M:%S" ( Mm / DD / yyyy HHM / S )

img

(15)cal command , Show calendar

img

(16)head command , Show the first few lines of the file

img

​ (17) tail command , Show the last lines of the file

img

​ notes : have access to tail -f xxx.log Check it out. log What's new in the document ( When xxx.log When the log has new data ,tail New data will be displayed )

Four 、vi Common commands

​ vi Editor keyboard input is divided into two cases , In one case, your input is the text you edited files , In another case, your input is the action you want the editor to help you complete . In short ,vim Two operation modes are required type : Information typed in a mode , As the content of the text itself , It's called editing mode . Information typed in a mode , As an action of execution , Call it command mode . Just started vim The editor automatically enters command mode , In this mode, type a command corresponding to vim An action , For example, enter editing mode 、 Save text 、 Copy and paste 、 Find, replace, etc ; Press again in command mode i Key , Enter edit mode . In edit mode , Press Esc Press to exit edit mode , Enter command mode .

vim command meaning
:w Save the current file
:w filename Save the current file ( If entering vim The file name to edit is not specified , You need to add the file name when you save the file filename, If entering vim It's time to specify file name , So this usage is equivalent to “ Save as ”)
:q Exit the file currently being edited
:q! Force to exit the file currently being edited and discard all operations saved until now
:wq Save file and exit
u Undo the last action .( Press Ctrl+r Restore undone operations )
i Insert a character in front of the cursor
a Insert the character after the cursor position
o Insert a new line next to the cursor line
O Insert a new line on the line above the cursor line
x Cut the character at the cursor .(x You can press a number first , Then cut several characters )
dd Cut the line at the cursor .(dd You can press a number first , Then cut several lines )
yy Copy the line at the cursor .(yy You can press a number first , Then copy several lines )
: Numbers Jump to specified line (:18 Jump to 18 That's ok )
p Paste the data in the clipboard to the cursor
r Change the character of the cursor ,r And then the character you want to fix ( For example, if you want to fox in Of o Change to i, Just stop the cursor at o On , And then press... Continuously r and i)
h Move the cursor forward one character
j Move the cursor down one character
k Move the cursor up one character
l( Lowercase L) Move the cursor back one character
gg Jump to the first line of text
G Jump to the last line of the text
Ctrl + u Up (up) Page turning
Ctrl + d Down (down) Page turning
:%s/old/new Put all the old Replace the string with new
/string Look down the string from the cursor string, Note that after entering the string you want to find string Then press enter . If you're looking for a string string There are many. , You can press n take The cursor jumps to the next position , Press N Jump the cursor to the previous position
?string With the above </string> It's the same , The difference is that it looks up from the cursor
 Commonly used vi operation :



 



1、 Enter edit mode :         Press i



 



2、 Enter command mode :         Press Esc



 



3、 Save file command           :w



 



4、 Exit edit command           :q



 



5、 Save and exit the command          :wq



 



6、 Copy line command            yy



 



7、 Cut line command          dd         ( Or the deletion effect )



 



8、 Paste command          p



 



9、 Revocation order          u



 



10、 Counter revocation order         Ctrl+r



 



11、 Search for commands         /str         (str Is the string to find )



 



12、 Replace the command         :%s/old/new    ( Put all the  old  Replace the string with  new)



 



13、 Go to the beginning of the file        gg



 



14、 Go to the end of the file       G



 



15、 Go to the beginning of the line            ^



 



16、 Go to the end of the line            $



 



17、 Cut from the cursor ( contain ) All characters from the beginning to the end of the line          d$



18、 Cut from the cursor ( Not included ) All characters from the beginning to the beginning of the line         d^



19、 Copy from the cursor ( contain ) All characters from the beginning to the end of the line         y$



20、 Copy from the cursor ( Not included ) All characters from the beginning to the beginning of the line       y^



 



 



21、 Block selection replication :



 



         Copy way 1: Mouse selected , Press y Copy ,p Paste .



 



         Copy way 2: Press v key , Get into Visual state , Press the up, down, left and right direction keys to select the area ( From the current cursor ( contain ) Start ), Press y Copy ,p Paste .



 



22、 Block selection cuts  ( Delete effect ):



 



         Cut way 1: Mouse selected , Press y shear ( Or press Delete key ),p Paste .



 



         Cut way 2: Press v key , Get into Visual state , Press the up, down, left and right direction keys to select the area ( From the current cursor ( contain ) Start ), Press d Copy ( Or press Delete key ),p Paste .



 



23、 Copy the contents of one file to another  



 



     For example a Copy part of the file to b In file 



 



        a) Open the terminal   cat a file        ( Show a The content of the document , Then you can choose 、 Right click replication (copy))



 



        b) use vi Open the file you want to paste into  vi b file 



 



        c) Right click Paste (paste)



 



24、 In command line mode :ZZ        Save and exit 



 



25、 Future generations , In command line mode , Press ggvG ( among gg It's back to the beginning ,v Is to enter visual Pattern , It's equivalent to block selection , Capitalization G It's at the end of the article ,ggvG The combination is from the beginning to the end )     ( Or at the terminal cat Right click to copy )

Add :

(1)【 Command line mode 】 Set up vi According to the line Numbers 、 Auto indent, etc , In command line mode :set option ( Add... Before the option no It's a ban )

​ Options :

​ number According to the line Numbers

​ ruler The bottom of the screen shows the line of the cursor 、 Column position

​ autoindent Automatic indentation

​ all List all options

such as set number Set the side display line number .set nonumber Set to not show line numbers

img

The above setting method only takes effect in the currently edited document . Global validation needs to be set vi Configuration file for . Of all users vim The configuration file /etc/vim/vimrc, For a user vim The configuration file /home/ user name /.vimrc.

For example, set the current user's profile ,( If not, create )

img

​ set backspace=2 You can use backspace key to delete at any time

​ set mouse=a Allow the mouse to move the cursor ” ( We are used to where we click the mouse , Where you can edit , But there are vi The default cursor is moved by the arrow keys )

​ syntax on Grammar test , Keyword coloring

​ set tabstop=4 Set up tab Width of key

​ To configure all users, you need to modify the file /etc/vim/vimsrc ( Super permissions are required ) For example, the default line number is displayed 、 Automatic indentation 、 Allow the mouse to move the cursor

img

(2)【 Command line mode 】Ctrl+v Square selection ,( Similar to notepad++ Press on the top Shift+Alt Key for mouse selection )

img

(3)【 Edit mode 】Ctrl+n Automatic completion For example, type pri, Then press Ctrl+n, I'll make it up ntf.

5、 ... and 、man Help book

1、man Commands help us find the information we need , And this information is classified into the following categories ( Details It can be used man Command to query yourself ): There are actually 9 book man Help document to manage the information separately , But maybe the system only installed the second 1 book ( I.e. find Shell Ordered man manual ), Other man The manual needs to be installed manually .

Liang@ubuntu:~$ man man



    1 Shell  command ( Default installed )



    2  system call 



    3  Library function 



    4  Special documents ( Usually occurs in /dev  Under the table of contents )



    5  A special format or agreement for a document ( for example /etc/passwd  The format of )



    6  game 



    7  miscellaneous ( For example, some macro definitions )



    8  The system administrator commands ( Usually it can only be performed by an administrator )



    9  Nonstandard kernel routines 

2、 With integrity man After the manual, you can find the information you want , Like looking for information about read The usage and explanation of Information about . Look up that manual for read This function :

Liang@ubuntu:~$ man -f read



    read (2) - read from a file descriptor



    read (1posix)- read a line from standard input 



    read (3posix) - read from a file

among ,read (1posix) What is offered is Shell Command help information ,read (2) What's provided is a system call ,read (3posix) It provides library functions and so on , If you want library functions read Help for , Enter the command :man 3posix read( Or in short man 3 read)

That's all Liangxu tutorial website For all of you Linux Related knowledge .

版权声明
本文为[Pioneer of code farmers]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/02/20210208195826922y.html

Tags linux manual
Scroll to Top