[Linux] Linux basic operation commands

1. Commonly used symbols in Linux

* represents any string
represents any character
/ Represents the root directory or is used as a path separator
\ Escape characters.
<ENTER> Line continuation character. You can use line continuation characters to write a command line on multiple lines.
$ Variable value replacement, such as: $PATH represents the value of the environment variable PATH
Characters between '...' will be treated as ordinary characters
‘’ Characters between ''...'' will be treated as text and variable value substitution is allowed.
` Command substitution, replacing the execution result of the command in `…`
< Enter redirection characters
> Output redirection characters
| pipe character
& Background execution characters. Add the character "&" after a command, and the command will be executed in the background.
; Multiple commands executed in sequence
() Execute command in subshell
{} Execute command in current shell
! Execute commands in command history
~ Represents the logged-in user's home directory (home directory)

2. Historical records

The Linux system retains the history of every command typed by the user in the shell, and provides many methods for users to find commands that have been used through the history, and call the commands in this history.

[root@node01 ~]# history
    1  ifconfig
    2  nmcli connection modify ens33 ipv4.addresses 192.168.2.220/24 ipv4.gateway 192.168.2.1 ipv4.method manual autoconnect yes
    3  route -n
    4  nmcli connection down ens33
   .
   .
   .
grammar replace
!! previous command
!n Command number n
!-n The nth command from the last
!cmd The last command used to start cmd

Files and variables related to history

[root@node01 ~]# echo $HISTFILE
/root/.bash_history
#用户的历史记录保存的位置
[root@node01 ~]# echo $HISTFILESIZE
1000
#启动时,从历史记录中读取的记录条数
[root@node01 ~]# echo $HISTSIZE
1000
#退出时,被写入历史记录的最大条数

History Tips

esc+./alt+.	调用上一条命令的最后一部分内容
ctrl+r	在历史记录中搜索给出关键字的命令

3. Standard input, standard output, standard error

In a Linux system, most of the time we read input from the keyboard and display the output on the terminal. Most of what we input on the keyboard is to execute commands. These commands belong to terminal programs. In addition to terminal programs, there are also graphics programs and screens. Programs (such as vim), no matter what kind of program they are, will involve input, output, and errors. In most cases, we enter information on the keyboard and view information on the monitor (correct information and incorrect information). These input information we It is called standard input (can be represented by 0), the output information is called standard output (can be represented by 1), and the error information (can be represented by 2) is called standard error. In daily use, in addition to using the keyboard to input information and reading information from the display, we can also specify the program to read the content that needs to be input from places other than the keyboard, or have the program output information to places other than the display.

Redirect input and output

#重定向输出
[root@node01 ~]# mkdir a
[root@node01 ~]# cd a
[root@node01 a]# mkdir aa ab ac
[root@node01 a]# cd aa
[root@node01 aa]# touch bb bc bd
[root@node01 ~]# ls a/ > test	
[root@node01 ~]# cat test
aa
ab
ac
[root@node01 ~]# ls a/aa/ >> test
[root@node01 ~]# cat test
aa
ab
ac
bb
bc
bd

>覆盖
>>追加
#重定向输入
[root@node01 ~]# wc -l test
6 hello
[root@node01 ~]# wc -l < test 
6
注意:第一个例子,会输出文件名;第二个不会,因为它仅仅知道从标准输入读取内容。

We know that standard input can be represented by 0, standard output can be represented by 1, and standard error can be represented by 2. Sometimes the output information contains both correct and incorrect information, such as:

[root@node01 ~]# ls /etc/rc.d/
init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local
[root@node01 ~]# head -1 /etc/rc.d/*
==> /etc/rc.d/init.d <==
head: 读取'/etc/rc.d/init.d' 时出错: 是一个目录

==> /etc/rc.d/rc0.d <==
head: 读取'/etc/rc.d/rc0.d' 时出错: 是一个目录

==> /etc/rc.d/rc1.d <==
head: 读取'/etc/rc.d/rc1.d' 时出错: 是一个目录

==> /etc/rc.d/rc2.d <==
head: 读取'/etc/rc.d/rc2.d' 时出错: 是一个目录

==> /etc/rc.d/rc3.d <==
head: 读取'/etc/rc.d/rc3.d' 时出错: 是一个目录

==> /etc/rc.d/rc4.d <==
head: 读取'/etc/rc.d/rc4.d' 时出错: 是一个目录

==> /etc/rc.d/rc5.d <==
head: 读取'/etc/rc.d/rc5.d' 时出错: 是一个目录

==> /etc/rc.d/rc6.d <==
head: 读取'/etc/rc.d/rc6.d' 时出错: 是一个目录

==> /etc/rc.d/rc.local <==
#!/bin/bash

When we use the ls command to view the /etc/rc.d directory, we find that there are both directories and files in this directory, and when we use the "head -1" command to view the first line of the file, Obviously, the first line cannot be viewed in the directory, and an error will be reported. When I want to write all this information to a specified file without seeing the content, what should I do?

[root@node01 ~]# head -1 /etc/rc.d/* > test
head: 读取'/etc/rc.d/init.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc0.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc1.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc2.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc3.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc4.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc5.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc6.d' 时出错: 是一个目录
[root@node01 ~]# cat test
==> /etc/rc.d/init.d <==
==> /etc/rc.d/rc0.d <==
==> /etc/rc.d/rc1.d <==
==> /etc/rc.d/rc2.d <==
==> /etc/rc.d/rc3.d <==
==> /etc/rc.d/rc4.d <==
==> /etc/rc.d/rc5.d <==
==> /etc/rc.d/rc6.d <==
==> /etc/rc.d/rc.local <==
#!/bin/bash
```powershell

