Linux common commands, Linux common basic commands

Preface

I've been dealing with Linux recently, and I feel pretty good. I think the more troublesome thing about Linux than windows is that many things have to be controlled by commands. Of course, this is also the reason why many people like linux. It is relatively short but powerful. I will list the common Linux commands I have learned for your reference only.

For more linux C/C++ learning materials, you can pay attention to the WeChat public account: "C and C plus" Reply: "Linux" can be obtained 

1. System Information Linux Common Commands

arch show the machine's processor architecture
uname -m show the machine's processor architecture
uname -r show the kernel version in use 
dmidecode -q show the hardware system components - (SMBIOS / DMI) 
hdparm -i /dev/hda list the architecture of a disk Features 
hdparm -tT /dev/sda Perform test read operations on disk 
cat /proc/cpuinfo Show CPU info info 
cat /proc/interrupts Show interrupts 
cat /proc/meminfo Verify memory use 
cat /proc/swaps Show what swap is used 
cat /proc/version show kernel version 
cat /proc/net/dev show network adapters and statistics 
cat /proc/mounts show mounted filesystems 
lspci -tv list PCI devices 
lsusb -tv show usb device 
date show system Date 
cal 2022 Display the calendar in 2022 
date 061217002022.00 Set date and time - month day hour minute year. second 
clock -w save time modification to BIOS 

Second, the system shutdown, restart Linux common basic commands

shutdown -h now shuts down the system
init 0 shuts down the system
telinit 0 shuts down the system
shutdown -h hours:minutes & shuts down the system at the scheduled time 
shutdown -c cancels the  system shutdown at the scheduled time
shutdown -r now restarts
reboot restarts the
logout logout 

3. Basic commands commonly used in Linux for directory operations

1. Directory switching cd

Command: cd directory

cd / switch to the root directory
cd /usr switch to the usr directory under the root directory
cd ../ switch to the upper directory or cd ..
cd ~ switch to the home directory
cd - switch to the last visited directory

2. Directory view ls [-al]

Command: ls [-al]

ls View all directories and files in the
current directory ls -a View all directories and files in the current directory (including hidden files)
ls -l or ll List View all directories and files in the current directory (list view, show more information)
ls /dir View all directories and files in the specified directory such as: ls /usr

3. Directory operations [add, delete, modify, check]

Create a directory [Add] mkdir

Command: mkdir directory

mkdir aaa Create a directory named aaa in the current directory
mkdir /usr/aaa Create a directory named aaa in the specified directory

Delete a directory or file [delete] rm

Command: rm [-rf] directory

Delete files:
rm files delete files in the current directory
rm -f files delete files in the current directory (do not ask)

Delete a directory:
rm -r aaa recursively delete the aaa directory in the current directory
rm -rf aaa recursively delete the aaa directory in the current directory (do not ask)

