Linux题目

1.删除某目录下所有文件,只保留指定文件。

  例:假设/abc文件夹下有a1、a2、...a10文件,保留a5和a6文件,其他全部删除

[root@localhost abc]# touch a{1..10}
[root@localhost abc]# ls
a1  a10  a2  a3  a4  a5  a6  a7  a8  a9

方法1:
[root@localhost abc]# rm -f `ls|egrep -v '(a5|a6)'`
[root@localhost abc]# ls
a5  a6

方法2:开启bash的extglob功能(此功能的作用就是用rm !(*jpg)这样的方式来删除不包括号内文件的文件)
[root@localhost abc]# shopt -s extglob  #开启extglob
[root@localhost abc]# rm -fr !(a5|a6)
[root@localhost abc]# ls
a5  a6
[root@localhost abc]# shopt -u extglob  #关闭extglob

 

2.chmod丢失x执行权限怎么处理?

[root@localhost bin]# chmod -x chmod
[root@localhost bin]# ls -l chmod
-rw-r--r--. 1 root root 50048 11月 22 2013 chmod

方法1:
[root@localhost bin]# /lib/ld-linux.so.2 /bin/chmod +x /bin/chmod

方法2:使用ACL获取
[root@localhost bin]# setfacl -m user:root:rwx /bin/chmod

方法3:偷权限
[root@localhost bin]# cp -p chown chown.bak
[root@localhost bin]# cat chmod > chown
[root@localhost bin]# chown +x /bin/chmod
[root@localhost bin]# rm -f chown
[root@localhost bin]# mv chown.bak chown

 

3.查看文件指定行内容

  例:查看/etc/passwd文件5-10行内容

方法1:
[root@localhost ~]# cat /etc/passwd -b|sed -n '5,10p'

方法2:(10-5=5,显示第5行+1=6)
[root@localhost ~]# cat /etc/passwd -b|head -10|tail -6
或
[root@localhost ~]# cat /etc/passwd -b|tail -n +5|head -6

 

 

 

 

 

 

 

 

 

 

 

 

 

[root@localhost abc]# touch a{1..10}[root@localhost abc]# lsa1  a10  a2  a3  a4  a5  a6  a7  a8  a9
方法1:使用find操作[root@localhost abc]# find . -type f ! -name "a10"|xargs rm -f[root@localhost abc]# lsa10
或:[root@localhost abc]# find . -type f ! -name "a10" -exec rm -f {} \;[root@localhost abc]# lsa10
方法2:开启bash的extglob功能(此功能的作用就是用rm !(*jpg)这样的方式来删除不包括号内文件的文件)[root@localhost abc]# shopt -s extglob[root@localhost abc]# rm -fr !(a10)[root@localhost abc]# lsa10[root@localhost abc]# shopt -u extglob

posted @ 2016-07-16 06:32  不浮不躁  阅读(193)  评论(0编辑  收藏  举报