Network Security Day7-Linux Packaging and File Search

1. Pack and compress

  1. Why pack and compress
    1. Put a bunch of files together and move them easily
    2. Save space after compression, text saves 2/3
    3. Implement the backup function
  2. Packing and compression tools: tar, gzip, unzip, zip

1.1. tarPacking and compression (generally use this extension .tar.gz)

1.1.1 tar packing

  1. Packaging combination: zcvf
    letter meaning
    z compression
    c rebuild file
    v display output
    f show file compression
  2. grammar:tar zcvf 压缩包的名字 要打包的内容
  3. Example: pack the files in the ect directory into etc.tar.gz in the current directory (create if not)tar zcvf etc.tar.gz /etc/
  4. Details: After packaging, the symbol of the root of the directory will be removed /etc( etcif not, the source file that may be updated will be replaced when decompressing)

1.1.2. tar unpacking

  1. combination of options
    letter meaning
    z compression
    x unpack
    v display output
    f show file compression
  2. Unpack to the current directory:tar zxvf 包名
  3. Unpack to the specified directory:tar zxvf 包名 -C 指定目录
  4. Common methods when unpacking
    1. Do not unpack to see the content:tar tf 包
    2. Let the packaged path take time (backup):
      1. 反引号and $()you can put commands inside
      2. method
        [root@localhost ~]# tar zcvf etc_$(date +%F).tar.gz /etc/
      3. result
        [root@localhost ~] ls
        anaconda-ks.cfg        etc.tar.gz  oldboy.txt
        etc_2023-07-21.tar.gz  m1          test
        

1.2 gzipPackage compression (delete the original file after compression and decompression)

  1. Pack into the current directory:gzip a.txt
  2. View the contents of the package:zcat a.txt.gz
  3. Pack and compress multiple files:gzip -r oldboy
  4. Unzip:gzip -d a.txt.gz

1.3 zipDecompression

  1. Uses: Unzip the Windows zip file to the current directory
  2. grammar:unzip -o oldboy.zip

2. Find

2.1 whichFind binary commands

  1. grammar:which 命令
  2. Features: fast
  3. Principle: Search from the path inside $PATH
[root@localhost ~] echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  1. expand
    1. Principle of Executing Commands
      1. Check whether the command exists in the directory in $PATH, if not, an error will be reported
      2. Execute the command if there is one, and the command is parsed by the bash interpreter
      3. After parsing, you can see the displayed results
    2. add command
      1. Add commands under ~
        1. cd ~
        2. vim fff: Press Enter to edit and add content echo "i am oldboy", and then save.
        3. chmod +x fff
        4. ./fff: can output the result
        5. fff:no result
      2. Put the command in the path inside the PATH variable
        1. cp fff /usr/bin/
        2. fff: can output the result
        3. rm -f /usr/bin/fff
        4. fff:no result
      3. Put the script path into the PATH variable
        1. export PATH="$PATH:~"
        2. fff: Find the fff in ~, so you can output the result

2.2 findcommand to find files

  • Features: traversal search, slow
  • grammar:find 路径 选项1 选项2

2.2.1 -nameSearch by name

  • Full name lookup:find 限定文件路径 -name "hosts"
  • Fuzzy lookup:find 限定文件路径 -name "h*ts"
  • Find by extension:find 限定文件路径 -name "*.txt"

2.2.2 -typeSearch by type

  • grammar:find 路径 -type 参数
parameter English effect
b block block device, hard drive
c character character device, serial device
d directory Table of contents
f regular file document
l symbolic link soft link, shortcut
s socket socket file
  • practise
    • Find files under ~
      [root@localhost ~] find ~ -type f
      /root/.bash_logout
      /root/.bash_profile
      /root/.cshrc
      
    • Find the directory under ~
      [root@oldboyedu ~] find ~ -type d
      /root
      /root/oldboy
      
    • Find the files under ~, and the end of txt
      [root@oldboyedu ~] find ~ -type f -name "*.txt"
      /root/a.txt
      

2.2.3 -sizeSearch files by size

  • files larger than a certain sizefind 文件 -size +大小
  • files equal to a certain sizefind 文件 -size 大小
  • files smaller than a certain sizefind 文件 -size -大小
  • unit
size unit symbol unit meaning
c character
w word
b byte
k gigabytes
M megabyte
G G

2.2.4 -mtimeSearch by modification time

  • n days agofind -mtime +n
  • n daysfind -mtime n
  • last seven daysfind -mtime -n

2.2.5 Other parameters

  • find 文件 -user 用户名
  • find 文件 -perm 数字: related to permissions

2.3 Combination of find command and other commands

2.3.1 find processes the found things (modify, delete)

  1. Method 1: Execute the action-exec
    1. grammar:find 路径 -mtime +7 -exec rm -f {} \;
      1. {}: represents the content found by find
    2. Features: low efficiency
    3. Principle: equivalent to deleting one by one, the efficiency is low
      find找到文件以后
      rm -f /data/file_2023-07-01
      rm -f /data/file_2023-07-02
      .....
      rm -f /data/file_2023-07-22
      
  2. Method 2: find+xargs+rm combination delete
    1. grammar:find /data -type f -mtime -7|xargs rm -f
    2. Principle: equivalent to a rm to delete all found files
      [root@oldboyedu ~] find /data -type f -mtime -7|xargs rm -f
      rm -f /data/file_2023-07-24 /data/file_2023-07-25 /data/file_2023-07-26 /data/file_2023-07-27 /data/file_2023-07-28 /data/file_2023-07-29 /data/file_2023-07-30
      

2.3.2 find+cp file copy find+mv similarly move files

  • Method 1 (use curly braces to move the position of the found file) (xargs -i is to put the found content in curly braces)
    find /data -name "file*" -mtime +5|xargs -i cp {} /tmp
  • Method 2 (use cp parameters to adjust cp syntax source and target position sequence)
    find /data/ -type f -mtime +3|xargs cp -t /tmp

2.3.3 Pack and compress the things found by find

  • Find and package all files starting with ip under /sbin
    find /sbin/ -type f -name "ip*"|xargs tar zcvf ip.tar.gz

2.3.4 Use find to find the file containing a certain string

  • find / -type f -mtime -7|xargs grep "oldboy"

2.3.5 Logical operators of find

  • And and -a defaults to the fact that -a is omitted in the middle of multiple conditions.
  • Or or -o writing:find /etc -type f -o -mtime -7
  • Non! Writing:find /etc ! -type f -o !-mtime -7

Guess you like

Origin blog.csdn.net/m0_73293867/article/details/131859155