The way of Linux learning -- basic commands

Posted by Mike521 on Fri, 04 Feb 2022 07:06:17 +0100

1, Linux command line Basics

  • Linux command is an instruction or program used to realize a certain kind of function. When executing most commands in Linux, you should find the program corresponding to the command
[root@server1 ~]# hostname  #Query host name
localhost
[root@server1 ~]#which  hostname   #Program corresponding to query command
/usr/bin/hostnamet
  • The execution of the command depends on the interpreter (default interpreter / bin/bash)
    • User - > interpreter - > kernel - > Hardware
  • Command line full format:
    • Command word (option)... (parameter 1) (parameter 2)
[root@server1 ~]# cat  --help           #View command help information
...
[root@server1 ~]# cat -n /etc/shells
     1  /bin/sh
     2  /bin/bash
     3  /usr/bin/sh
     4  /usr/bin/bash
[root@server1 ~]# ls   -l    /etc/passwd    #Long format display
-rw-r--r--. 1 root root 2319 Nov 29  2020 /etc/passwd
	#Detailed properties of directory contents are displayed by default

Computer capacity unit:

It is generally expressed by B, KB, MB, GB, TB, PB, EB, ZB, YB and BB. The relationship between them is:

1KB (Kilobyte) = 1024B;1MB (Megabyte, referred to as "megabyte") = 1024KB;1GB (Gigabyte, also known as "Gigabit") = 1024MB;1TB (Terabyte terabyte) = 1024GB;1PB (Petabyte terabytes) = 1024TB;1EB (Exabyte 10 billion bytes) = 1024PB;

  • Tab key automatic completion
    • The command word, options, parameters, file path, software name and service name can be supplemented
[root@server1 ~]# if(tab) (tab)        #Lists commands that begin with if
if         ifcfg      ifconfig   ifdown     ifenslave  ifstat     ifup
[root@server1 ~]# ifco(tab)

[root@server1 ~]# cat /etc/re(tab)(tab)
redhat-release    request-key.conf  request-key.d/    resolv.conf
[root@server1 ~]# cat /etc/re(tab)

[root@server1 ~]# ls /etc/sysconfig/network-scripts/
ifcfg-eth0  ifcfg-privbr0
[root@server1 ~]#ls  /et(tab)/sysco(tab)/netw(tab)- (tab)
  • Shortcut key

    • Ctrl + c: end the running command
    • Esc+. Or Alt +.: Paste the parameters of the previous command
    [root@server1 ~]# ls /etc/hosts
    
    /etc/hosts
    
    [root@server1 ~]# ls -l /etc/hosts
    
    -rw-r--r--. 1 root root 782 Nov 24  2020 /etc/hosts
    
    [root@server1 ~]# cat /etc/hosts
    
    ...
    
    [root@server1 ~]# cat -n /etc/hosts
    
      1 127.0.0.1   localhost localhos...
    
      ...
    
    
    • Ctrl + l: clear the entire screen
    • Ctrl + u: clear from the cursor to the beginning of the line
    • Ctrl + w: delete a word back (delimited by spaces)

Linux virtual machine installation software:

Optical disk image file - > virtual optical drive device - > access point (directory)

2, Mount mount

1. View Linux optical drive devices

[root@server1 ~]# ls -l /dev/cdrom
lrwxrwxrwx. 1 root root 3 Feb  3 22:21 /dev/cdrom -> sr0
[root@server1 ~]# ls -l /dev/sr0
brw-rw----+ 1 root cdrom 11, 0 Feb  3 22:21 /dev/sr0

2. Mount operation

  • Using the mount command

    • Format: mount device path mount point directory
  [root@server1 ~]# mkdir /dvd
  [root@server1 ~]# ls /dvd
  [root@server1 ~]# mount /dev/cdrom /dvd/
  mount: /dev/sr0 Write protected, will mount as read-only
  • Common errors:

    1. The image file of the optical disc is not put into the optical drive device

    2. The optical drive device is not connected

[root@server1 ~ ]# mount  /dev/cdrom  /nsd01
mount: stay /dev/sr0 Media not found on

3. Uninstall operation

[root@server1 ~ ]# umount   /dvd   #uninstall
[root@server1 ~ ]# ls  /dvd/

[root@server1 ~ ]# mkdir  /mydvd
[root@server1 ~ ]# mount   /dev/cdrom   /mydvd
mount: /dev/sr0 Write protected, will mount as read-only
[root@server1 ~ ]# ls   /mydvd

matters needing attention:

