一、文件重定向
IO的分类
名称 | 说明 | 编号 | 默认输出设备 |
---|---|---|---|
STDIN | 标准输入 | 0 | 键盘 |
STDOUT | 标准输出 | 1 | 终端 |
STDERR | 标准错误输出 | 2 | 终端 |
各种常用的重定向及示例
二、常用文本处理命令
grep –Gun Regular Expression Print
–正则表达式,用于匹配输出文件中你想要的内容
# grep root /etc/passwd —匹配出包含root的行
# grep root /etc/passwd –color –将匹配的信息加颜色输出
# grep -c root passwd –color –统计匹配的行数
# grep ^root passwd –color –只匹配开头是root的
# grep bash$ passwd –color –查找以bash结尾
# grep -v bash$ passwd –color –找出所有不是以bash结尾的内容(-v:取反)
grep -c 出现数字
grep -c root /etc/passwd
grep -w word
grep
-w word
-c count
tr 替换字符
# cat a.txt | tr ‘\n’ ’ ’
# cat a.txt | tr a-z A-Z
tr默认从键盘输入数据
将某个文件中或者管道送来的转换为其他
tr [optios]
tr ‘123’ ‘abc’
tr -d 把出现中属于字符集合从文件中删除
echo “hello world” | tr ‘a-z’ ‘A-Z’
HELLO WORLD
不能处理文件
处理文件必须是输入重定向 tr ‘a-z’ ‘A-Z’ < /etc/fstab
cut –切
# cut -d : -f1 passwd –d:指定分隔符,-f:指定显示第几列
# cut -d : -f1,3 passwd –显示第1和3列
# cut -d : -f1-3 passwd –显示1-3列
# cut -d : -f1-3,5 passwd –显示1-3列和第5列
切割
在关系型数据库里叫投影
cut :文件切割,最常用的功能是能够制定的符号作为行中的内容的分隔符,将每一行切片,只取出某些内容
cut [options] file
-d 指定分隔符 默认分隔符就是空白字符 -d:
-f 取第几字段 1,5 1-5 1和5 1到5
uniq 唯一的独特的 取出来文件不重复的行
-c 统计每行重复的次数
-u 只显示没有重复的行
-d 只显示重复过的行 挨着的才算重复的
# cat 1.txt
aaaaaa
bbbbbb
ffffff
dddddd
yyyyyy
oooooo
oooooo
tttttt
yyyyyy
# uniq 1.txt –去除连续的重复行
aaaaaa
bbbbbb
ffffff
dddddd
yyyyyy
oooooo
tttttt
yyyyyy
# uniq -c 1.txt –统计连续的重复行
1 aaaaaa
1 bbbbbb
1 ffffff
1 dddddd
1 yyyyyy
2 oooooo
1 tttttt
1 yyyyyy
[root@leopard test]# uniq -d 1.txt –只显示连续的重复行
oooooo
[root@leopard test]# uniq -u 1.txt –只显示不连续的重复行
aaaaaa
bbbbbb
ffffff
dddddd
yyyyyy
tttttt
yyyyyy
sort 排序 对文件中的文本内容排序 不会操作原文件 ,只会显示到屏幕
-f 忽略字符大小写
-n 数值从小到大排序 默认是mask表中的字符排序的
-r 逆序从大到小排序
-u 将重复的行移除,只保留一个
-t 指定分隔符
-k 指定用第几个段分割
tk用的不多
-R 随机排序
tee 在管道中既保存数据到文件又能够传送到管道 多道输出
tee
command | tee /文件
cat /etc/passwd | tee /text/text.txt | less
文件查找
find –全盘搜索命令
-iname 查找名字不区分大小写
-empty 正常的空文件或空目录
-user username 特定用户的文件
-group groupname
-uid id
-gid gid
-nouser 查找没有用户的文件
-nogroup 超找没有属组的文件
组合查找条件
-a 与 可以省略
-o 或
-not ! 非 取反
德摩根定律
find /tmp/ -nouser -a -nogroup = find /tmp/ -nouser -nogroup
find /tmp/ -nouser -o -nogroup 没有用户或没有属组的
根据文件类型查看
-type
根据文件大小
-size [+-] 不给单位就是字节
进行向上进的圆整
ls -lh ‘find /etc/ -size 2k‘=2k的文件
根据时间戳查找
amin最近几分钟访问的文件 [+/-]
mmin修改过的文件
cmin改变的文件
atime
mtime
ctime
-amin -3最近3分钟访问过
-amin +3 至少有3分钟没有访问过刚好3分钟
根据权限查找
-perm +mode
-perm 666 精确匹配你指定的权限 ls -ldh ’find /etc/ -perm 755‘ 002 有一定的权限就行
-perm -mode 所有的权限都符合你指定的权限位,对应位必须对应上 ls -ldh find /etc/ -perm -755
同时包含了 666 660是否符合
动作:
-print
-ls
-exec
-ok 对查找到的文件逐个执行
语法:find 路径名称 条件 参数
搜索条件
可以基于文件名称、文件类型、文件大小、修改时间、权限、属主和属组等等查找文件
基于文件名称查找:
文件的类型:
b block (buffered) special --块设备文件
c character (unbuffered) s-pecial --字符型设备(IO)
d directory --目录文件
p named pipe (FIFO) --管道文件
f regular file --普通的文本文件
l symbolic link --链接文件
s socket --套接字文件
-iname 查找名字不区分大小写
-empty 正常的空文件或空目录
# find / -name file3
/test/file3
[root@leopard Desktop]# find /etc -name ifcfg-eth0
[root@leopard test]# find / -name “file*” –模糊匹配
[root@leopard test]# find /test -name “*.txt”
基于文件所有者和所属组查找
# find / -user tom
# find /test -group group1 -ls
基于文件的类型查找
# find /test -type d -ls
# find /test -type f -ls
# find /dev -type b
# find /dev/pts -type c -ls
# find /var -type p -ls
# find /var -type s -ls
# find /usr/sbin/ -type l
基于文件大小查找:cwbkMG
通过dd命令可以构造任意大小的文件
# dd if=/dev/zero of=/test/myfile1 bs=1M count=4
# dd if=/dev/zero of=/test/myfile2 bs=512K count=4
# dd if=/dev/zero of=/test/myfile3 bs=1 count=1024
# find /test -size +2M –查找大于2M的文件
# find /test -size 2M -ls –查找大小就是2M的文件
# find /test -size -2M –查找小于2M的文件
基于时间的查找(以n*24小时为单位ctime、atime、mtime,以分钟为单位cmin、amin、mmin)
# find /test -mtime 6 -ls –第6+1个24小时内内容被修改的文件
# find /test -mtime +6 -ls –第6+1个24小时以前内容被修改的文件
# find /test -mtime -4 -ls –从当前时间开始4个24小时内被修改的文件
基于权限查找
# find /test -perm 111 -ls –精确查找
# find /test -perm +111 -ls –只要ugo中有个具有x权限即可
# find /test -perm -111 -ls –保证ugo中至少每个都具有x权限
which 显示命令所在的路径
# which vim
/usr/bin/vim
查找的路径
# echo $PATH
/usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
手动创建一个小命令
# cd /test
# cat nfile
mkdir /test/dir
touch /test/dir/file1
# ls
nfile
# chmod u+x nfile
# ll
总用量 4
-rwxr–r–. 1 root root 38 1月 16 07:27 nfile
# nfile
-bash: nfile: command not found
# pwd
/test
# PATH=$PATH:/test –临时改变变量的赋值
# which nfile
/test/nfile
如何设置变量永久生效:
# cat /root/.bash_profile
PATH=
PATH:
HOME/bin
PATH=$PATH:/test
export PATH
# source /root/.bash_profile –从新读取配置文件
locate –按文件名称检索
–基于数据库查找
# locate nfile
如何更新数据库
# updatedb
数据库位置
# ll /var/lib/mlocate/mlocate.db
-rw-r—–. 1 root slocate 2074651 1月 16 04:40 /var/lib/mlocate/mlocate.db