Delete all:
rm -rf * Delete all directories and files in the current directory
rm -rf /* [Suicide command! Use with caution! Use with caution! Use with caution! 】Delete all files in the root directory

Note: rm can not only delete directories, but also other files or compressed packages. For the convenience of everyone's memory, no matter if any directory or file is deleted, use rm -rf directory/file/compressed package directly

Directory modification [change] mv and cp

Rename directory
    command: mv current directory New directory
    for example: mv aaa bbb Change directory aaa to bbb
    Note: The syntax of mv can not only rename directories but also rename various files, compressed packages, etc.

Cut directory
    command: mv directory name new location of the directory
    Example: Cut the aaa directory in the /usr/tmp directory to the /usr directory mv /usr/tmp/aaa /usr
    Note: The mv syntax can not only cut directories Cut operation, can perform cut operation on files and compressed packages, etc.

Copy directory
    command: cp -r directory name directory copy target location -r stands for recursion Example: copy the aaa directory in the /usr/tmp directory to the /usr directory     cp
    /usr/tmp/aaa /usr
You can copy directories and files, compressed packages, etc., without writing -r recursion when copying files and compressed packages

search directory [check] find

Command: find directory parameter file name
Example: find /usr/tmp -name 'a*' Find all directories or files starting with a in the /usr/tmp directory
 

Four, file operation Linux common commands

1. File operations [add, delete, modify, check]

Create a new file [Add] touch
command: touch file name
Example: Create a file named aa.txt in the current directory touch aa.txt

Delete file [Delete] rm
command: rm -rf file name

Modify the file [change] vi or vim

[3 modes of vi editor]
    Basically vi can be divided into three states, namely command mode (command mode), insert mode (Insert mode) and bottom line mode (last line mode), the functions of each mode are as follows :
1) Command mode command mode)
      Control the movement of the screen cursor, delete characters, words or lines, search, move and copy a section and enter Insert mode, or go to last line mode.
      Common commands in command line mode:
      【1】Control cursor movement: ↑, ↓, j
      【2】Delete current line: dd 
      【3】Search: / character
      【4】Enter edit mode: ioa
      【5】Enter bottom line mode ::
      
2) Edit mode (Insert mode)
      Only in Insert mode, text input can be done, press "ESC" key to return to command line mode.
      Common commands in edit mode:
      【1】ESC Exit edit mode to command line mode;
      
3) In last line mode,
     save the file or exit vi, and you can also set the editing environment, such as searching for strings, listing line numbers ……Wait.
     Common commands in bottom line mode:
     [1] Exit editing: :q
     [2] Force quit: :q!
     [3] Save and exit: :wq

open a file

Command: vi filename
Example: Open the aa.txt file in the current directory vi aa.txt or vim aa.txt

Note: After using the vi editor to open the file, it cannot be edited, because it is in command mode at this time, click i/a/o on the keyboard to enter the editing mode.

edit file

Use the vi editor to open the file and click the button: i, a or o to enter the editing mode.

i: start inserting before the character where the cursor is
a: start inserting after the character where the cursor is
o: insert a new line below the line where the cursor is

Save or cancel editing

save document:

Step 1: ESC to enter command line mode
Step 2: : Enter bottom line mode
Step 3: wq save and exit editing

Cancel editing:

Step 1: ESC to enter command line mode
Step 2: : Enter bottom line mode
Step 3: q! Cancel this modification and exit editing

File viewing [check]

File viewing command: cat/more/less/tail

cat: see the last screen

Example: Use cat to view the /etc/sudo.conf file, only the content of the last screen can be displayed
cat sudo.conf

more: percentage display

Example: use more to view the /etc/sudo.conf file, the percentage can be displayed, carriage return can go to the next line, space can go to the next page, q can exit to view
more sudo.conf

less: turn pages to view

Example: use less to view /etc/sudo.conf file, use PgUp and PgDn on the keyboard to page up and down, q to end view
less sudo.conf

tail: specify the number of lines or dynamically view

Example: Use tail -10 to view the last 10 lines of /etc/sudo.conf file, Ctrl+C to end  
tail -10 sudo.conf

2. Permission modification

rwx: r stands for readable, w stands for writable, x stands for the file is an executable file, if rwx becomes - at any position, it stands for unreadable or writable or non-executable file.

Example: Change the permission of aaa.txt file to executable file permission, the permission of aaa.txt file is -rw-------

The first one: - means it is a file, and d means a folder.
The first paragraph (3 digits): the permission of the owner The second paragraph
(3 digits): the group that the owner belongs to, and the permission of the group members.
(Last 3 digits): Represents the permissions of other users

   421  421  421
-  rw-   ---     --

Five, compressed file operation Linux common commands

1. Packing and compression

The extension of the compressed file in Windows .zip/.rar
The packaged file in      
linux: aa.tar The compressed file in    
linux: bb.gz The packaged and compressed file in linux: .tar.gz

Package files in Linux generally end with .tar, and compressed commands generally end with .gz.
In general, packaging and compression are carried out together, and the suffix name of the packaged and compressed file is generally .tar.gz.

Command: tar -zcvf Pack the compressed file name of the file to be packed
Among them: z: call the gzip compression command to compress
  c: pack the file
  v: display the running process
  f: specify the file name
  
Example: pack and compress the files under /usr/tmp The compressed package of all files is named xxx.tar
tar -zcvf ab.tar aa.txt bb.txt 
or: tar -zcvf ab.tar *

2. Decompression

Command: tar [-zxvf] compressed file    
where: x: represents decompression
Example: decompress ab.tar under /usr/tmp to the current directory

Example: Extract the ab.tar under /usr/tmp to the root directory /usr
tar -xvf ab.tar -C /usr------C represents the specified decompression location

Six, find the command Linux common commands

1、grep

grep command is a powerful text search tool

Example of use:

ps -ef | grep sshd Find the specified ssh service process 
ps -ef | grep sshd | grep -v grep Find the specified service process, exclude the gerp body 
ps -ef | grep sshd -c Find the number of the specified process 

2、find

The find command searches a directory structure for files and performs the specified action on the search results. 

find searches the current directory and its subdirectories by default, and does not filter any results (that is, returns all files), displaying them all on the screen.

Example of use:

find . -name "*.log" -ls Find files ending in .log in the current directory and display detailed information. 
find /root/ -perm 600 Find files with a permission of 600 in the /root/ directory 
find . -type f -name "*.log" Find the current directory and find ordinary files ending with .log 
. -type d | sort Find the current All directories and sort 
find . -size +100M Find files larger than 100M in the current directory

3、locate

locate allows users to quickly search for a path. By default, it is automatically updated once a day, so the latest changed files cannot be found using the locate command. To avoid this situation, you can use the updatedb command to manually update the database before using locate. If there is no queried data in the database, the error locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory will be reported! updatedb can be!

yum -y install mlocate If it is a streamlined CentOS system, you need to install the locate command

Example of use:

updatedb
locate /etc/sh Search all files starting with sh in the etc directory 
locate pwd Find all files related to pwd

4、whereis

The whereis command is used to locate executable files, source code files, and help files in the file system. The attributes of these files should belong to source code, binary files, or help files.

Example of use:

whereis ls finds all files related to the ls file

5、which

The function of the which command is to search the location of a system command in the path specified by the PATH variable and return the first search result.

Example of use:

which pwd finds the path where the pwd command is located 
which java finds the path of java in path 

Seven, su, sudo

1、his

su is used to switch between users. However, the user before the switch remains logged in. If it is root to switch to ordinary or virtual users, no password is required, otherwise, ordinary users need password authentication to switch to any other user.

su test: switch to the test user, but the path is still the /root directory
su - test : switch to the test user, the path becomes /home/test
su : switch to the root user, but the path is still the original path
su - : switch to root user, and the path is /root
su is not enough: If a user needs to use root privileges, the root password must be told to this user.

Exit to return to the previous user: exit

2、sudo

sudo is designed for all ordinary users who want to use root privileges. Allows ordinary users to temporarily use root privileges. Just enter the password for your account.

Enter the sudo configuration file command:

vi /etc/sudoer or visudo
case:
Allow hadoop user to execute various application commands as root, need to enter the password of hadoop user.
hadoop ALL=(ALL) ALL 
 
case:
Only the hadoop user is allowed to execute the ls and cat commands as root without entering a password. 
In the configuration file: 
hadoop ALL=NOPASSWD: /bin/ls, /bin/cat 

8. System Services

service iptables status --View the status of the iptables service
service iptables start --Open the iptables service
service iptables stop --Stop the iptables service
service iptables restart --Restart the iptables service
 
chkconfig iptables off --Turn off the iptables service's self-starting
chkconfig iptables on - -Enable the iptables service to start automatically at boot

Nine, network management Linux common commands

1. Hostname configuration

[root@node1 ~]# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1

2. IP address configuration

[root@node1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

3. Domain name mapping

The /etc/hosts file is used for IP address resolution when accessing by hostname. So, what kind of host name you want to access, you need to put the host name and its corresponding ip address.

[root@node1 ~]# vi /etc/hosts
#### Add
192.168.52.201 node1
192.168.52.202 node2
192.168.52.203 node3 at the end

Ten, timed task command crontab configuration Linux common commands

crontab is a command used by Unix and Linux to set up scheduled tasks. Through the crontab command, you can execute specified system commands or shell scripts at regular intervals. The unit of time interval can be minutes, hours, days, months, weeks, and any combination of the above.

crontab installation:

yum install crontabs
service operation instructions:

service crond start ## start the service 
service crond stop ## shut down the service 
service crond restart ## restart the service

1. Command format

crontab [-u user] file

crontab [-u user] [ -e | -l | -r ]

Parameter Description:

-u user: used to set the crontab service of a user  

file: file is the name of the command file, indicating that file is used as the task list file of crontab

and load crontab.

-e: Edit the contents of a user's crontab file. If no user is specified, it means editing the current

The user's crontab file.

-l: Display the contents of a user's crontab file. If no user is specified, it means that the current

Contents of the user's crontab file.

-r: delete the scheduled task configuration, delete a user's crontab from the /var/spool/cron directory

file, if no user is specified, the crontab file of the current user is deleted by default.

Command example:

crontab file [-u user] ## Replace the current crontab with the specified file
crontab -l [-u user] ## List the user's current crontab
crontab -e [-u user] ## Edit the user's current crontab

2. Configuration instructions

Command: * * * * * command  

Explanation: Time division day month week order

The first column indicates minutes 1 to 59 with * or */1 for every minute    

