1.grep 过滤内容,筛选内容(三剑客之一,排行老三)
按行输出
grep [选项] 内容 文件
---color-auto 过滤的内容显示颜色
1) 过滤出含有root字符串的行:
[root@localhost ~]# grep root oldboy.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# \grep root oldboy.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2) 过滤出不含root字符串的行
-v 取反
[root@localhost ~]# grep -v root oldboy.txt
lsls:x:0:0:lsls:/lsls:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
asyncfsc:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/ljyljy:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
1 I am oldboy.I am 30
2 I am oldboy.I am 30
3 I am oldboy.I am 30
[root@localhost ~]#vim oldboy.txt
在123....上边行里输入大写ROOT,并且在123....里边随机插入ROOT,输入冒号:,按键盘键wq!即可保存退出。
3) 过滤出含有root字符串(含大写)的行
-i 不区分大小写
[root@localhost ~]# grep -i root oldboy.txt
ROOT
2 I am ROOT oldboy.I am 30
5 I am oldboy.I ROOT am 30
7 I ROOT am oldboy.I am 30
4) 同时过滤含有oldboy和root的行
-E 同时过滤多个字符串
[root@localhost ~]# grep -E "oldboy|ROOT" oldboy.txt
ROOT
1 I am oldboy.I am 30
2 I am ROOT oldboy.I am 30
3 I am oldboy.I am 30
4 I am oldboy.I am 30
5 I am oldboy.I ROOT am 30
6 I am oldboy.I am 30
7 I ROOT am oldboy.I am 30
8 I am oldboy.I am 30
9 I am oldboy.I am 30
10 I am oldboy.I am 30
grep -E = egrep
[root@localhost ~]# egrep "oldboy|ROOT" oldboy.txt
ROOT
1 I am oldboy.I am 30
2 I am ROOT oldboy.I am 30
3 I am oldboy.I am 30
4 I am oldboy.I am 30
5 I am oldboy.I ROOT am 30
6 I am oldboy.I am 30
7 I ROOT am oldboy.I am 30
8 I am oldboy.I am 30
9 I am oldboy.I am 30
10 I am oldboy.I am 30
5)了解参数
-o 只输出匹配的内容
[root@localhost ~]# grep -o ROOT oldboy.txt
ROOT
ROOT
ROOT
ROOT
[root@localhost ~]# grep -o oldboy oldboy.txt
oldboy
oldboy
oldboy
oldboy
oldboy
oldboy
oldboy
oldboy
-n 过滤出的内容,在文件中的行号
[root@localhost ~]# grep -n ROOT oldboy.txt
27:ROOT
29:2 I am ROOT oldboy.I am 30
32:5 I am oldboy.I ROOT am 30
34:7 I ROOT am oldboy.I am 30
[root@localhost ~]# grep -n "." oldboy.txt
特殊注意: 此处过滤的点代表任意一个字符。
1:lsls:x:0:0:lsls:/lsls:/bin/bash
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:bin:x:1:1:bin:/bin:/sbin/nologin
5:adm:x:3:4:adm:/var/adm:/sbin/nologin
6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7:sync:x:5:0:sync:/sbin:/bin/sync
8:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9:halt:x:7:0:halt:/sbin:/sbin/halt
10:adm:x:3:4:adm:/var/adm:/sbin/nologin
11:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
12:asyncfsc:x:5:0:sync:/sbin:/bin/sync
13:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
14:halt:x:7:0:halt:/sbin:/sbin/halt
15:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
16:operator:x:11:0:operator:/ljyljy:/sbin/nologin
17:games:x:12:100:games:/usr/games:/sbin/nologin
18:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
19:nobody:x:99:99:Nobody:/:/sbin/nologin
20:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
21:dbus:x:81:81:System message bus:/:/sbin/nologin
22:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
23:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
24:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
25:ntp:x:38:38::/etc/ntp:/sbin/nologin
26:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
27:ROOT
28:1 I am oldboy.I am 30
29:2 I am ROOT oldboy.I am 30
30:3 I am oldboy.I am 30
31:4 I am oldboy.I am 30
32:5 I am oldboy.I ROOT am 30
33:6 I am oldboy.I am 30
-w 按单词过滤
oldboy和oldboy123不是一个东西了,oldboy123不会被过滤出来。
[root@localhost ~]# grep -w oldboy oldboy.txt
1 I am oldboy.I am 30
2 I am ROOT oldboy.I am 30
3 I am oldboy.I am 30
4 I am oldboy.I am 30
5 I am oldboy.I ROOT am 30
6 I am oldboy.I am 30
7 I ROOT am oldboy.I am 30
8 I am oldboy.I am 30
9 I am oldboy.I am 30
10 I am oldboy.I am 30
[root@localhost ~]# echo oldboy123 >>oldboy.txt
[root@localhost ~]# cat oldboy.txt
lsls:x:0:0:lsls:/lsls:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
asyncfsc:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/ljyljy:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
ROOT
1 I am oldboy.I am 30
2 I am ROOT oldboy.I am 30
3 I am oldboy.I am 30
4 I am oldboy.I am 30
990 I am oldboy.I am 30
991 I am oldboy.I am 30
992 I am oldboy.I am 30
993 I am oldboy.I am 30
994 I am oldboy.I am 30
995 I am oldboy.I am 30
996 I am oldboy.I am 30
997 I am oldboy.I am 30
998 I am oldboy.I am 30
999 I am oldboy.I am 30
1000 I am oldboy.I am 30
oldboy123
[root@localhost ~]# grep -w oldboy oldboy.txt
1 I am oldboy.I am 30
2 I am ROOT oldboy.I am 30
3 I am oldboy.I am 30
4 I am oldboy.I am 30
5 I am oldboy.I ROOT am 30
6 I am oldboy.I am 30
7 I ROOT am oldboy.I am 30
8 I am oldboy.I am 30
9 I am oldboy.I am 30
10 I am oldboy.I am 30
2.touch 创建文件
[root@localhost ~]# touch file1
[root@localhost ~]# cat file1
[root@localhost ~]# cat file{1..3}
cat: file2: No such file or directory
cat: file3: No such file or directory
[root@localhost ~]# touch file{1..3}
[root@localhost ~]# ls
anaconda-ks.cfg file1 file2 file3 oldboy.txt
[root@localhost ~]# touch file{1..100}
[root@localhost ~]# ls
anaconda-ks.cfg file12 file17 file21 file26
file30 file35 file4 file44 file49 file53
file58 file62 file67 file71 file76 file80
file85 file9 file94 file99 file57 file61
file1 file13 file18 file22 file27 file31
file36 file40 file45 file5 file54 file59
file63 file68 file72 file77 file81 file86
file90 file95 oldboy.txt file66 file70
file10 file14 file19 file23 file28 file32
file37 file41 file46 file50 file55 file6
file64 file69 file73 file78 file82 file87
file91 file96 file48 file52 file75 file8
file100 file15 file2 file24 file29 file33
file38 file42 file47 file51 file56 file60
file65 file7 file74 file79 file83 file88
file97 file43 file84 file89 file93 file98
file11 file16 file20 file25 file92 file39
file3
file34
{1..100} 一个序列:
[root@localhost ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@localhost ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@localhost ~]# echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
seq输出数字序列:
输出1-5:
[root@localhost ~]# seq 5
1
2
3
4
5
输出2-5:
[root@localhost ~]# seq 2 5
2
3
4
5
步长2;从2到10:
[root@localhost ~]# seq 2 2 10
2
4
6
8
10
-s指定空格位空格符:
[root@localhost ~]# seq -s "" 5
12345
-w 补齐位数:
[root@localhost ~]# seq -w -s " " 10
01 02 03 04 05 06 07 08 09 10
3.别名:alias
为什么要用别名?
提高效率。好记。
防止误删数据,提醒用户。
1) 查看已有别名:
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color
=auto'
alias fgrep='fgrep --color
=auto'
alias grep='grep --color
=auto'
alias l.='ls -d .* --color
=auto'
alias ll='ls -l --color
=auto'
alias ls='ls --color
=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='
alias | /usr/bin/which
--tty-only --read-alias --show-dot --show-tilde'
2) 如何设置别名:
提高输入效率:
alias net='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.11
GATEWAY=255.255.255.0
GATEWAY=192.168.1.2
[root@localhost ~]# alias net
='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color
=auto'
alias fgrep='fgrep --color
=auto'
alias grep='grep --color
=auto'
alias l.='ls -d .* --color
=auto'
alias ll='ls -l --color
=auto'
alias ls='ls --color
=auto'
alias mv='mv -i'
alias net
='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
alias rm='rm -i'
alias which=
'alias | /usr/bin/which --tty-only --read-alias
--show-dot --show-tilde'
[root@localhost ~]# net
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.11
GATEWAY=255.255.255.0
GATEWAY=192.168.1.2
防止误删数据,提醒用户:
[root@localhost ~]# alias rm
='echo "你好,请不要使用rm命令,用mv替代"'
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color
=auto'
alias fgrep='fgrep --color
=auto'
alias grep='grep --color
=auto'
alias l.='ls -d .* --color
=auto'
alias ll='ls -l --color
=auto'
alias ls='ls --color
=auto'
alias mv='mv -i'
alias net
='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
alias rm
='echo "你好,请不要使用rm命令,用mv替代"'
alias which
='alias | /usr/bin/which --tty-only --read-alias --show-dot
--show-tilde'
[root@localhost ~]# rm
你好,请不要使用rm命令,用mv替代
步骤:
1. 命令能执行
echo "你好,请不要使用rm命令,用mv替代"
2. 英文状态输入。引号一定是英文输入。
3. alias rm='echo "你好,请不要使用rm命令,用mv替代"’
4.alias
5.rm看效果。
2.执行命令:
【别名】优先于【命令】
[如何能不使用别名?]
1. 命令全路径执行:
[root@localhost ~]# rm
你好,请不要使用rm命令,用mv替代
[root@localhost ~]# /bin/rm a
/bin/rm: cannot remove ‘a’: No such file or directory
[root@localhost ~]# \rm -f *
[root@localhost ~]# touch a
[root@localhost ~]# rm a
你好,请不要使用rm命令,用mv替代 a
[root@localhost ~]# which rm
alias rm='echo "你好,请不要使用rm命令,用mv替代"'
/usr/bin/echo
[root@localhost ~]# /bin/rm a
2. 命令开头加反斜线:
[root@localhost ~]# rm -f *
你好,请不要使用rm命令,用mv替代 -f *
[root@localhost ~]# \rm -f *
3. 取消别名:
[root@localhost ~]# unalias ls
[root@localhost ~]# ls /etc
adjtime cron.deny
environment host.conf libaudit.conf
my.cnf.d popt.d
redhat-release skel tmpfiles.d
aliases cron.hourly
ethertypes hostname libnl
NetworkManager postfix
resolv.conf ssh tuned
aliases.db cron.monthly
exports hosts libuser.conf
networks ppp
resolv.conf.save ssl udev
alternatives crontab
favicon.png hosts.allow locale.conf
nsswitch.conf prelink.conf.d rpc
statetab vconsole.conf
anacrontab cron.weekly
filesystems hosts.deny localtime
nsswitch.conf.bak printcap
rpm statetab.d vimrc
asound.conf crypttab firewalld
init.d login.defs ntp
profile rsyslog.conf subgid
virc
audisp csh.cshrc fstab
inittab logrotate.conf
ntp.conf profile.d rsyslog.d
subuid wpa_supplicant
audit csh.login
gcrypt
inputrc logrotate.d
openldap protocols rwtab
sudo.conf X11
bash_completion.d dbus-1
gnupg iproute2 machine-id
opt python rwtab.d
sudoers xdg
bashrc default
GREP_COLORS issue magic
os-release rc0.d sasl2
sudoers.d xinetd.d
binfmt.d depmod.d
groff issue.net
makedumpfile.conf.sample pam.d
rc1.d securetty
sudo-ldap.conf yum
centos-release dhcp
group kdump.conf man_db.conf
passwd rc2.d security
sysconfig yum.conf
centos-release-upstream DIR_COLORS
group- kernel mke2fs.conf
passwd- rc3.d selinux
sysctl.conf yum.repos.d
chkconfig.d DIR_COLORS.256color
grub2.cfg krb5.conf modprobe.d
pkcs11 rc4.d services
sysctl.d
chrony.conf DIR_COLORS.lightbgcolor
grub.d krb5.conf.d modules-load.d
pki rc5.d sestatus.conf
systemd
chrony.keys dracut.conf
gshadow ld.so.cache motd
plymouth rc6.d shadow
system-release
cron.d dracut.conf.d
gshadow- ld.so.conf mtab
pm rc.d shadow-
system-release-cpe
cron.daily e2fsck.conf gss
ld.so.conf.d my.cnf polkit-1
rc.local shells terminfo