Learn basic Linux command - file compression and packaging search command (four)

A rookie to learn programming techniques, record and share knowledge to everyone, hope you can enjoy.

Today to write an article on the basis of learning Linux command - file packages Bowen compression and search commands, but also a lot of practice, summed up for everyone to learn together!

一、tar命令

Packaging for the file compression or decompression; Format: tar [options] archive file list

  • -c: Generate archive
  • -v: Set out in detail the process of file archiving solution
  • -f: Specify archive file name
  • -r: Append files to the end of archive
  • -z: Compressed or uncompressed file format with gzip
  • -j: Compressed or uncompressed file format with bzip
  • -t: View content
  • -x: Unlock the archive
    example:
tar -cvf /tmp/etc.tar /etc  #将/etc目录下的文件导报到/tmp目录下并命名为etc.tar,仅打包,不压缩
tar -zcvf /tmp/etc.tar.gz /etc #/etc目录下的文件打包到/tmp目录下并命名为etc.tar.gz,打包后以gzip的格式进行压缩
tar -jcvf /tmp/etc.tar.bz2 /etc ##/etc目录下的文件打包到/tmp目录下并命名为etc.tar.bz2,打包后以bzip的格式进行压缩

题目:

1、将/tmp/etc.tar.gz文件解压缩在/usr/local/src下
cd /usr/local/src
tar -zcvf /tmp/etc.tar.gz

二、搜索命令

Find files generally have the following command:

  • which: View the location of the executable file
  • whereis: View the location of the executable file and related documents
  • locate: With database cache, to quickly view the file location
  • find: Find relevant documents
  • grep: Filter match, it is a file search tool

find命令

Used to find files in the specified directory;

  • -name: Search by file name
  • -size n: Search by file size
  • -perm: Find files by permission
  • -user: According to the owner of the file to find the file
  • -group: According to the group the file belongs to locate the file
  • -mtime n: Find modify the contents of the file n days
  • -mmin n: Find modify the contents of the file within n minutes
  • -type: Find a certain type of file
-type  查找某一类型的文件
b - 块设备文件
d - 目录
c - 字符设备文件
p - 管道文件
l - 符号链接文件
f - 普通文件

Example:

find . -size +9M | xargs ls -lh  #查找当前目录下大于9M的文件详细信息
 find . -type f -name "*.log" -size +1M -exec cp -av {} /tmp \;  #查找当前目录下以 .log 结尾且大于5M的文件,并复制到/tmp目录下
find /var -mtime +3 -mtime -5 :在/var下查找更改时间在三天到五天的文件
find . -mmin +1 -mmin -3 :查找当前文件夹下1分钟前3分钟内修改的文件

grep命令

; For performing a keyword search in the text, and display the results match, then the use of the positive expression associated with
the string grep [parameter] is to be found: Format

  • -v: Adverse selection, not only lists the line keywords
  • -c: The number of rows displayed only found
  • -i:not case sensitive
  • -b: The executable file (binary) as a text file (text) search
  • -n: Each row only matching row number displayed in the opposite
  • ^linux: Line beginning with linux
  • $php: At the end of the line php
  • .: Matches any single character
  • ^$:Blank line
  • .+: Match any number of characters
  • .*: Matches zero or more characters (optional)
  • [0-9a-z]: Match any character within the brackets
  • [abc]: That matches one character, this character must be abc one.
  • (linux)+: Linux word appears more than once
  • (web){2}: Web appear more than twice
  • \: Escape shield
  • |: Or the meaning of

Example:
grep common operations:

grep -n 'root' /etc/passwd  #查找/etc/passwd下包含 root字符串的文件
grep -Ev "root|nologin" /etc/passwd  #查找不包含root和nologin关键字的行
grep "root" /etc/{passwd,shadow}  #查找/etc/passwd和/etc/shadow文件中包含root关键字的行
echo "a bc de" |xargs -n1 |grep '^b' :匹配以字符串"b"开头的行
grep -c root /etc/passwd   #统计/etc/passwd文件中包含root字符串行的数量
grep -E -v "^$|^#" /etc/nginx/nginx.conf   #去除空号和以#号开头的行
grep -r 'sshd' /etc --include *.conf :递归搜索/etc 目录下包含 "sshd"字符串 的 conf 后缀文件

题目

1、在/etc/passwd文件中找到root用户的信息,并显示行号
grep -n root /etc/passwd    #不进行筛选,列出与root相关的用户信息
grep -n ^root /etc/passwd   #筛选出以root开头的行
2、查看passwd中包括nologin或root的数据
grep -n “root\|nologin” /etc/passwd 
3、在/etc/passwd文件中找到非nologin用户的信息,并显示行号
grep -nv “root\|nologin” /etc/passwd    #取反操作
4、找出/etc/passwd文件中同时含有root和nologin关键字的用户信息
grep -n “root|nologin” /etc/passwd   

四、重定向和管道

And redirection operator:

  • 输入重定向: The specified file into command, rather than through the keyboard input
  • 输出重定向: Save the normal output command to the specified file, instead of directly on the display screen of the display
  • Redirected output using the ">", ">>" (> direction is the direction of data flow)

grammar:

> 文件名 	`#表示将标准输出的内容,写到后面的文件中,如果此文件名已经存在,将会覆盖原文件中的内容`
>> 文件名  `#表示将标准输出的内容,追加到后面的文件中。若重定向的输出的文件不存在,则会新建该文件`

题目

1、查看当前主机的cpu的类型保存到cpu.txt文件中(而不是直接显示到屏幕上)
cat /proc/cpuinfo >cpu.txt
find cpu*
cat cpu
2、将内核的版本信息追加到cpu.txt
uname -a>>cpu.txt
3、清空cpu.txt文件
>cpu.txt

管道符命令

作用: The contents of the left as input, so that the right to accept and process the command, you can connect multiple commands using the
left side of the results as input values on the right
题目:

1、将磁盘使用的信息写入disk.txt文件的同时输出到屏幕上(磁盘使用信息:df -h)
df -h| tee disk.txt
cat disk.txt
多重管道:
2、查看sshd进程有没有被启用?
ps -aux| grep “sshd”| grep -v “grep”
Published 14 original articles · won praise 95 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44723773/article/details/105220341