我们可以利用之前的>将输出的信息重定向到一个指定的文件,但是仍然会收到错误提示,这是为什么呢?因为在linux当中正确的输出和错误的输出实际上是两种数据流,默认情况下这两种数据流都会在显示器上打印出来,而我们使用的>相当于1>,也就是将正确的信息写入到了test文件中,错误的信息依旧会看到。利用前面的提到0,1,2这三个数字,我们可以这样做
```powershell
[root@node01 ~]# head -1 /etc/rc.d/* > test 2> test.err
[root@node01 ~]# cat test
==> /etc/rc.d/init.d <==
==> /etc/rc.d/rc0.d <==
==> /etc/rc.d/rc1.d <==
==> /etc/rc.d/rc2.d <==
==> /etc/rc.d/rc3.d <==
==> /etc/rc.d/rc4.d <==
==> /etc/rc.d/rc5.d <==
==> /etc/rc.d/rc6.d <==
==> /etc/rc.d/rc.local <==
#!/bin/bash
[root@node01 ~]# cat test.err 
head: 读取'/etc/rc.d/init.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc0.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc1.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc2.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc3.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc4.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc5.d' 时出错: 是一个目录
head: 读取'/etc/rc.d/rc6.d' 时出错: 是一个目录

But these are still two files. Can all this information be written into one file?

[root@node01 ~]# head -1 /etc/rc.d/* > test.both 2>&1
[root@node01 ~]# cat test.both 
==> /etc/rc.d/init.d <==
head: 读取'/etc/rc.d/init.d' 时出错: 是一个目录
==> /etc/rc.d/rc0.d <==
head: 读取'/etc/rc.d/rc0.d' 时出错: 是一个目录
==> /etc/rc.d/rc1.d <==
head: 读取'/etc/rc.d/rc1.d' 时出错: 是一个目录
==> /etc/rc.d/rc2.d <==
head: 读取'/etc/rc.d/rc2.d' 时出错: 是一个目录
==> /etc/rc.d/rc3.d <==
head: 读取'/etc/rc.d/rc3.d' 时出错: 是一个目录
==> /etc/rc.d/rc4.d <==
head: 读取'/etc/rc.d/rc4.d' 时出错: 是一个目录
==> /etc/rc.d/rc5.d <==
head: 读取'/etc/rc.d/rc5.d' 时出错: 是一个目录
==> /etc/rc.d/rc6.d <==
head: 读取'/etc/rc.d/rc6.d' 时出错: 是一个目录
==> /etc/rc.d/rc.local <==
#!/bin/bash

or

[root@node01 ~]# head -1 /etc/rc.d/* >& test.both1
[root@node01 ~]# cat test.both1 
==> /etc/rc.d/init.d <==
head: 读取'/etc/rc.d/init.d' 时出错: 是一个目录
==> /etc/rc.d/rc0.d <==
head: 读取'/etc/rc.d/rc0.d' 时出错: 是一个目录
==> /etc/rc.d/rc1.d <==
head: 读取'/etc/rc.d/rc1.d' 时出错: 是一个目录
==> /etc/rc.d/rc2.d <==
head: 读取'/etc/rc.d/rc2.d' 时出错: 是一个目录
==> /etc/rc.d/rc3.d <==
head: 读取'/etc/rc.d/rc3.d' 时出错: 是一个目录
==> /etc/rc.d/rc4.d <==
head: 读取'/etc/rc.d/rc4.d' 时出错: 是一个目录
==> /etc/rc.d/rc5.d <==
head: 读取'/etc/rc.d/rc5.d' 时出错: 是一个目录
==> /etc/rc.d/rc6.d <==
head: 读取'/etc/rc.d/rc6.d' 时出错: 是一个目录
==> /etc/rc.d/rc.local <==
#!/bin/bash

这两种使用方式都是告诉shell将错误信息写入到正确信息所写入的文件中。
如果这些错误信息是我们早就知道的,并且还不想看到的呢?
[root@node01 ~]# head -1 /etc/rc.d/* 2> /dev/null 
==> /etc/rc.d/init.d <==
==> /etc/rc.d/rc0.d <==
==> /etc/rc.d/rc1.d <==
==> /etc/rc.d/rc2.d <==
==> /etc/rc.d/rc3.d <==
==> /etc/rc.d/rc4.d <==
==> /etc/rc.d/rc5.d <==
==> /etc/rc.d/rc6.d <==
==> /etc/rc.d/rc.local <==
#!/bin/bash

/dev/null:表示的是一个黑洞,通常用于丢弃不需要的数据输出

In summary, the usage of input and output redirection and merging are as follows:

grammar effect
cmd < file Redirect standard input from file
cmd > file Redirect standard output to file, overwriting (corrupting) file if it exists
cmd>>file Redirect standard output to file, if file exists, append to it
cmd 2>file Redirect standard error to file, if file exists, overwrite (corrupt) it
cmd 2>> file Redirect standard error to file. If file exists, append it to it.
cmd>file 2>&1 Combine standard output and standard error and redirect them to file (portable syntax)
cmd >& file Combine standard output and standard error and redirect them to file (convenient syntax)

4. Pipeline

Earlier, we saw that a process's output can be redirected to somewhere other than the terminal display, or that a process can be made to read input from somewhere other than the terminal keyboard. One of the most common and powerful forms of redirection is a combination of the two, in which the output of one command (standard output) is "piped" directly to the input of another command (standard input). , thus forming what Linux (and Unix) calls a pipe. When two commands are piped together, the standard output stream of the first process is connected directly to the standard input sequence of the second process. To create a pipeline in bash, connect the two commands with a vertical barline |.

image20200323144226709.png

[root@node01 ~]# pwd
/root
[root@node01 ~]# ls | grep ana
anaconda-ks.cfg
注意:从管道读数据是一次性操作,数据一旦被读,它就从管道中被抛弃,释放空间以便写更多的数据。它只能处理经由前面一个指令传出的正确输出信息,对错误信息信息没有直接处理能力。然后,传递给下一个命令,作为标准的输入。

5. Common tools for data processing

5.1. find file search command

.  代表当前目录
~  代表用户家目录

find command options

-name	按照文件名查找文件。
[root@node01 ~]# find ~ -name "test.*"
/root/test.err
/root/test.both
/root/test.both1


-perm	按照文件权限来查找文件。
[root@node01 ~]# chmod 777 hello
[root@node01 ~]# find . -perm 777
./hello


-user	按照文件属主来查找文件。
-group	按照文件所属的组来查找文件。
-uid	查找指定uid
-gid	查找指定gid
[root@node01 ~]# chown hello.hello test
[root@node01 ~]# find . -user hello
./test
[root@node01 ~]# find . -group hello
./test
[root@node01 ~]# chown root.hello hello
[root@node01 ~]# find . -user root -group hello
./hello
[root@node01 ~]# find . -uid 1000
./test
[root@node01 ~]# find . -gid 1000
./test


-mtime -n +n	按照文件的更改时间来查找文件, -n表示n天以内,+n表示n天以前。还有-atime和-ctime 选项
-amin/-cmin/-mmin n		 查找系统中最后N分钟

  
#如果-mtime +2 表示当前时间-2day以前的mtime的文件。即文件的mtime小于 sysdate -2
#如果-mtime -2 表示当前时间-2day以后的mtime的文件。即文件的mtime大于 sysdate -2
#如果-mtime  2 表示文件mtime在sysdate -2 与 sysdate-1 之间的文件。
#!!!!!实际上再记不住,记住一般删除旧数据,一定是选择+。

[root@node01 ~]# find /etc -mtime -2 
/etc
/etc/cups
/etc/cups/subscriptions.conf
/etc/resolv.conf



-nogroup	查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser	查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
[root@node01 ~]# chown 1400.1400 hello
[root@node01 ~]# ll hello
-rwxrwxrwx 1 1400 1400 8 3月  22 23:25 hello
[root@node01 ~]# find . -nogroup
./hello
[root@node01 ~]# find . -nouser
./hello

-newer file1 查找更改时间比文件file1新的文件。
[root@node01 ~]# find /etc -newer initial-setup-ks.cfg 
/etc
/etc/dnf/modules.d
/etc/dnf/modules.d/httpd.module
/etc/logrotate.d
还有anewer和cnewer




-type	查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。


[root@node01 ~]# find /etc -newer initial-setup-ks.cfg -type f
/etc/dnf/modules.d/httpd.module
/etc/logrotate.d/glusterfs
/etc/pki/nssdb/cert9.db
/etc/pki/nssdb/key4.db
/etc/yum.repos.d/server.repo


-size n[bcwkMG] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
			b	512字节一块
			c	字节
			w	字(2字节)
			k	k字节(1024)
			M	M字节(1024x1024字节)
			G	G字节(1024x1024x1024字节)
[root@node01 ~]# find . -size +1M
./.cache/tracker/meta.db
./.cache/tracker/meta.db-wal
./.cache/mozilla/firefox/mduza4q4.default/startupCache/startupCache.8.little
./.cache/mozilla/firefox/mduza4q4.default/startupCache/scriptCache-child-current.bin
./.cache/mozilla/firefox/mduza4q4.default/startupCache/scriptCache-current.bin
./.mozilla/firefox/mduza4q4.default/places.sqlite
./.mozilla/firefox/mduza4q4.default/favicons.sqlite
# 在使用的时候注意一下单位,如find . -size 4k使用4k时会显示所有大与3k小于等于4k的文件,如果使用的是4096c则是查找大小为4k的文件
[root@node01 ~]# find . -size 4k
.
./.cache/mozilla/firefox/mduza4q4.default/startupCache/urlCache-current.bin
./.cache/mozilla/firefox/mduza4q4.default/startupCache/urlCache.bin
./.cache/mozilla/firefox/mduza4q4.default/safebrowsing
./.config/pulse
./.mozilla/firefox/mduza4q4.default
./.mozilla/firefox/mduza4q4.default/datareporting/archived/2020-02
./.mozilla/firefox/mduza4q4.default/saved-telemetry-pings
[root@node01 ~]# find . -size 4096c
.
./.cache/mozilla/firefox/mduza4q4.default/safebrowsing
./.config/pulse
./.mozilla/firefox/mduza4q4.default
./.mozilla/firefox/mduza4q4.default/datareporting/archived/2020-02
./.mozilla/firefox/mduza4q4.default/saved-telemetry-pings
[root@node01 ~]# find . -size 4k |wc -l
8
[root@node01 ~]# find . -size 4096c |wc -l
6
[root@node01 ~]# ll -h .cache/mozilla/firefox/mduza4q4.default/startupCache/urlCache-current.bin 
-rw-r--r-- 1 root root 3.1K 2月  24 02:34 .cache/mozilla/firefox/mduza4q4.default/startupCache/urlCache-current.bin


-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
[root@node01 ~]# ln -s /etc/ linketc
[root@node01 ~]# find . -name passwd
[root@node01 ~]# find . -name passwd -follow
./linketc/pam.d/passwd
./linketc/passwd

-exec   command   {
    
    } \;      —–将查到的文件执行command操作,{
    
    } 和 \;之间有空格
-ok 和-exec相同,只不过在操作前要询用户
[root@node01 ~]# find /etc -name passwd -exec grep "hello" {} \;
hello:x:1000:1000:hello:/home/hello:/bin/bash
[root@node01 ~]# find /etc -name passwd -ok grep "hello" {} \;
< grep ... /etc/pam.d/passwd > ? y
< grep ... /etc/passwd > ? y
hello:x:1000:1000:hello:/home/hello:/bin/bash


-empty	查找空文件或目录
[root@node01 ~]# find /etc -empty
/etc/crypttab
/etc/dnf/aliases.d
/etc/dnf/modules.defaults.d


-inum	查找i节点编号为指定数值的文件
-samefile	查找相同的文件

[root@node01 ~]# ln hello hello1
[root@node01 ~]# ln hello hello2
[root@node01 ~]# ll -i hello
34510098 -rwxrwxrwx 3 1400 1400 8 3月  22 23:25 hello
[root@node01 ~]# find . -inum 34510098
./hello
./hello1
./hello2

[root@node01 ~]# find . -samefile hello
./hello
./hello1
./hello2

5.2. grep&egrep data retrieval command

A powerful text search tool that can use regular expressions to search text and print matching lines. It is most commonly used. egrep is an extension of grep, supports more regular expression metacharacters, and is equivalent to grep -E.

useradd hello
useradd hello
useradd hello

grep and regular expression symbols

^ 行的开始 如:'^grep'匹配所有以grep开头的行。
[root@node01 ~]# grep '^he' /etc/passwd

$	行的结束 如:'grep$'匹配所有以grep结尾的行。
[root@node01 ~]# grep 'sh$' /etc/passwd

.	匹配一个非换行符('\n')的字符如:'gr.p'匹配gr后接一个任意字符,然后是p。
[root@node01 ~]# grep 't.e' /etc/passwd

* 匹配零个或多个先前字符 如:' *grep' (注意*前有空格)匹配所有零个或多个空格后紧跟grep的行,需要用egrep 或者grep带上 -E 选项。 .*一起用代表任意字符。
[root@node01 ~]# grep -E 'el*lo' /etc/passwd

[]	匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。
[root@node01 ~]# grep '[Hh]el' /etc/passwd

[^] 匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-F和H-Z的一个字母开头,紧跟rep的行。
[root@node01 ~]# grep '[^A-Z]ello' /etc/passwd
[root@node01 ~]# grep '[^a-z]ello' /etc/passwd

\?	匹配零个或一个先前的字符。如:'gre\?p'匹配gr后跟一个或零个e字符,然后是p的行。
[root@node01 ~]# grep 'hell\?o' /etc/passwd
hello:x:1000:1000:hello:/home/hello:/bin/bash
helo:x:1002:1002::/home/helo:/bin/bash

x\{
    
    m\} 重复字符x,m次,如:'o\{5\}'匹配包含5个o的行。
[root@node01 ~]# grep 'l\{2\}' /etc/passwd

x\{
    
    m,\} 重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。
[root@node01 ~]# grep 'l\{2,\}' /etc/passwd

x\{
    
    m,n\} 重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。
[root@node01 ~]# grep 'l\{1,3\}' /etc/passwd

\b 单词锁定符,如: '\bgrep\b'只匹配grep。
[root@node01 ~]# grep 'hell' /etc/passwd
hello:x:1000:1000:hello:/home/hello:/bin/bash
helllo:x:1003:1003::/home/helllo:/bin/bash
[root@node01 ~]# grep '\bhell\b' /etc/passwd


egrep和 grep -E的扩展集
+	匹配一个或多个先前的字符。如:'[a-z]+able',匹配一个或多个小写字母后跟able的串.
[root@node01 ~]# grep -E '[a-z]+llo' /etc/passwd
hello:x:1000:1000:hello:/home/hello:/bin/bash
helllo:x:1003:1003::/home/helllo:/bin/bash
[root@node01 ~]# grep -E '[a-z]+lo' /etc/passwd

?	作用同\?,如:'gre?p'匹配gr后跟一个或零个e字符,然后是p的行。
[root@node01 ~]# grep -E 'hel?lo' /etc/passwd
hello:x:1000:1000:hello:/home/hello:/bin/bash
helo:x:1002:1002::/home/helo:/bin/bash
[root@node01 ~]# grep 'hel?lo' /etc/passwd
[root@node01 ~]# grep 'hel\?lo' /etc/passwd
hello:x:1000:1000:hello:/home/hello:/bin/bash
helo:x:1002:1002::/home/helo:/bin/bash

a|b|c 匹配a或b或c。如:grep|sed匹配grep或sed
[root@node01 ~]# grep -E 'llo|lllo' /etc/passwd
hello:x:1000:1000:hello:/home/hello:/bin/bash
helllo:x:1003:1003::/home/helllo:/bin/bash

*	0次或多次
?	0次或1次
+	1次或多次

-E		支持扩展正则,相当于egrep
-i		不区分大小写
-v		取反,显示不匹配的
--color=auto	高亮显示

5.3. sort sorts the file contents

Common command options

-b 忽略每行前面开始出的空格字符。
[root@node01 ~]# sort passwd
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
 hello:x:1000:1000:hello:/home/hello:/bin/bash
    HELLO:x:1001:1001::/home/HELLO:/bin/bash
      helo:x:1002:1002::/home/helo:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[root@node01 ~]# sort -b passwd
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
 hello:x:1000:1000:hello:/home/hello:/bin/bash
    HELLO:x:1001:1001::/home/HELLO:/bin/bash
      helo:x:1002:1002::/home/helo:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
root:x:0:0:root:/root:/bin/bash

-c 检查文件是否已经按照顺序排序。
[root@node01 ~]# sort -c passwd 
sort:passwd:2:无序: bin:x:1:1:bin:/bin:/sbin/nologin

-t	用指定的符号做为分隔符
-k	指定区间
-n 	依照数值的大小排序。
-r 以相反的顺序来排序。
[root@node01 ~]# sort -t ':' -k 3 -r -n  passwd 
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
      helo:x:1002:1002::/home/helo:/bin/bash
    HELLO:x:1001:1001::/home/HELLO:/bin/bash
 hello:x:1000:1000:hello:/home/hello:/bin/bash
dbus:x:81:81:System message bus:/:/sbin/nologin
   apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
或者
[root@node01 ~]# sort -t ':' -k 3rn passwd 
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
      helo:x:1002:1002::/home/helo:/bin/bash
    HELLO:x:1001:1001::/home/HELLO:/bin/bash
 hello:x:1000:1000:hello:/home/hello:/bin/bash
dbus:x:81:81:System message bus:/:/sbin/nologin
   apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash


-o<输出文件> 将排序后的结果存入指定的文件。
[root@node01 ~]# sort -t ':' -k 3 -r -n  passwd -o /root/passwd-sort
[root@node01 ~]# cat /root/passwd-sort 
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
      helo:x:1002:1002::/home/helo:/bin/bash
    HELLO:x:1001:1001::/home/HELLO:/bin/bash
 hello:x:1000:1000:hello:/home/hello:/bin/bash
dbus:x:81:81:System message bus:/:/sbin/nologin
   apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash

5.4. uniq data deduplication

Generally used with sort

Case document

[root@node01 ~]# cat testuniq
192.168.98.2
192.168.98.8
192.168.98.3
192.168.98.3
192.168.98.9
192.168.98.8
192.168.98.8
192.168.98.0
192.168.98.3

Common command options

-c	在每列旁边显示该行重复出现的次数。
[root@node01 ~]# uniq -c testuniq 
      1 192.168.98.2
      1 192.168.98.8
      2 192.168.98.3
      1 192.168.98.9
      2 192.168.98.8
      1 192.168.98.0
      1 192.168.98.3

[root@node01 ~]# uniq -c testuniq | sort
      1 192.168.98.0
      1 192.168.98.2
      1 192.168.98.3
      1 192.168.98.8
      1 192.168.98.9
      2 192.168.98.3
      2 192.168.98.8

-d	仅显示重复出现的行列。
[root@node01 ~]# uniq -d testuniq 
192.168.98.3
192.168.98.8

-u	仅显示出一次的行列。
[root@node01 ~]# uniq -u testuniq 
192.168.98.2
192.168.98.8
192.168.98.9
192.168.98.0
192.168.98.3
[root@node01 ~]# uniq -u testuniq | sort
192.168.98.0
192.168.98.2
192.168.98.3
192.168.98.8
192.168.98.9

5.5. tree lists directory contents in a tree structure

-a 显示所有文件和目录,默认不显示隐藏文件
[root@node01 ~]# tree -a

-C 在文件和目录清单加上色彩,便于区分各种类型。
[root@node01 ~]# tree -C

-d 只显示目录。
[root@node01 ~]# tree -d

-D 列出文件或目录的更改时间。
[root@node01 ~]# tree -D
.
├── [Feb  4 15:07]  公共
├── [Feb  4 15:07]  模板

-f 在每个文件或目录之前,显示完整的路径名称。
[root@node01 ~]# tree -f
.
├── ./公共
├── ./模板

-F 加上文件类型标记,在执行文件,目录,Socket,管道名称名称,各自加上"*","/","@","|"号。
[root@node01 ~]# tree -F
.
├── 公共/
├── 模板/

-g -u 列出文件或目录的所属群组、主名称,没有对应的名称时,则显示群组、主的识别码。
[root@node01 ~]# tree -g
.
├── [root    ]  公共
├── [root    ]  模板

-i 不以阶梯状列出文件或目录名称。
[root@node01 ~]# tree -i
.
公共
模板
视频

-n 不在文件和目录清单加上色彩。
[root@node01 ~]# tree -n
.
├── 公共
├── 模板

-p 列出权限标示。
[root@node01 ~]# tree -p
.
├── [drwxr-xr-x]  公共
├── [drwxr-xr-x]  模板

-s 列出文件或目录大小。
[root@node01 ~]# tree -s
.
├── [          6]  公共
├── [          6]  模板

-t 用文件和目录的更改时间排序。
[root@node01 ~]# tree -t
.
├── anaconda-ks.cfg
├── initial-setup-ks.cfg
├── 公共

-u 列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。
[root@node01 ~]# tree -u
.
├── [root    ]  公共
├── [root    ]  模板

-L 显示目录层数,数值为正整数
[root@node01 ~]# tree -a -L 2
.
├── .cache
│   ├── dconf
│   ├── event-sound-cache.tdb.d5b6fe70a77f4945aa0999abdd33e686.x86_64-redhat-linux-gnu
│   ├── evolution
│   ├── gnome-shell
│   ├── gnome-software

5.6. xargs command

xargs is a filter for passing parameters to commands and a tool for combining multiple commands. Used for some commands that do not support pipe input such as ls

case file

[root@node01 ~]# cat xargtest 
a a a
b b b
c c c
d d d
e
f
g
h
i
j
k
l

Common command options

-n num 后面加列数
[root@node01 ~]# cat xargtest | xargs -n 2
a a
a b
b b
c c
c d
d d
e f
g h
i j
k l
[root@node01 ~]# cat xargtest | xargs
a a a b b b c c c d d d e f g h i j k l

-I	指定替换的字符串,并在后续的命令中用指定的字符串表示接收到的输入内容,并执行,可以用任意字符代替(不推荐有特殊含义的字符,如*等,如果使用请记得转意),一般为[]{
    
    }
[root@node01 ~]# mkdir testx
[root@node01 ~]# find /var/ -name "*.log" | xargs  -I a cp a ./testx
[root@node01 ~]# ls testx | wc -l
39

-i	类似-I,相当于固定就是使用{
    
    }来表示接收到的输入内容
[root@node01 ~]# find /var/ -name "*.log" | xargs  -i cp {} ./testx
[root@node01 ~]# ls testx | wc -l
39


-d 指定分隔符
[root@node01 ~]# echo "a9b9c9d9" | xargs -d 9
a b c d 

Guess you like

Origin blog.csdn.net/qq_45277554/article/details/130882656
Recommended