1. Uninstall: the current path is the mount point directory
[root@localhost mydvd]# umount /mydvd
umount: /mydvd: target busy.
(in some cases, you can use lsof(8) or fuser(1)
Find useful information about the processes that use this device)
2. Mount allows one device to be mounted to different mount point directories
3. One mount point directory is not allowed to mount multiple different devices
4. It is recommended to create a directory of mount points by yourself

3, Directory and file management

  • Use Wildcards

    • For uncertain document names, it is represented by special characters

      *: any number of arbitrary characters
      ?: Single character

[root@server1 ~]# ls /boot/vm*
/boot/vmlinuz-0-rescue-cf9b054dab8a48c29f233ab8340cb874
/boot/vmlinuz-4.18.0-193.el8.x86_64
[root@server1 ~]# ls /etc/*tab
/etc/anacrontab  /etc/crontab  /etc/crypttab  /etc/fstab  /etc/inittab  /etc/mtab
[root@server1 ~]# ls /etc/??tab
/etc/fstab

[a-z]: one of multiple characters or consecutive ranges. If none, it will be ignored

{a,min,xy}: multiple groups of different strings, all matching

[root@server1 ~]# ls /dev/tty[3-9]
/dev/tty3  /dev/tty4  /dev/tty5  /dev/tty6  /dev/tty7  /dev/tty8  /dev/tty9
[root@server1 ~]# ls /dev/tty[1-7]
/dev/tty1  /dev/tty2  /dev/tty3  /dev/tty4  /dev/tty5  /dev/tty6  /dev/tty7
[root@server1 ~]# ls /dev/tty{1,17,20}
/dev/tty1  /dev/tty17  /dev/tty20
[root@server1 ~]# ls /etc/{cron,fs}tab
/etc/crontab  /etc/fstab
  • Definition of aliases: simplifying complex commands
    • View alias set - alias [alias name]
    • Define a new alias - alias alias name = 'command line actually executed'
    • Cancel set alias - unalias [alias name]
[root@server1 ~]# hostname
server1
[root@server1 ~]# alias hn='hostname'
[root@server1 ~]# alias
alias hn='hostname'
...
[root@server1 ~]# hn
server1
[root@server1 ~]# unalias hn
[root@server1 ~]# hn
bash: hn: Command not found...
  • mkdir — Make Directory

    • Format: mkdir [/ path /] directory name

      [- p]: created with parent directory

[root@server1 ~]# mkdir -p /opt/aa/bb/cc
[root@server1 ~]# ls /opt/aa
bb
  • rm delete with caution!!!

    • rm — Remove
      Format: rm [options]... File or directory
    • Common command options
      -r. - f: recursive deletion (including directory), forced deletion
[root@server1 ~]# rm -rf /opt/aa
[root@server1 ~]# ls /opt/
[root@server1 ~]#
  • mv - Move (cut): the source data will disappear
    • Format: mv original file... Destination path
[root@server1 ~]# touch /opt/b.txt
[root@server1 ~]# ls /opt/
b.txt
[root@server1 ~]# mv /opt/b.txt /opt/c.txt  #Rename: move with unchanged path
[root@server1 ~]# ls /opt/
c.txt
  • cp - Copy: the source data will not disappear

    • Format: cp [options]... Original file... Destination path
    • Common command options
      -r: Recursion, this option is required when copying directories
[root@server1 ~]# cp /etc/passwd /opt/
[root@server1 ~]# ls /opt/
aaa  c.txt  passwd
[root@server1 ~]# cp -r /boot/ /opt/
[root@server1 ~]# ls /opt/
aaa  boot  c.txt  passwd


[root@server1 ~]# \cp -r /boot/ /opt/  #Duplicate names for forced overwrite
[root@server1 ~]# cp -r /home/ /opt/myhome  #Replication can support renaming. The name of the data under the target path
[root@server1 ~]# ls /opt/
aaa  boot  c.txt  myhome  passwd

[root@server1 ~]# mkdir /qwe
[root@server1 ~]# cp -r /home/ /etc/passwd /boot/  /qwe  #Replication can support more than two parameters. Always take the last parameter as the target and all other parameters as the source data
[root@server1 ~]# ls /qwe
boot  home  passwd
  • Filter in the text file, including the line of the specified string

    • grep [options] 'string' text file

    • Common command options
      -v. Reverse match (not included)
      -i. Ignore case
      - ^ word starts with the string word
      – word $ends with the string word

[root@server1 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@server1 ~]# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
...


[root@server1 ~]# grep ROOT /etc/passwd
[root@server1 ~]# grep -i ROOT /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@server1 ~]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@server1 ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
ldapuser0:x:1040:1040::/rhome/ldapuser0:/bin/bash
lisi:x:1041:1041::/home/lisi:/bin/bash

Topics: Linux