本文共 13868 字,大约阅读时间需要 46 分钟。
which :用来查找命令的绝对路径
-- 显示 shell 命令的绝对路径 -- 仅仅会在 PATH 变量中搜索要查找的命令 -- 搜索时先查找别名,然后从 PATH 中查找 1 、查看用户的 PATH 变量:命令的搜索路径 # echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin ------------------------- command not found 可能原因: 1 )敲错了 2 )命令没有安装 3 )命令所在路径没在 PATH 变量的定义中 ------------------------- # cp `which vim` /tmp/vim2 # vim2 /etc/passwd bash: vim2: command not found # /tmp/vim2 /etc/passwd // 绝对路径执行 2 、添加路径到 PATH 1 )临时修改 PATH 值 # PATH=$PATH:/tmp //$PATH :保留变量原有值 # echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/tmp 2 )永久修改 PATH 值 /etc/profile // 全局配置文件,对所有用户生效 ~username/.bash_profile // 局部配置文件,只对特定用户生效 # vim /root/.bash_profile PATH=$PATH:$HOME/bin:/tmp 上述文件不是即时生效的,正常情况下,它是用户登录时执行的。 # source /root/.bash_profile // 重新读取配置文件,使修改生效 # echo $PATH /usr/lib64/qt- 3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/root/bin:/tmp 弊端:每次新开启一个终端或标签,都需要执行 # source /root/.bash_profile 如果想一劳永逸,那么需要退出系统,重新登录,即注销。 System ——> Log out root ——> Log out # echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/tmp --------------------------------- # which ls alias ls='ls --color=auto' /bin/ls # which vim /usr/bin/vim 命令的别名: alias 1 、查看当前系统中有哪些别名 (root 用户和普通用户的别名可能不一样 ) # alias 2 、设置命令的别名 1 )临时 # alias vi='vim' # vi /etc/passwd // 执行 vi 时候,实际上执行的是 vim 2 )永久,改文件 ( 1 ) /root/.bashrc cp rm mv ( 2 ) /etc/profile.d colorls.sh which2.sh 3 、取消别名 [ profile.d]# unalias vi [ profile.d]# vi /etc/passwd // 没颜色了 locate -- 通过文件名检索文件,检索速度最快 -- 所有能够检索的东西,都是存放在数据库中的 -- locate 局限性,有的文件系统、有的文件及有的目录默认是不会搜索的 1 、假设我知道网卡配置文件的名字,但是不知道具体路径: # locate ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0 # locate ifcfg /etc/dbus-1/system.d/nm-ifcfg-rh.conf /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-lo /sbin/ifcfg /usr/lib64/NetworkManager/libnm-settings-plugin-ifcfg-rh.so /usr/share/man/man8/ifcfg.8.gz /var/log/anaconda.ifcfg.log 2 、手动更新数据库 # cp `which vim` /root/vim3 # locate vim3 // 未查询到结果 原因:因为 locate 的数据库是一天一更新,不是实时更新的。 # updatedb # locate vim3 /root/vim3 数据库文件: /var/lib/mlocate/mlocate.db ---------------------------------------------------------- 报错: 1 )数据库文件不存在 2 )手动生成它 # updatedb ---------------------------------------------------------- 3 、 locate 数据库配置文件 # vim /etc/updatedb.conf # ls /tmp/vim2 /tmp/vim2 # locate vim2 // 搜索不到,因为 /tmp 在排除列表中 find ***** -- 全局性搜索文件 -- 工作方式:沿着文件的层次结构依次向下搜索,找到符合条件的,打印或者是执行相应的操作 一、语法格式 find 要搜索路径 条件 ( 选项 ) [ 动作 ] 1 、基本例子 # find /etc/ -name network /etc/vmware-tools/scripts/vmware/network /etc/sysconfig/network /etc/rc.d/init.d/network 一般情况下,查找范围越大,目录层级越深,查找速度就越慢。 # mkdir -p /q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m # touch /q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m/test.txt # time find / -name test.txt /q/w/e/r/t/y/u/i/o/p/a/s/d/f/g/h/j/k/l/z/x/c/v/b/n/m/test.txt /acl/test.txt /tmp/test.txt real 0m3.188s user 0m0.061s sys 0m2.953s 2 、按条件查找 1 )按照文件名搜索 -name :按名字查找 ***** -iname :忽略大小写 # find /etc -name zm # find /etc -iname zm 通配符: * 代表任意字符 ? 代表单个字符 查找 /etc 目录下,所有以 .conf 结尾的文件 # find /etc/ -name *.conf 查找 /etc/ 目录下,以 .conf 结尾,名称是 5 个字符的 find /etc/ -name "?????.conf" 2 )按照文件类型查找 -type f :普通文件 b c d l s p (不知道意思的查看以往的笔记) # find /var -type l // 查找 /var 目录下类型是软链接的文件 # ll `find /var -type l` // 验证是否查找到的都是软链接 # find /tmp/ -type f ----------------------- -type 后面只能跟一个字母 ----------------------- 查找的时候,可能会遇到 No such file or directory 的错误提示,是正常的,若不想看到可以将错误重定向到 “ 黑洞 ”(/dev/null) # find / -type s 2> /dev/null 3 )按照时间查找 -atime n 以天为单位 -ctime n -mtime n -amin n 以分钟为单位 -cmin n -mmin n n 为数字,前面可以带 + 或者 - 号 -mtime n +n : n+1 天之前 n : n 到 n+1 天之间 -n : n 天以内 以 n 等于 7 为例: 搜索最近七天内被访问过的所有文件 find . -type f -atime -7 搜索恰好在七天前被访问过的所有文件 find . -type f -atime 7 搜索超过七天内被访问过的所有文件 find . -type f -atime +7 搜索访问时间超过 10 分钟的所有文件 find . -type f -amin +10 找出比 file.log 修改时间更长的所有文件 find . -type f -newer file.log ---------------------------------------------------------------------- 插曲: 查看文件的属性信息? stat 文件名 # stat passwd File: `passwd' Size: 1030 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 917613 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-08-08 20:38:22.080984537 +0800 Modify: 2015-08-08 20:38:22.080984537 +0800 Change: 2015-08-08 20:38:22.080984537 +0800 一个文件有三个时间: atime :访问时间, cat more less ... ... mtime :文件的内容发生变化的时间 vim ... ... ( 也是 ll 所显示的时间 ) ctime :文件的属性发生变化的时间 比如:权限改变、大小改变、所有者、所属组等改变 例子: # echo hello > file # stat file File: `file' Size: 6 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 917619 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-08-13 12:16:19.494018488 +0800 Modify: 2015-08-13 12:16:19.495018470 +0800 Change: 2015-08-13 12:16:19.495018470 +0800 # cat file hello # stat file File: `file' Size: 6 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 917619 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-08-13 12:17:32.381018468 +0800 Modify: 2015-08-13 12:16:19.495018470 +0800 Change: 2015-08-13 12:16:19.495018470 +0800 # chmod +x file # stat file File: `file' Size: 6 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 1197781 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-08-13 12:17:32.381018468 +0800 Modify: 2015-08-13 12:16:19.495018470 +0800 Change: 2015-08-13 12:21:55.563018148 +0800 //ctime 发生变化 # echo hahaha >> file # stat file File: `file' Size: 13 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 1197781 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-08-13 12:17:32.381018468 +0800 Modify: 2015-08-13 12:23:54.286017831 +0800 Change: 2015-08-13 12:23:54.286017831 +0800 1 、打印文件的数字权限 # stat -c %a file 755 2 、打印文件的字母权限 # stat -c %A file -rwxr-xr-x ---------------------------------------------------------------------- 按时间查找内容 # mkdir /find # date Thu Aug 13 14:22:43 CST 2015 # cd /find/ [ find]# touch -t 08131425.30 f0813 //-t :指定文件的创建时间 MMDDHHmm.SS [ find]# touch -t 08121425.50 f0812 [ find]# touch -t 08111426.30 f0811 [ find]# touch -t 08101426.30 f0810 [ find]# ll f* -rw-r--r-- 1 root root 0 Aug 10 14:26 f0810 -rw-r--r-- 1 root root 0 Aug 11 14:26 f0811 -rw-r--r-- 1 root root 0 Aug 12 14:25 f0812 -rw-r--r-- 1 root root 0 Aug 13 14:25 f0813 -- 查找 /find 下修改时间在 24 小时 (1 天 ) 之内的文件 [ find]# find . -mtime -1 . ./f0813 -- 查找 /find 下修改时间在 2 天前的普通文件 [ find]# find . -type f -mtime +1 ./f0810 ./f0811 4 )按照用户和组查找 -user 用户名 -group 组名 -uid uid -gid gid -nouser :孤儿文件 没有所有者的文件 -nogroup :没有所属组的文件 -- 查找系统中所有者是 quota2 的文件 [ home]# find / -user quota2 -type f [ home]# find / -user quota2 -type f 2>/dev/null -- 查找系统中的孤儿文件 [ home]# userdel quota2 [ home]# find . -type f -nouser ./quota2/.bash_history ./quota2/.bashrc ./quota2/.bash_profile ./quota2/.bash_logout [ home]# ll `find . -type f -nouser` -rw------- 1 502 quota2 29 Aug 11 10:16 ./quota2/.bash_history -rw-r--r-- 1 502 quota2 18 Aug 29 2012 ./quota2/.bash_logout -rw-r--r-- 1 502 quota2 176 Aug 29 2012 ./quota2/.bash_profile -rw-r--r-- 1 502 quota2 124 Aug 29 2012 ./quota2/.bashrc -- 查找系统中所有者不是 root 的普通文件 ! 或者 -not [ home]# find / ! -user root -type f 或者 -or 或者 -o -- 查找系统中所有者不是 root 或者类型是套接字的文件 [ home]# find / ! -user root -o -type s 5 )按照权限查找 -perm +222 或者 (用户可写 or 组可写 or 其他人可写) 二进制中有 1 的位置,只要满足其中一个位就可以 -222 并且 (用户可写 and 组可写 and 其他人可写) 二进制中有 1 的位置必须都要有 1 # rm -f /find/* # cd /find/ [ find]# touch p{r,w,x}_{1,2,3} [ find]# chmod 400 pr_1 [ find]# chmod 440 pr_2 [ find]# chmod 444 pr_3 [ find]# chmod 200 pw_1 [ find]# chmod 220 pw_2 [ find]# chmod 222 pw_3 [ find]# chmod 100 px_1 [ find]# chmod 110 px_2 [ find]# chmod 111 px_3 [ find]# ll `find ./ -perm +020 -type f` --w--w---- 1 root root 0 Aug 13 15:13 ./pw_2 --w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3 [ find]# ll `find ./ -perm -020 -type f` --w--w---- 1 root root 0 Aug 13 15:13 ./pw_2 --w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3 当权限位只有一位的时候, + 和 - 是一样的。 [ find]# ll `find ./ -perm -222 -type f` --w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3 [ find]# ll `find ./ -perm +222 -type f` --w------- 1 root root 0 Aug 13 15:13 ./pw_1 --w--w---- 1 root root 0 Aug 13 15:13 ./pw_2 --w--w--w- 1 root root 0 Aug 13 15:13 ./pw_3 思考:查找系统中拥有 suid 权限的文件 6 )按照文件大小查找 -size + 大于 - 小于 直接数字 等于 ‘b’ for 512-byte blocks (this is the default if no suffix is used) //0.5KB ‘c’ for bytes ‘w’ for two-byte words ‘k’ for Kilobytes (units of 1024 bytes) ‘M’ for Megabytes (units of 1048576 bytes) ‘G’ for Gigabytes (units of 1073741824 bytes) [ find]# rm -f /find/* [ find]# dd if=/dev/zero of=f1M bs=1M count=1 [ find]# dd if=/dev/zero of=f2M bs=1M count=2 [ find]# dd if=/dev/zero of=f3M bs=1M count=3 [ find]# dd if=/dev/zero of=f4M bs=1M count=4 [ find]# find . -type f -size -3M ./f2M ./f1M [ find]# find . -type f -size 3M ./f3M [ find]# find . -type f -size +3M ./f4M 3 、动作 -exec 动作 -- 找到结果之后直接执行动作 -ok 动作 -- 执行动作之前先提示,即需要交互 [ find]# find . -type f -size +3M -exec ls -l {} \; -rw-r--r-- 1 root root 4194304 Aug 13 15:51 ./f4M {} —— 用来代替找到的结果 \; —— 表示结束标志 [ find]# find . -type f -size +3M -ok ls -l {} \; < ls ... ./f4M > ? y -rw-r--r-- 1 root root 4194304 Aug 13 15:51 ./f4M [ find]# find . -type f -size +3M -ok ls -l {} \; < ls ... ./f4M > ? n 练习: 1 、查找 /find 目录下,类型是 普通文件的文件将其移动到 /test 目录下 [ find]# find . -type f -exec mv {} /test \; [ find]# ls /test/ f1M f2M f3M f4M 或者 [ find]# mv `find . -type f` /test 2 、查找 /test 目录下类型为普通文件的文件,对其进行备份,备份文件的后缀名为 .bak [ find]# find /test -type f -exec cp {} {}.bak \; [ find]# ls /test/ f1M f1M.bak f2M f2M.bak f3M f3M.bak f4M f4M.bak 3 、删除 /test 目录下修改时间在一天以内的普通文件 [ find]# find /test/ -type f -mtime -1 -exec rm {} \; grep 文本过滤 一 .grep :目的是过滤出用户感兴趣的内容 *** 语法: grep [ 选项 ] 模式或关键字 文件列表 简单例子: [ loring ~]# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 1 、 --color 带颜色显示匹配到的关键字 [ loring ~]# grep --color root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 2 、 -i 忽略大小写 [ loring tmp]# grep --color -i root /find/passwd Root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 3 、 -v 取反 过滤出不包含 nologin 的行 [ loring tmp]# grep -v nologin /etc/passwd 4 、 ^ 以某关键字开头 显示 /root/.bashrc 文件中的非注释行 [ loring tmp]# grep -v ^# /root/.bashrc 5 、 $ 以某关键字结尾 显示 passwd 文件中以 sh 结尾的行 [ loring tmp]# grep sh$ /etc/passwd 6 、 ^$ 空行 显示 /root/.bashrc 文件中的非注释行和非空行 [ loring tmp]# grep -v ^# /root/.bashrc | grep -v ^$ 7 、 -c count ,统计匹配到的行数 [ loring tmp]# grep -c root /etc/passwd 2 8 、 -l 一般和 -r 联用,只显示包含关键字的文件的名字,而不是显示文件内容 9 、 -r 递归检索 显示 test 目录下文件内容中含有 root 的文件名 [ loring tmp]# grep -rl root /test 10 、 -q quiet 静默输出 一般在写脚本时候用 [ loring tmp]# grep -q root /etc/passwd [ loring tmp]# echo $? //$? 表示上一条命令的执行结果 0 返回结果为 0 :表示上一条命令的执行时成功的 返回结果非 0 :表示上一条命令执行失败 [ loring tmp]# grep -q jsjdjjdfhfh /etc/passwd [ loring tmp]# echo $? 1 [ loring tmp]# grep -q root /asdaf grep: /asdaf: No such file or directory [ loring tmp]# echo $? // 文件不存在返回 2 2 11 、 -n 显示匹配行的行号 [ loring tmp]# grep -n root /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin 1 、显示 /etc/group 文件中含有 root 的行 [ loring test]# grep root /etc/group 2 、显示 /etc/passwd 文件中以 rp 开头的行 [ loring test]# grep ^rp /etc/passwd 3 、显示 /etc/group 文件中不以 : 为结尾的行 [ loring test]# grep -v :$ /etc/group 4 、显示 /etc/rc.local 文件中的空行,要求带行号 [ loring test]# grep -n ^$ /etc/rc.local 6: 5 、显示仅 /mnt/cdrom 目录下的文件类型为目录的文件 ( 前提是光盘已挂载,且不使用 find) [ loring test]# ll /mnt/cdrom/ | grep ^d 二、 cut :就是截取的意思,它的处理对象是 “ 一行 ” 文本,可以从中选取出用户所需要的部分,不影响原文件 可以指定分隔符,然后打印出以分隔符隔开的具体某一列或某几列 语法: cut -f 指定的列 -d ‘ 分隔符 ’ -d :指定字段分隔符 -f :指定要输出的区域,多个之间用逗号分隔 -c :指定列的字符 显示 /etc/passwd 文件中的用户名和 uid 字段 [ loring test]# cut -d: -f1,3 /etc/passwd [ loring test]# head -5 /etc/passwd | cut -d: -f1,3 root:0 bin:1 daemon:2 adm:3 lp:4 [ loring test]# head -5 /etc/passwd > t1 [ loring test]# vim t1 :%s/:/ /g [ loring test]# cat t1 root x 0 0 root /root /bin/bash bin x 1 1 bin /bin /sbin/nologin daemon x 2 2 daemon /sbin /sbin/nologin adm x 3 4 adm /var/adm /sbin/nologin lp x 4 7 lp /var/spool/lpd /sbin/nologin 以空格为分隔,取出第一个字段 [ loring test]# cut -d" " -f1 t1 root bin daemon adm lp 取出 /etc/group 文件中组名的字段 cut -d: -f1 /etc/group 打印 /etc/passwd 文件中每行的第 1-5 个字符,以及第 7-10 个字符的内容 [root@web test]# cat /etc/passwd | cut -c 1-5,7-10 三、 sort 排序 -t :指定字段分隔符 -k :指定第几个字段 -n :按照数字顺序排序 -r :反向排序 reverse -u :排序后重复行只打印一次 unique [root@web test]# cat sort.txt b:3 c:2 a:4 e:5 d:1 f:11 对输出内容直接排序,默认按照每行的第一个字符进行排序 [root@web test]# cat sort.txt | sort a:4 b:3 c:2 d:1 e:5 f:11 对输出内容进行反向排序 [root@web test]# cat sort.txt | sort -r f:11 e:5 d:1 c:2 b:3 a:4 使用 “ : ” 做分隔符,对第 2 个字段进行排序 [root@web test]# cat sort.txt | sort -t ":" -k 2 d:1 f:11 c:2 b:3 a:4 e:5 使用 “ : ” 做分隔符,对第 2 个字段进行排序,按照数字大小排序 [root@web test]# cat sort.txt | sort -t ":" -k 2 -n d:1 c:2 b:3 a:4 e:5 f:11 对 /etc/passwd 文件按照 uid 来排序 [ loring test]# sort -t ":" -k 3 -n /etc/passwd 对 passwd 文件按照 uid 由大到小的顺序排序 [ loring test]# sort -t ":" -k 3 -nr /etc/passwd 按照 gid 由小到大的顺序打印 /etc/group [ loring test]# sort -t: -k 3 -n /etc/group 四、 uniq 去重,唯一 去除相邻重复行 -c : 显示重复的行数 -i: 忽略大小写 [ loring test]# uniq num.txt 111 222 333 444 222 555 使用 uniq 时,一般先排序,再去重 [ loring test]# sort num.txt | uniq 111 222 333 444 555 [ loring test]# sort num.txt | uniq -c 1 111 3 222 2 333 1 444 1 555 五、 tr 主要作用在于文本转换或者删除。 将 /etc/passwd 文件中的小写字母转换成大写字母 [root@web test]# cat /etc/passwd | tr '[a-z]' '[A-Z]' 将 /etc/passwd 文件中的 “ : ” 删除掉 [root@web test]# cat /etc/passwd | tr -d ":" 六、 paste 文本合并。将文件按照行进行合并,中间使用 tab 隔开。 [root@web test]# cat a.txt 1 2 3 4 5 [root@web test]# cat b.txt a b c d e 按照行合并文件 [root@web test]# paste a.txt b.txt 1 a 2 b 3 c 4 d 5 e 也可以使用 -d 指定合并文件时行间的分隔符 [root@web test]# paste -d: a.txt b.txt 1:a 2:b 3:c 4:d 5:e 统计 /etc/passwd 文件中一共有几种 shell ,并显示每种 shell 有几个用户 [ loring test]# cut -d: -f7 /etc/passw转载地址:http://owxvb.baihongyu.com/