Extended use of some common commands in linux

One, grep command

       The Linux grep command is used to filter text and display
       grep can search each line of the specified file according to a specific string. If the string is found, it will output the line containing the content.

Usage example          ^Beginning of line $End of line   

Claim usage
Find the string with the character h in the file grep h file name
Match whole word grep -w word file name
Match lines starting with q grep ^q filename
Match lines ending with q grep q$ file name
Match strings that are not f or g before oo grep [^gf]oo file name
Match lines beginning with # grep ^# file name
Match blank lines grep ^$
If you want to explicitly search for subdirectories greo -r h ./*
Ignore subdirectories grep -d skip h ./*
When querying multiple files, only file names that contain matching characters are output -l
Ignore case when searching grep -i -o -v
When searching, display no matching lines grep -v
Display the matched line and the next 2 lines when searching grep -A 2
Display the matched line and the previous 2 lines when searching grep -B 2
Display the matched line and the upper and lower 2 lines when searching grep -C 2

Exercise

#将/etc/passwd中第一个字段(用户名)截取到user文件中
cut -d:-f 1 /etc/passwd > user

#将3,4字段分别截取出来写入文件UID和文件GID但是要使这两个文件中的信息是以数值由大到小的形式显示
cut -d:-f 3 /etc/passwd | sort -nr > UID
cut -d:-f 4 /etc/passwd | sort -nr > GID

#截取当前日期的年月日显示在文件A.txt中
data | cut -d "" -f 1-3 |tee A.txt

#统计/etc/passwd 一共有多少行但不显示后面的文件名
echo dfhdjflhsfdlgkjhk adjfkhdlfjkhl 'wc -l /etc/passwd | cut -d "" -f 1'

Two, find command

       The linux find command is used to search for files in the specified directory.
       Any string before the parameter will be regarded as the name of the directory to be searched. If you use this command without setting any parameters, the find command will search for subdirectories and files in the current directory. And display all the subdirectories and files found.
       find [path] [options] [expression] find / -name haha

    1. Time-related parameters
parameter Features
-mtime The meaning is the file that has been changed in the day before n days; -mtime 4 refers to the file on the day before 4~5
-mtime -n List file names that have been changed within n days (including n days itself), -mtime -4, file names within 4 days or less
-mtime +n List file names that have been changed n days ago (not including n days itself), -mtime +4, file names greater than or equal to 5 days ago
find /-mtime 0 #0 represents the current time, so files that have changed content from now to 24 hours ago will be listed
find /etc/ -newer /etc/passwd Look for the file under /etc/, if the file date is newer than /etc/passwd, list it
    2. Parameters related to the user or user group name
parameter Features
n -uid n is a number, this number is the user's account ID, that is, UID
-gid n n is a number, this number is the account ID of the user group, namely GID
-user name Find files by file owner
-group name Find files according to their group
-nouser Looking for people whose owner does not exist in /etc/passwd
-nogroup All user groups looking for files do not exist in /etc/group (when you install the software yourself, there may be no file owner in the properties of the software)
    3. Parameters related to file permissions and names
parameter Features
-name filename Find the file named filename, support wildcard * and?
-size [±] SIZE Find files larger (+) or smaller (-) than SIZE. The specifications of this SIZE are: c stands for bytes, k stands for 1024 bytes, -size +50k means to find a file larger than 50kB
-type TYPE Find files according to file type, generally regular file f, device file c, b, directory file d, link file l, socket(s), FIFO(p)
-perm -mode Find files based on file permissions. -perm -0744, when a file permission is 4755, it will also be listed
-perm +mode Find files with file permissions "including the permissions of any mode". -perm +755, when a file permission is -rw------- it will also be listed, because it has rw attributes
find /bin /sbin -perm +6000 Find files with SUID or SGID in the two directories /bin/sbin
    4. Other executable operations
parameter Features
-exec command Command is other commands, and other commands can be followed by -exec to process the found results
-print 将结果打印到屏幕上,这个操作是默认操作
find / -perm +700 -exec ls -l {} ; {}表示"由find找到的内容,find的结果会被放置在{}位置中";-exec一直到";"是关键字,代表额外命令的开始(-exec)到结束(;),在这中间的就是find命令内的额外命令。额外的命令不支持别名

三、文件的压缩与解压缩

      常见的压缩文件扩展名

参数 功能
*.gz gzip程序压缩的文件
*.bz2 bzip2程序压缩的文件
*.tar tar程序打包的数据,并没有经过压缩
*.tar.gz tar程序打包的文件,其中经过gzip的压缩
*.tar.bz2 tar程序打包的文件,其中经过bip2的压缩

      linux上常见的压缩命令就是gzip与bzip2
         1、gzip
            压缩文件:gzip 文件名(源文件不存在)
                             gzip -c 文件名1 > 文件名1.gz(保留源文件)
            解压文件:gzip -d 文件名.gz (不保留源文件)
                             gunzip 文件名.gz
                             gzip -cd 文件名2.gz > 文件名2(保留源文件)
            查看压缩过的文本文件内容:zcat 文件名.gz
         2、bzip2
            压缩文件:bzip2 文件名
                             bzip2 -c 文件名1 >文件名1.bz2(保留源文件)
            解压文件:bzip2 -d 文件名.bz2(不保留源文件)
                             bunzip2 文件名.bz2
                             bzip2 -cd 文件名2.bz2 > 文件名2(保留源文件)
            查看压缩过的文件内容:bzcat 文件名.bz2
         3、tar [主选项+辅选项] 文件或目录 (文件的归档即打包文件)
            主选项:只能出现一个主选项
                    c— create 创建一个新归档文件(打包文件)
                    x— 从归档文件中提取文件出来
                    t— 列出归档文件的内容,查看已经打包了哪些文件,重点在查看文件名
            辅选项
                   z— 通过gzip的支持进行压缩/解压缩,一般格式为*.tar.gz
                   j— 通过bzip的支持进行压缩/解压缩,一般格式为*.tar.bz2
                   J— 通过xz的支持进行压缩/解压缩,一般格式为*.tar.xz
                   v— 归档或解包过程中显示被打包的文件
                   C— 这个参数用于在解压缩时,若要在特定目录解压缩,可以使用这个参数
                   f— 输出结果到文件,必须写该选项
                   --exclude=FILE 在打包的过程中,不要将FILE打包! --排除某个文件打包
            常见的两种压缩方式:jcvf zcvf
                   打包、压缩文件1和2并重命名:tar zcvf 新文件名 文件名1 文件名2 文件名3 文件名4
                   解压:tar xvf 文件名 该命令会自动判断归档文件的压缩格式,自动调用相关程序进行解压缩
                   解压到指定路径:tar xvf 文件名 -C 目标路径
                                               tar -xjv -f filename.tar.bz2 -C 欲解压的目录
                   查看已压缩文件:tar tf 文件名

四、用户身份切换

      su
            su [-lm] [-c 命令] [username]

参数 功能
- 单纯使用-如"su -",代表使用login-shell的变量文件读取方式来登陆系统;若用户没有写,则代表切换为root用户
-l 与-类似,但后面需要加欲切换的用户账号,也是login-shell的方式
-m -m与-p是一样的,表示使用目前的环境变量设置,而不读取新用户的配置文件
-c 仅进行一次命令,所以-c后面可以加上命令
login-shell 取得bash时需要完整的登陆流程。login-shell读取配置文件的顺序是:/etc/profile、/.bash_profile、/.bashrc、/etc/bashrc
non-login shell 取得bash接口的方法不需要重复登录。non-login shell会读取的配置文件为: ~/.bashrc、/etc/bashrc

            ~/.bash_logout文件记录了当我注销bash后系统再帮我做完什么操作后才离开。默认情况下,注销时bash只是帮我们清掉屏幕的信息而已。
      sudo
            sudo是一种以限制配置文件中的命令为基础,在有限时间内给用户使用,并且记录到日志中的命令。sudo命令的配置在/etc/sudoers中,sudo是系统管理员用来允许某些用户以root身份运行部分/全部系统命令的程序
            查看当前是哪个用户登录的shell:whoami
            sudo [-u 新用户账号]
                  -u:后面可以接欲切换的用户,若无此项则代表切换身份为root
            sudu的执行流程如下(默认只有root用户能使用):
                  1、当用户执行sudo时,便会让用户输入自己的密码来确认(root执行sudo时不需要输入密码)
                  2、若欲切换的身份和执行者身份相同,那也不需要输入密码
                  3、若密码输入成功,系统会去让/etc/sudoers文件中查找该用户是否有执行sudo的权限
                  4、若用户具有执行sudo的权限,便开始sudo后续接的命令

Guess you like

Origin blog.csdn.net/Han_V_Qin/article/details/106392323