mycpen

Mycpen

记录学习历程与受益知识
github
telegram
bilibili

04_Linux Basics - .&.. - cat - tac - Redirection - EOF - Basic Commands

I. Review#

Review
//		Copy all files and folders in the /home/ directory to the /jindafu directory
		cp  /home/*  .  -r
# Note: Copying folders requires -r		Recursive copy (because folders are mentioned)
# Note: All	Wildcard *
//		Copy the /etc/hosts file to jindafu without prompting for overwrite
		Use the absolute path of cp
			which  cp	Find the path of cp
			/usr/bin/cp  /etc/hosts  .
# Note: No prompt when entering the absolute path of the cp command	Because cp is an alias, there will be a prompt
//		Rename the xiena directory to xienana
		mv  xiena/  xienana
//		Copy all files in the singer directory to the hejiong directory
		cp  haiquan/singer/*  hejiong/
# Note: All	Wildcard *
# Note: Copying files does not require -r
//		Delete all xienana directories
		Use find command or tree to see where xienana is  rm  -rf  path
		find  /hunantv/  -name  “xienana”  -exec  rm  -rf  {} \;
//		View directory structure tree  Check types file
//		echo  $PS1  $PS2 How to view PS1 PS2
//		hostname  View hostname
//		echo  $SHELL  View default shell  (is bash)
//		cat  /ect/shells  View what shells are available on the machine
		sh  bash  exit
#/etc	Stores configuration files
//		d  directory
		-  regular file
		l  link file
		c  character device file (display)
		b  block device file (disk)
//		reboot  Restart the machine
//		clear  Clear the screen
//		alias alias , unalias , vim  /root/.bashrc
//		env  environment variables  set  all variables


II. . .. Cannot Delete#

//		rm  -rf  will not delete hidden files
		rm  -rf  .lixh  Delete hidden files
		. and .. cannot be deleted

III. cat#

//		ls  View the contents of the folder
        cat 1. View the contents of the file
        	2. Can concatenate multiple files together for output
        -n, --number  Number the output lines
        Use case 2: cat  tangliangfei.txt  wangtc.txt (concatenation function)
        First output the contents of tangliangfei.txt, then output the contents of wangtc.txt

tac and cat#

//		tac displays in reverse order, from the last line to the first line
			cannot use -n
		cat displays in order, from the first line to the last line (with -n option)

IV. Redirection#

//		>  Output redirection, will overwrite the original content, if the file does not exist, it will be created automatically
			Redirection: changes the direction of the output that would be displayed on the screen, outputting to a file
		>>  Append output redirection, will not overwrite the original content, just appends at the end, if the file does not exist, it will be created automatically
		> will overwrite the original content, >> will not overwrite, appends at the end
		# cat  tangliangfei.txt  wangtc.txt  >tang_wang.txt
		# echo 123456789  >>tang_wang.txt 
		(Learn to combine cat with > and >>)

V. here document#

//		Generate a file with specified content --》here document  --》mainly used in scripts to generate specified content
		//  <<EOF defines the ending string
			Ends when EOF is entered, end of file 
# cat   >chenpeng.txt  <<EOF  (Note 1: It doesn't have to be EOF, but it should be meaningful)
> chen peng					(Note 2: 1 > symbol, original content will be overwritten)
> jiangsu
> xinhua
> nongda
> linux
> cali
> EOF (Note 3: Press Enter)

# cat >>cali.txt <<end (Note 1: 2 > symbols, content is appended, not overwritten)
> sanchuang
> nongda
> changsha

VI. Shell Script#

//		Shell script: is actually a file that contains many Linux commands, this file can be executed, and when executed, all commands in the file will be executed
# vim  first_shell.sh
#!/bin/bash (Note 1: Declare that the interpreter used for this script is bash)

mkdir  -p  /test (Note 2: No error if it exists, creates if it does not)

cd /test

mkdir sc{1..100}

touch pengyf{1..100}.txt

cp /etc/hosts  /test

cat >sanchuang.txt  <<EOF (Note 3: After pressing Enter, type directly, no prompt in the text file)
sanchuang   hunan  changsha  furongqu hunannongda
feng de yong
linux
EOF (Note 4: End)

echo "####################################"
cat  -n  sanchuang.txt 
echo "####################################"
Script is written↑↑↑
# bash  first_shell.sh   Execute the script (Note: The content is as follows)
####################################
     1	sanchuang   hunan  changsha  furongqu hunannongda
     2	feng de yong
     3	linux
####################################
//		When the script is executed, it runs from the first command downwards, if a command fails in the middle, the subsequent commands will still execute (in Python, if there is an error in the middle without exception handling, it will not continue executing)

VII. more#

// more command paginates display (Note 1: no -n option, cat has -n option)
		Usage: Full-screen paginated display of file content
Interactive operation methods:
     Press Enter to scroll down line by line
     Press the space bar to scroll down a page, press b to scroll up a page   back
     Press q to exit (Note 2: Automatically exits after display, difference from less)
# more  messages 

VIII. ps aux#

//		# ps aux  View information about currently running processes in Linux --》Task Manager (ps  -aux also works)
        # ps -aux|more
        # cat messages |more is the same as more messages
        # cat -n messages |more Displays line numbers

IX. less#

// less command
	Usage: Same as more command, but with more extended features
	Interactive operation methods:
 	Similar to more command, but some operations may differ
	【page down】【page up】 Scroll up and down (Note 1: more does not support)
	Does not exit after display, press q to exit (Note 2: difference from more)
	(Note 3: Also supports space, b, Enter; anything supported by more is also supported by less)
# less messages 
# cat messages |less

X. head#

// head command (Note 1: Defaults to take the first 10 lines, consecutive lines)
        Usage: View a portion of the beginning of a file (default is 10 lines)
        Format: head -n number filename
        Format: head -number  filename
        cat  -n  passwd  |head  Displays the first 10 lines, with numbering
        head  passwd  Displays the first 10 lines, without numbering
        head  -5  passwd  Displays the first 5 lines (no numbering)
        head  -n  5  passwd  Displays the first 5 lines (no numbering, same effect as the previous command)

XI. tail#

// tail command
        Usage: View a small portion of the end of a file (default is 10 lines)
        Format: tail  -n  number filename
        Format: tail  -number   filename
           tail  -f  filename  = tailf		# Note: Don't know how to use tailf
			# Note: tail  -f dynamically monitors changes at the end of the file
tail  passwd  Get the last 10 lines (no numbering)
cat  -n  passwd  |tail  Get the last 10 lines (with numbering)
cat  -n  passwd  |tail  -5  Get the last 5 lines (with numbering)
cat  -n  passwd  |tail  -1  Last 1 line (with numbering)
cat  -n  passwd  |tail  -n  3  Get the last 3 lines (with numbering)

tail  -n  2  passwd  Displays the last 2 lines
tail  -n  -2  passwd  Same effect as above
tail  -n  +2  passwd  Displays from the 2nd line to the end  (Note 1: Usage: When counting how many lines there are, do not want to display the first line, when counting disk mounts do not want to see the title line)
    (Note 2: df  -Th displays the usage of disk partitions in Linux)
    df  -Th|tail  -n  +2  Count disk mounts without displaying the title line
    df  -Th|tail  -n  +2|wc  -l  Count the number of lines

# tail -f feng.txt   Dynamically monitor changes at the end of the file
(Note 4: Often used to view changes in log files, troubleshooting based on log records)
(Note 2: Reopen the window to connect, modify the file content on the right, dynamically monitor the changes at the end of the file on the left, displaying new content as it appears) (Note 3: Ctrl+C forcibly terminate)
tail  -f extension 
	Specially suitable for viewing certain log files
        /var  Stores dynamically changing files  variable  changeable
        log  Log: is a record of what happens in the program
# cd  /var/log		(Note 1: /var/log stores a large number of log files)
# tail  -f  secure   View the secure log file (Note 2: secure security)
(Note 3: Perform operations in another window, the log file will dynamically update at the end)

XII. sed#

    1. Get the 3rd line of passwd
    # cat -n /etc/passwd|head -3|tail -1 (Note 1: First take the first 3 lines, then take the last line using | pipe)
    # cat  /etc/passwd|head -3|tail -1 (Note 2: Compared to the above command, no numbering)
	2. Get lines 5 to 10 of the passwd file
	# Note: This first looks at the end using head because line numbers are counted from front to back
    # cat -n /etc/passwd|head |tail -6 (Note 3: First head takes the default first 10 lines, then takes the last six lines)
									   (Note 4: From line 5 to 10, tail  -6, not -5)
	# cat -n messages |head -200|tail -101  (100 to 200 lines)
	3. Get the 3rd, 5th, and 10th lines
	# sed  -n  '1p;3p;5p'  passwd (Note 5: -n is an option, only shows lines that meet the requirements, not line numbers)

	Exercise 3 extension
	// sed
		-n effect: only shows lines that meet the requirements (Note 9: Lines that do not meet the requirements are not displayed)
            1p;3p;5p
            1p shows the 1st line  p is the print command
			; Command connector
        # cat  -n  passwd  |sed  -n  ‘1p;3p;5p’ (Note 6: Shows lines 1, 3, 5, with numbering)
        # sed  -n  '1p;3p;5p'  passwd (Note 7: No numbering)
								      (Note 10: Using double quotes "" is also acceptable)
        # cat -n passwd |sed -n  '3p;5p;10p'  Display non-continuous lines 3, 5, 10 (this way shows line numbers)
        # cat -n passwd |sed -n  '3,10p' Display the content from line 3 to line 10, continuous in between

        # cat -n passwd |sed  '3,10p' (Note 8: If sed is not followed by -n, it will display all lines [including those that do not meet the requirements], just that lines 3 to 10 will be output twice, so it must be followed by -n)

Summary: sed  1. Displays continuous  2. Displays non-continuous

XIII. grep#

//		grep text filtering (filters files, not folder directories)
        grep  “root”  /etc/passwd Lines containing root
        grep  “^liang”  /etc/passwd Lines starting with liang
        grep  “bash$  /etc/passwd Lines ending with bash
        Ctrl + c terminate
//		Text processing trio sed  grep  awk

XIV. which#

which  Find executable files and display their locations
		The search range is specified by the PATH environment variable
    # which  mkdir
    /usr/bin/mkdir
    # cd /usr/bin/
    # ll mkdir
    -rwxr-xr-x. 1 root root 195192 Apr  10 02:53 mkdir

    rwxr-xr-x permissions
    r  read   --》cat  vim
    w  write  --》 vim   >>
    x  execute   ---》run

    # ll first_shell.sh
    -rw-r--r--. 1 root root…… (Note 1: Script has no executable permission)
    # bash  first_shell.sh  (Note 2: Use bash to execute it)
    # chmod  +x  first_shell.sh  (Note 3: Give it executable permission)
    -rwxr-xr-x.  root root……  (Note 4: Now has executable permission)
    # ./first_shell.sh  (Note 5: Execute directly, run in the current terminal, executed by the current bash)
    # /lianxi/first_shell.sh  (Note 6: Execute directly with absolute path)
    # chmod  -x  first_shell.sh  (Note 7: Cancel executable permission)
    # /lianxi/first_shell.sh 
    -bash: /lianxi/first_shell.sh: Permission denied  (Note 8: Cannot execute after canceling executable permission)

whereis#

//		whereis Find where the command is placed (Note 1: Similar to which) (Note 2: Not very useful, where the man manual is not very important)
		# whereis  mkdir
		mkdir:  /usr/bin/mkdir  /usr/share/man/man1/mkdir.1.gz
								(Note 2: Compressed file, is its documentation)
        1. View command placement path
        2. The path of the command's man manual (where the content viewed during man is stored)
// whereis and which differences
        Which does not display the man manual path, whereis displays the man manual path
        # whereis  mkdir
        mkdir:  /usr/bin/mkdir  /usr/share/man/man1/mkdir.1.gz
        # which  mkdir
        /usr/bin/mkdir
// whereis and which both search in the PATH variable (common point)

XV. PATH#

//PATH is the path
The which command searches for commands in the paths specified by the PATH variable
# which fengdeyong
/usr/bin/which: no fengdeyong in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
(Note 1: Red is the path  Note 2: Multiple folders are separated by colons)
# echo $PATH  (Note 3: $PATH references the PATH variable)
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
The search order is from left to right, if found in the first one, it will not look further, and so on

In-depth understanding of the role of the PATH variable#

  1. Write a file named schello.c
# vim schello.c (Note 1: Write the script as follows)
#include<stdio.h>
int main(){
	printf("hello,world\n"); (Note 2: \n new line)
	printf("i am cali\n");
	return 0;
}
# file schello.c 
schello.c: C source, ASCII text (Note 3: C language file, text file, not recognized by the machine)
  1. Compile into a binary program file

gcc is a compiler in Linux that compiles C source files into binary files

image-20220313140253260

gcc is a compiler in Linux that compiles C source files into binary files

# yum  install  gcc -y
# gcc  -o  schello  schello.c  (Note 1: -o outputs a binary file) (Note 2: Command to compile schello.c)
# ls		(Note 3: schello generated file, schello original file)
schello  schello.c (Note 2: Generates a green executable file schello)
# ll
Total 20
-rwxr-xr-x. 1 root root 12744 Sep  18 15:12 schello
-rw-r--r--. 1 root root    93 Sep  18 15:08 schello.c
# ./schello        (Note 4: ./ run)
# /lianxi/sc/schello  (Note 5: Run with absolute path, both ./ and absolute path can run)
# schello cannot run (Note 6: Linux does not know where schello is, it will only look for it in the PATH variable)
-bash: schello: command not found (Note 7: The PATH variable does not include the current path, does not include /lianxi/sc)
(Solve as follows)

How to let the Linux system know where our commands are?#

1. Adapt the PATH variable by copying our command to a path already in the PATH variable
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# cp  schello  /usr/local/bin/
# schello (Note 1: Successfully run)
hello,world
i am cali
# which schello
/usr/local/bin/schello (Note 2: After copying to the path in the PATH variable, bash can find schello)
2. Modify the PATH variable to add the path where our command is located
 Temporarily modify the PATH variable
# PATH=/lianxi/sc:$PATH   Temporarily modify the PATH variable
(Note 3: First reference the value of the PATH variable, then concatenate it with /lianxi/sc: string and assign it to the PATH variable)
(Note 3: $PATH——》/lianxi/sc:+$PATH re-combining——》assigning to the PATH variable from right to left)
# echo $PATH
/lianxi/sc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
(Note 4: The new PATH variable, with /lianxi/sc: added at the front)
[root@sanchuang-linux sc]# which schello (Note 5: which can find it)
 /lianxi/sc/schello
# schello (Note 6: bash can run)
hello,world
 Permanently effective
Modify the file to add the PATH variable to make it permanently effective
# vim  /root/.bashrc  
	.bashrc file is executed when the Linux system starts or the user logs in, so it is permanently effective
PATH=/lianxi/sc:$PATH (Note 1: Type it in, place it at the last line)
//		mkdir written in C

//		# yum  install  python3  -y   Install python3 software
        # python3 (Note 1: Enter Python3 interpreter)
        >>> a = 10 (Note 2: Prompt >>>)
        >>> b = 20 (Note 4: Python does not need to compile, Python code runs directly, interpreted by Python interpreter)
        >>> sg = "tangliangfei"
        >>> c = a  + b
        >>> c
        30
        >>> print(sg) (Note 3: Output)
        tangliangfei
        >>> exit() (Note 5: exit() exits, exit() is the exit function)
// Extension
.py files are Python program files
.sh files are shell scripts
.c files are C language files
# vim  sc_py_hello.py
#!/usr/bin/python3 (Note 1: which python3——》/usr/bin/python3)
				   (Note 2: Declare that the script is a Python script, commands inside are interpreted by Python3 interpreter)
username = input("please input your name:") (Note 3: The text in quotes is just a prompt, indicating what to input)
				   (Note 4: The input() function will receive your input and assign it to username)
age = input("please input your age:")

print(f"your name is {username} ,your age is {age}") (Note 5: print(f ) syntax)
				   (Note 6: {username}{age} references the values of the variables)

# ls
schello  schello.c  sc_py_hello.py
# python3  sc_py_hello.py (Note 7: Directly execute Python3, similar to bash  a.sh)
please input your name:cali
please input your age:18
your name is cali ,your age is 18
[root@sanchuang-linux sc]# cat sc_py_hello.py 

/lianxi/sc/sc_py_hello.py (Note 1: Cannot run directly, as below permission denied, use pwd to check the path)#

-bash: /lianxi/sc/sc_py_hello.py: Permission denied

chmod +x sc_py_hello.py (Note 2: Give it executable permission)#

/lianxi/sc/sc_py_hello.py (Note 3: Now can run directly)#

please input your name
please input your age:18
your name is wangtc ,your age is 18

// How to let sc_py_hello.py run in the PATH variable
# which sc_py_hello.py (Note 1: Can run because the path has been added to the PATH variable (/lianxi/sc) before)
# sc_py_hello.py (Note 2: Can run because the path has been added to the PATH variable (/lianxi/sc) before)
# echo $PATH
/lianxi/sc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


```bash
  Summary: It doesn't matter what language it is written in
		1. Place it in the path where the PATH variable is located, and 2. Give it executable permission to run directly
    # chmod  -x  sc_py_hello.py (Note 3: Cancel executable permission)
    # sc_py_hello.py  (Note 4: Cannot run without executable permission, even if the path is in the PATH variable)
	-bash:  /lianxi/sc/sc_py_hello.py: Permission denied
  Summary: For Python programs, it is essential to 1. Give it executable permission, and 2. Place it in the path where the PATH variable is located, otherwise it cannot be executed
    // Extension  (Note 2: Summary: If the file name is long, consider using an alias [shortcut key])
    # alias sc_py_hello=sc_py_hello.py   Define an alias to shorten the command length
    # sc_py_hello (Note 1: If you don't want to type so long, you can use an alias, which can be used to be lazy)
    please input your name:feng (Run successfully, provided it is in the PATH variable path and has executable permission, as above)
    please input your age:19
    your name is feng ,your age is 19
    # which sc_py_hello
    alias sc_py_hello='sc_py_hello.py'
		/lianxi/sc/sc_py_hello.py

XVI. bash#

//bash is a very good shell parser in Linux (Note 1: Two things to do)
		1. Find commands --》In the path of the PATH variable
		2. Tell the kernel to start the command (run this command)

XVII. /usr#

// /usr stores Linux program directories   unix  system  resource 
    /usr/bin stores binary programs, all users can use  binary
    /usr/sbin   stores binary programs, programs executed by users with higher permissions (root)  super user binary
        # which  useradd (Note 1: Find the path of the useradd command)
        /usr/sbin/useradd (Note 2: Stored in the sbin directory)
        # su  -  xulilin (Note 3: Switch to a regular user)
        $ which  useradd
        $ /usr/sbin/useradd
        $ useradd  xu123 (Note 4: Regular users cannot execute commands in sbin/)
        ……Error useradd:Permission denied (Note 5: Permission denied)

XVIII. locate#

//		locate knowledge point (Note 4: Needs to be installed) (Note 5: Often needs to be updated # updatedb)
# yum provides locate -y  Query which software package provides locate (Note 1: New knowledge point)
mlocate-0.26-20.el8.x86_64 : An utility for finding files by name
(Note 3: yum provides …… -y queries which command is provided by which software package)
(Note 2: From the result, we know that mlocate provides the locate command)
# yum install  mlocate -y Install mlocate software
locate command
Format: locate filename
(Note 5: locate searches its own database, can be specific to /, and must update its own database)
Searches based on the daily updated database (/var/lib/mlocate), speed is fast
	/var/lib/mlocate/mlocate.db
	database  database (Note 6: Explanation of db)
The content in the database is generated based on the content under the /
The database is automatically updated every day	/var/lib/mlocate/mlocate.db
Manually update the database updatedb
# updatedb   Generate and update the database mlocate.db (Note 1: Manual update)
# cd /var/lib/mlocate/
# ls
mlocate.db
# locate mkdir (Note 2: Finds all files in the Linux system whose names contain mkdir)
 /usr/bin/mkdir
 /usr/lib/python3.6/site-packages/pip/_vendor/lockfile/mkdirlockfile.py
 /usr/lib/python3.6/site-packages/pip/_vendor/lockfile/__pycache__/mkdirlockfile.cpython-36.opt-1.pyc
 /usr/lib/python3.6/site-packages/pip/_vendor/lockfile/__pycache__/mkdirlockfile.cpython-36.pyc
 /usr/share/man/man1/mkdir.1.gz
# which mkdir (Note 3: Only finds /usr's mkdir, does not find mkdir elsewhere)
 /usr/bin/mkdir
The range of locate search:
	/--》Search in the mlocate.db (Note 2: Search in the root's own database mlocate.db)
	Method: Fuzzy search, file names just need to contain it
	Disadvantage: New files may not be found sometimes, because the database has not been updated   --》updatedb (Note 3: Manual update)
	Can search for any file (Note 4: New files, # updatedb manually update the database, otherwise cannot be found)
which search range:
	Only in the PATH variable
	Method: Exact search (Note 1: File names must be exactly the same)
	Can only search for commands

which, whereis, locate, find#

//		which, whereis, locate, find command search types
        Which, whereis can only find commands
        locate can also find ordinary files, can find any files

XIX. find#

//* find command

​ Format: find [search range] [search condition] [action]

​ find can find 1. commands, 2. files (i.e., any files)

​ (Note 1: Integrates all functions of which, whereis, locate)

​ find command

​ Usage: Used to find files or directories

​ Format: find [search range] [search condition] [action]

​ Common search conditions

​ ==-name==: Search by file name

​ ==-iname==: Search by file name, case insensitive

​ ==-size==: Search by file size

​ -user: Search by file owner

​ ==-type==: Search by file type

​ -perm : Search by file permissions

​ ==-mtime== : Search by file modification time

​ -newer: Search for files newer than a specific file

-name and -iname
    # find /lianxi  -name "lixh.txt" (Note 1: Exact search, -name searches by file name)
    /lianxi/lixh.txt
    # find  / -name  "schello" (Note 2: Exact search, can search under /)
    /lianxi/sc/schello
    # find  / -name  "schello*" (Note 3: * fuzzy search, can search under /)
    /lianxi/sc/schello.c
    /lianxi/sc/schello
    # find  / -name  "li*.txt" (Note 4: * fuzzy search)
    # find  / -iname  "lixh.txt" (Note 5: -iname searches for file names case insensitive)
    /lianxi/lixh.txt
    /lianxi/LIXH.TXT
-size (size)
    # ll  -h  View the size of each file in the folder
    # du  -a View the size of each file in the folder (unit KB)
     du  -ah  View the size of each file in the folder (unit K, M, G)
    # find  /boot  -size  +1M (Note 1: +1M greater than 1M, find all) (Note 2: -1M, 1M: less than 1M, equal to 1M)
    /boot/grub2/fonts/unicode.pf2  (Note 3: The search result is not particularly precise)
    /boot/initramfs-4.18.0-193.el8.x86_64kdump.img
    /boot/initramfs-4.18.0-193.el8.x86_64.img
-type (type)
    # find  .  -type  d (Note 1: Find directories)
    # find  .  -type  f (Note 2: Find regular files)
    # find  .  -type  l (Note 3: Find link files)
    # find  .  -type  c (Note 4: Find character device files)
    # find  .  -type  b (Note 5: Find block device files)
    # find  .  -type  p (Note 6: Find pipe files)
    # find  .  -type  s (Note 7: Find socket files) (files for inter-process communication)
-mtime (in 24-hour units) Search by file modification time
-mmin  (in minute units)
    # find  .  -time  +1  More than 1 day ago
    # find  .  -time  -1  Within 1 day
    # find  .  -mmin  -30  Within 30 minutes
    # find  .  -mmin  +30  More than 30 minutes ago
-user (which user created the file) (not very useful)
    # find  .  -user  root
    # find  /  -user  cali
-newer (files newer than the specified file, files created later)
	# find  .  -newer  feng.txt
//		Special search conditions
-o : Logical OR, as long as one of the given conditions is satisfied, the search condition is considered satisfied or
-not : Logical NOT, can be represented in the command by “!” This operator indicates to find files that do not meet the given conditions
-a: Logical AND, the system defaults to AND, can be omitted, indicating that only when all given conditions are satisfied, the search condition is considered satisfied

  find  /boot -size +1024k  -a -name “vmlinuz* (Note 1: Logical AND -a can be omitted, the system defaults to logical AND)
  find  /boot -size +1024k  -o -name “vmlinuz*
  find  .  -user nie  -type f   -size  +1k  -o  -name  "vmlinuz*" 
  find  .  -user nie  -type f  \( -size +1k -o -name "vmlinuz*" \)  (Note 3: () changes the priority, \ explanation below) (Note 5: \(  \) means () change the priority)
  find   /home  !-user  hello  -exec  ls  -ld  {}  \;
\  Escape character (Note 2: Because parentheses () have special effects, connecting after / makes it represent only parentheses)
By default, -a has a higher priority, executing logical AND (-a) first, then executing logical OR (-o)
# find  / -iname "*.conf"  -size +1k  -type f  -user root -o -size +10M
(Note 4: The front part forms a condition, either satisfying the condition before -o or satisfying the condition after -o)
//	Extension, following the command above -exec and -ok
	  # find  /lianxi  -name  "li*.txt"  -exec  rm -rf  {}  \; (Note 1: The front part is the search, -exec indicates the action to be executed) Related explanation below
    -exec  Execute the command that follows
    rm -rf  Specific delete command
    {}  Represents the content found by the previous find, understood as a container storing the content found by the previous find
    \; Indicates the end of the find command
    # find /lianxi -name "*.txt"  -size +1k  -type f  -exec cp {} /fengdeyong \;
    (Note 2: Files ending with .txt, larger than 1k, files, copy to fengdeyong)
    (Note 3: find looks under /lianxi at every level)
    -ok
    -ok lets you confirm before executing
    # find . -name "*wang*" -ok rm -rf {} \;
    < rm ... ./wangtc.txt > ? y
    < rm ... ./tang_wang.txt > ? y

Summary: -exec executes the command without confirmation (commonly used in scripts for convenience)
	 -ok executes the command and requires confirmation
-maxdepth  When searching for files, the depth of the directory
    # find  .  -name  bb
    ./aa/bb
    ./aa/bb/bb
    ./bb
    # find  .  -maxdepth  1  -name  bb (Note 2: Only dig 1 level)
    ./bb
    # find  .  -maxdepth  2  -name  bb (Note 3: Only dig 2 levels)
    ./aa/bb
    ./bb
    # find  .  -maxdepth  3  -name  bb (Note 4: Only dig 3 levels)
    ./aa/bb
    ./aa/bb/bb
    ./bb
    (Note 1: If not followed by -maxdepth, it will dig as many layers as there are)
    # find /lianxi  -name  "*.txt"
    (Note 6: If not followed by -maxdepth, too many things will be dug too deep)
    /lianxi/tangliangfei.txt
    /lianxi/chenpeng.txt
    /lianxi/cali.txt
    /lianxi/feng.txt
    /lianxi/aa/bb/feng.txt
    /lianxi/aa/feng.txt
    # find /lianxi -maxdepth 1 -name  "*.txt"
    (Note 5: This situation is often used, only want to find 1 layer, do not want to see subfolders and sub-subfolders)
    /lianxi/tangliangfei.txt (Note 7: Display as follows)
    /lianxi/chenpeng.txt
    /lianxi/cali.txt
    /lianxi/feng.txt
// Using negation, following the command above (Note 1: Not *.txt under lianxi)
    # find  /lianxi  -maxdepth  1  !  -name  "*.txt" (Only negates the condition that follows)
    /lianxi
    /lianxi/passwd
    /lianxi/first_shell.sh
    /lianxi/messages
    /lianxi/sc
    /lianxi/LIXH.TXT
    /lianxi/aa
    /lianxi/bb
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.