The second column represents the hour from 0 to 23 (0 means 0 o'clock)

The third column represents the date 1 to 31  

The fourth column represents the month 1 to 12  

The 5th column identifies the day of the week 0 to 6 (0 means Sunday)  

Column 6 Command to run

Eleven, other common Linux commands

View the current directory: pwd
Command: pwd View the current directory path

View processes: ps -ef
Command: ps -ef View all running processes

End the process: kill
command: kill pid or kill -9 pid (forcibly kill the process) pid: process number

Network communication command:
ifconfig: View network card information

Command: ifconfig or ifconfig | more

ping: Check the connection to a machine

Command: ping ip

netstat -an: View the current system port

Command: netstat -an

Search for the specified port
Command: netstat -an | grep 8080

Configure the network
Command: setup

Restart the network
Command: service network restart

Switch user
Command: su - username

Turn off the firewall
Command: chkconfig iptables off

or:

 iptables -L;
 iptables -F;
 service iptables stop
modify file permissions
Command: chmod 777

Clear screen
command: ctrl + l


After the shortcut key esc in vi mode :

Save and exit shortcut: shift+z+z

The cursor jumps to the last line shortcut key: shift+g

delete a line: dd

Copy a line of content: y+y

Paste the copied content :p

12. More linux C/C++ learning materials

For more linux C/C++ learning materials, you can pay attention to the WeChat public account: "C and C plus" Reply: "Linux" can be obtained  

Guess you like

Origin blog.csdn.net/weixin_55305220/article/details/123588501