Linux learning day3: Linux commands novices must master (2)

Time:2020-7-28

  The commands learned today are often used in the operation and maintenance work, which are very practical. We must study them carefully and try to master them well. The specific contents are as follows:

1、 System status monitoring command


   1. Ifconfig command

It is used to obtain network card configuration and network status information.

[[email protected] Desktop]# ifconfig
eno16777728: flags=4163  mtu 1500
        inet 192.168.134.128  netmask 255.255.255.0  broadcast 192.168.134.255
        inet6 fe80::20c:29ff:feff:6b1f  prefixlen 64  scopeid 0x20
        ether 00:0c:29:ff:6b:1f  txqueuelen 1000  (Ethernet)
        RX packets 32  bytes 3653 (3.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 7267 (7.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 6  bytes 560 (560.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 560 (560.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Eno16777728: network card name
INET 192.168.134.128: IP address
  Ether 00:0c: 29: FF: 6B: 1F: MAC address
  RX packets 32 bytes 3653 (3.5 KIB): the number of received packets and the cumulative traffic. If the system is restarted, the traffic will be cleared
  TX packets 58 bytes 7267 (7.0 KIB): the number of packets sent and the cumulative traffic. If the system is restarted, the traffic will be cleared

   2. Uname command

It is used to view the system kernel and system version information. It is usually fixed with the – a parameter

[[email protected] ~]# uname -a
Linux linuxprobe.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux

   3. Uptime command

It is used to view the load information of the system. The average load value (BOLD information) of the system in 1 minute, 5 minutes and 15 minutes should not exceed 1 for a long time, and should not exceed 5 in the production environment.

[[email protected] ~]# uptime
 11:12:49 up 23 min,  2 users,  load average: 0.08, 0.04, 0.05

   4. Free command

It is used to display the memory usage information in the current system. It is generally used in combination with the – h parameter to increase readability.

[[email protected] ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          1.9G       910M       1.1G       9.6M       924K       295M
-/+ buffers/cache:       614M       1.3G
Swap:         2.0G         0B       2.0G

   5. Who command

It is used to view the user terminal information of the current Login Host. The output result below shows that a terminal is currently opened.

[[email protected] ~]# who
root     :0           2020-02-17 10:49 (:0)
root     pts/0        2020-02-17 10:50 (:0)

  6. Last command

It is used to view all login records of the local computer. It should be noted that the login records can be tampered with artificially.

[[email protected] ~]# last
root     pts/0        :0               Mon Feb 17 10:50   still logged in   
root     :0           :0               Mon Feb 17 10:49   still logged in   
(unknown :0           :0               Mon Feb 17 10:49 - 10:49  (00:00)    
reboot   system boot  3.10.0-123.el7.x Mon Feb 17 10:49 - 11:32  (00:42)    
root     pts/2        192.168.134.1    Sun Feb 16 19:58 - 20:05  (00:07)    
root     pts/1        192.168.134.1    Sun Feb 16 16:51 - down   (04:48)

  7. History command

Used to display the commands that have been executed in history. You can also use the “! Number” method to repeatedly execute a command.

[[email protected] ~]# history
    1  cat /etc/redhat-release 
    2  reboot
    3  ls
    4  passwd root
    5  su - linuxprobe
    6  shutdown
    7  shutdown -r

  8. Sosreport command

It is used to collect system configuration and architecture information and output diagnostic documents.

[[email protected] ~]# sosreport

sosreport (version 3.0)

This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications.

An archive containing the collected information will be generated in
/var/tmp and may be provided to a Red Hat support representative.

Any information provided to Red Hat will be treated in accordance with
the published support policies at:

  https://access.redhat.com/support/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.

Press enter to continue, or ctrl-c to quit. // press enter here to confirm the collection

Please enter your first initial and last name [linuxprobe.com]:      //Press enter here to confirm the host number
Please enter the case number that you are generating this report for:  //Press enter here

Running plugins. Please wait …

Running 1/69: abrt…
Running 6/69: auditd…
Running 7/69: block…
Running 69/69: yum…
Creating compressed archive…

Your sosreport has been generated and saved in:
/var/tmp/sosreport- linuxprobe.com -20200217114206. tar.xz / / the location where the generated diagnostic document is saved

The checksum is: fd53340877a57c34803cccbcc9d15676

Please send this file to your support representative.

2、 Working directory switch command


  1. PWD command

Used to display the user’s current working directory.

[[email protected] ~]# pwd
/root

  2. CD command

Used to switch working paths. The “CD -” command returns the previous directory, the “CD…” command returns to the parent directory, and the “Cd ~” command switches to the current user’s home directory.

  3. Ls command

It is used to display the file information in the directory. The – a parameter can display the hidden file, and the – L parameter can display the file attribute, size and other details.

[[email protected] ~]# ls -al
total 64
dr-xr-x---. 14 root root 4096 Feb 17 16:28 .
drwxr-xr-x. 17 root root 4096 Feb 17 16:28 ..
-rw-------.  1 root root 1032 Feb 15 20:24 anaconda-ks.cfg
-rw-------.  1 root root 3299 Feb 17 16:28 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
drwx------.  9 root root 4096 Feb 15 19:46 .cache
drwx------. 15 root root 4096 Feb 15 19:46 .config
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
drwx------.  3 root root   24 Feb 15 12:25 .dbus
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Desktop
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Documents
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Downloads
-rw-------.  1 root root   16 Feb 15 19:41 .esd_auth
-rw-------.  1 root root 1240 Feb 17 16:28 .ICEauthority
-rw-r--r--.  1 root root 1083 Feb 15 12:26 initial-setup-ks.cfg
drwxr-xr-x.  3 root root   18 Feb 15 19:41 .local
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Music
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Pictures
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Public
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Templates
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Videos
-rw-------.  1 root root 1150 Feb 15 20:11 .viminfo
-rw-------.  1 root root  120 Feb 16 19:58 .Xauthority

3、 Text file editing command


  1. Cat command

Used to view plain text files (with less content), the – n parameter can display line numbers.

[[email protected] ~]# cat -n initial-setup-ks.cfg 
     1    #version=RHEL7
     2    # X Window System configuration information
     3    xconfig  --startxonboot
     4    
     5    # License agreement
     6    eula --agreed
     7    # System authorization information
     8    auth --enableshadow --passalgo=sha512
     9    # Use CDROM installation media
    10    cdrom
    11    # Run the Setup Agent on first boot
    12    firstboot --enable

  2. More command

For viewing plain text files (with more contents), you can use the space key or the Enter key to page down.

[[email protected] ~]# more initial-setup-ks.cfg 
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom

  3. Head command

It is used to view the first n lines of a plain text document. It is generally used with the – n parameter. The following command indicates that you can view the first 10 lines of a document.

[[email protected] ~]# head -n 10 initial-setup-ks.cfg 
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom

  4. Tail command

It is used to view the last n lines of a plain text document or to continuously refresh the contents. The former command form is “tail – N 20 file name”, and the latter is “tail – f file name”.

[[email protected] ~]# tail -f /var/log/messages
Feb 17 17:40:36 linuxprobe systemd: Starting Hostname Service...
Feb 17 17:40:36 linuxprobe dbus-daemon: dbus[1042]: [system] Successfully activated service 'org.freedesktop.hostname1'
Feb 17 17:40:36 linuxprobe dbus[1042]: [system] Successfully activated service 'org.freedesktop.hostname1'
Feb 17 17:40:36 linuxprobe systemd: Started Hostname Service.
Feb 17 17:40:37 linuxprobe chronyd[1013]: Selected source 162.159.200.123
Feb 17 17:40:37 linuxprobe kernel: TCP: lp registered
Feb 17 17:40:51 linuxprobe systemd-logind: Removed session c1.
Feb 17 17:41:07 linuxprobe systemd: Starting Session 2 of user root.
Feb 17 17:41:07 linuxprobe systemd: Started Session 2 of user root.
Feb 17 17:41:07 linuxprobe systemd-logind: New session 2 of user root.
Feb 17 17:41:25 linuxprobe chronyd[1013]: Selected source 202.112.29.82

  5. TR command

Used to replace characters in text, for example, to replace all English in text with uppercase.

[[email protected] ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
#VERSION=RHEL7
# SYSTEM AUTHORIZATION INFORMATION
AUTH --ENABLESHADOW --PASSALGO=SHA512

# USE CDROM INSTALLATION MEDIA
CDROM
# RUN THE SETUP AGENT ON FIRST BOOT
FIRSTBOOT --ENABLE
IGNOREDISK --ONLY-USE=SDA
# KEYBOARD LAYOUTS
KEYBOARD --VCKEYMAP=US --XLAYOUTS='US'
# SYSTEM LANGUAGE
LANG EN_US.UTF-8

   6. WC command

Used to count the number of lines, words and bytes of the specified text. The – L parameter displays the number of text lines.

[[email protected] ~]# wc -l /etc/passwd
38 /etc/passwd

  7. Stat command

It is used to view the specific storage information and time of the file.

[[email protected] ~]# stat anaconda-ks.cfg 
  File: ‘anaconda-ks.cfg’
  Size: 1032          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 68860812    Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2020-02-18 14:55:53.095941142 + 0800 // the last time to view the file contents
Modify: 2020-02-15 20:24:56.710988547 + 0800 // the time when the file content was last modified
Change: 2020-02-15 20:24:56.710988547 + 0800 // the time when the file permissions or attributes were last changed
 Birth: -

  8. Cut command

It is used to extract text characters by column. The – D parameter sets the interval symbol and the – f parameter sets the number of column characters to be extracted. The following command means to extract the first column of data with ‘:’ as the separator.

[[email protected] ~]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp

  9. Diff command

Used to compare the differences between multiple text files, the – brief parameter only returns the comparison results, and the – C parameter compares the differences between multiple files in detail.

[[email protected] ~]# diff -c anaconda-ks.cfg initial-setup-ks.cfg 
*** anaconda-ks.cfg    2020-02-15 20:24:56.710988547 +0800
--- initial-setup-ks.cfg    2020-02-15 12:26:03.533009017 +0800
***************
*** 1,26 ****
  #version=RHEL7
  # System authorization information
  auth --enableshadow --passalgo=sha512
- 
  # Use CDROM installation media
  cdrom
  # Run the Setup Agent on first boot
  firstboot --enable
- ignoredisk --only-use=sda
  # Keyboard layouts
  keyboard --vckeymap=us --xlayouts='us'
  # System language
  lang en_ US.UTF -8---------------------------------

4、 File directory management command


  1. Touch command

It is used to create a blank file or set the file time. You can set the file viewing time (atime), permission or attribute change time (CTime) and file modification time (mtime), corresponding to the three times in stat command. -The D parameter indicates that atime and mtime are modified at the same time.

[[email protected] ~]# ls -l anaconda-ks.cfg  
-rw-------. 1 root root 1032 Feb 15 20:24 anaconda-ks.cfg
[[email protected] ~]# 
[ [email protected]  ~]# touch -d "2019-2-18 15:20" anaconda- ks.cfg      //Modify Anaconda- ks.cfg Atime and mtime of files

  [[email protected] ~]# ls -l anaconda-ks.cfg
-rw——-. 1 root root 1032 Feb 18 2019anaconda- ks.cfg / / modification successful

  2. MKDIR command

Used to create a blank directory, combined with the – P parameter to create a nested file directory.

[[email protected] ~]# mkdir -p a/b/c/d
[[email protected] ~]# 
[ [email protected]  ~]#CD a // / enter directory a
[ [email protected]  a] # CD B // / enter directory B
[[email protected] b]#

  3. CP command

It is used to copy files and directories in the format of “CP parameter source file target file”. The copy operation can be divided into four situations:

  • If the target file is a directory, copy the source file to the directory;
  • If the target file is also a normal file, you will be asked whether to override the target file;
  • If the target file does not exist, the normal copy operation is performed;
  • If the source file is also a directory, you need to add the – R parameter.
[ [email protected]  ~]# cp anaconda- ks.cfg  anaconda- ks.cfg_ Bak // backup Anaconda- ks.cfg file
[[email protected] ~]# ls a anaconda-ks.cfg_bak Documents initial-setup-ks.cfg Pictures Templates anaconda-ks.cfg Desktop Downloads Music Public Videos [[email protected] ~]#

  4. MV command

Used to cut or rename a file. To cut a file in the same directory is to rename it.

[ [email protected]  ~]# mv anaconda- ks.cfg_ bak anaconda- ks.cfg_ Cpoy // will Anaconda- ks.cfg_ The BAK file is renamed anaconda- ks.cfg_ copy
[[email protected] ~]# ls
a                anaconda-ks.cfg_cpoy  Documents  initial-setup-ks.cfg  Pictures  Templates
anaconda-ks.cfg  Desktop               Downloads  Music                 Public    Videos
[[email protected] ~]#

   5. RM command

Used to delete a file or directory. The – f parameter indicates forced deletion without prompting for confirmation. The – R parameter is required to delete a directory.

[ [email protected]  ~]# rm -f anaconda- ks.cfg_ Cpoy // force deletion of Anaconda- ks.cfg_ Copy file
[[email protected] ~]# 
[[email protected] ~]# ls
a  anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
[[email protected] ~]#

   6. DD command

  It is used to copy or convert files according to the specified size and number of data blocks. The related parameters and functions are as follows:

parameter effect
if Input file name, i.e. input file
of The name of the output file, i.e. output file
bs Set the size of each block
count Set the number of copied blocks
[ [email protected]  ~]# dd if=/dev/zero of=test_ File count = 1 BS = 20m // in the / dev / zero device file, take a data block with a size of 20m, and then save it and name it test_ File
1+0 records in
1+0 records out
20971520 bytes (21 MB) copied, 0.0999151 s, 210 MB/s
[[email protected] ~]# ls
a  anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  test_file  Videos
[[email protected] ~]#

 7. File command

The type used to view the file.

[[email protected] ~]# file anaconda-ks.cfg 
anaconda- ks.cfg : ASCII text // text file
[[email protected] ~]# 
[[email protected] ~]# file Downloads
Downloads: directory // directory
[[email protected] ~]#

5、 Pack compression and search command


 1. Tar command

It is used to pack, compress and decompress files. The related parameters and their functions are as follows:

parameter effect
-c Create compressed file
-x Unpacking compressed files
-z Compress or decompress with gzip
-j Compress or decompress with bzip2
-v Shows the compression or decompression process
-f Target file name
-P Keep the original permissions and attributes
-C Specify the directory to which to unzip 
[ [email protected]  ~]# tar -czvf  test.tar.gz  /Root // package and compress the / root directory in gzip format. The compressed package is named test.tar.gz
tar: Removing leading `/' from member names
/root/
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda- ks.cfg -----------------Omitting part of the compression process information-----------------

 [[email protected] ~]# ls
  a Desktop Downloads Music Public test_file Videos
  anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test.tar.gz

Next, unzip the compressed file to the / root / test directory. Note: the – f parameter must be placed at the end of the parameter, regardless of compression or decompression.

[ [email protected]  ~]#MKDIR test // create the test directory
[[email protected] ~]# 
[ [email protected]  ~]# tar -xzvf  test.tar.gz  -C / root / test // decompress
root/
root/.bash_logout
root/.bash_profile
root/.bashrc
root/.cshrc
root/.tcshrc
root/anaconda-ks.cfg

2. Grep command

  It is used to perform keyword search in text and display the matching results. The format is “grep parameter keyword file name”. The related parameters and their functions are as follows:

parameter effect
-b Search executable files as text files
-c Show only the number of rows found
-i ignore case
-n set number
-v Reverse selection – lists only rows without keywords
[ [email protected]  Root] ා grep root / etc / passwd // search the / etc / passwd file to display the line containing the keyword "root"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

  3. Find command

  It is used to find the file according to the specified conditions, and the format is “find” to find the condition operation. The related parameters and their functions are as follows:

parameter effect
-name Match name
-user Match owner
-group Match all groups
–type b/d/c/p/l/f

Matching file type (the following letter parameter indicates block device, directory, character device, pipeline, linked file and text file in turn)

-size Match file size (+ 50kb for files over 50kb, – 50kb for files less than 50kb)
-exec …. {}\; This can be followed by commands for further processing of search results
[ [email protected]  Root] # find / root - name test * // find the file whose file name begins with test in the / root directory
/root/test_file
/root/test/root/test_file
[[email protected] root]#

  Find command advanced experiment: find all the files belonging to the Linux probe user in the whole file system and copy them to the / root / findresults directory.

[ [email protected]  ~]#Find / - user linuxprobe - exec CP - a {} / root / findresults / \; // where {} represents every file searched by the find command, that is, assuming the find command searches for the first time test.txt File, execute CP - A test.txt  /root/findresults/
find: ‘/proc/10213/task/10213/fd/6’: No such file or directory
find: ‘/proc/10213/task/10213/fdinfo/6’: No such file or directory
find: ‘/proc/10213/fd/6’: No such file or directory
find: ‘/proc/10213/fdinfo/6’: No such file or directory
cp: cannot overwrite non-directory ‘/root/findresults/linuxprobe’ with directory ‘/home/linuxprobe’
[[email protected] ~]# 
[ [email protected]  Findresults] # LS - L // view the command execution results
total 3868
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:37 1960
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:48 1967
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:45 1975
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 19:28 1979
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 15:22 1981
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:29 2031
-rw-r--r--.  1 linuxprobe linuxprobe     696 Feb 15 12:29 6c80566e9d01473b8952f753857b506f-card-database.tdb
-Rw-r -- R --. 1 linuxprobe linuxprobe 10 Feb 15 19:28 6c80566e9d01473b8952f753857b506f default sink--------------------------