如何过滤出以下目录的中的所有一级目录
[root@xcn ~]# ll
total 16
-rw-r--r-- 1 root root 0 Jul 5 11:43 111
-rw-r--r-- 1 root root 0 Jul 5 11:43 222
-rw-r--r-- 1 root root 0 Jul 5 11:43 333
drwxr-xr-x 2 root root 4096 Jul 5 11:43 aaa
drwxr-xr-x 2 root root 4096 Jul 5 11:43 bbb
drwxr-xr-x 2 root root 4096 Jul 5 11:43 ccc
drwxr-xr-x 2 root root 4096 Jul 5 11:43 ddd
方法1:ls -l结果中以d开头的就是目录
通过过滤以长格式输出的开头文件标识d [root@xcn ~]# ls -l |grep ^d drwxr-xr-x 2 root root 4096 Jul 5 11:43 aaa drwxr-xr-x 2 root root 4096 Jul 5 11:43 bbb drwxr-xr-x 2 root root 4096 Jul 5 11:43 ccc drwxr-xr-x 2 root root 4096 Jul 5 11:43 ddd 提示:^ :以...开头
方法2:通过给目录加标识,然后通过过滤标识,过滤出目录
[root@xcn ~]# ls -F #把目录结尾加上"/",便于区分和过滤出来,ls -p也可以让目录带上斜线 111 222 333 aaa/ bbb/ ccc/ ddd/ [root@xcn ~]# ls -F|grep /$ #把包含斜线"/"的过滤出来就是目录 aaa/ bbb/ ccc/ ddd/
方法3:find的应用
[root@xcn ~]# find . -type d #.代表当前目录 ./aaa ./ccc ./bbb ./ddd
方法4:tree
[root@xcn ~]# tree -dl . |-- aaa |-- bbb |-- ccc `-- ddd
过滤空行及#开头的行
[root@xcn ~]# grep -Ev "^#|^$" /etc/init.d/sshd [root@xcn ~]# egrep -v "^#|^$" /etc/init.d/sshd 注释: -E #egrep -v #过滤 ^# #井号开头 ^$ #空行
方法5:sed
[root@xcn ~]# ls -l|sed -n /^d/p drwxr-xr-x 2 root root 4096 Jul 5 11:43 aaa drwxr-xr-x 2 root root 4096 Jul 5 11:43 bbb drwxr-xr-x 2 root root 4096 Jul 5 11:43 ccc drwxr-xr-x 2 root root 4096 Jul 5 11:43 ddd 注释: -n #拒绝默认输出 p #打印
方法6:awk
[root@xcn ~]#ls -l|awk ‘/^d/ {print $0}‘ drwxr-xr-x 2 root root 4096 Jul 5 11:43 aaa drwxr-xr-x 2 root root 4096 Jul 5 11:43 bbb drwxr-xr-x 2 root root 4096 Jul 5 11:43 ccc drwxr-xr-x 2 root root 4096 Jul 5 11:43 ddd
2.假如当前目录是
[root@xcn ~]# pwd #这是打印当前目录 /root
现在因为需要进入到了/tmp目录下进行操作,执行的命令如下:
[root@xcn ~]# pwd /root [root@xcn ~]# cd /tmp/ [root@xcn tmp]# cd - /root [root@xcn ~]#
原理:
[root@xcn ~]# env|grep -i oldpwd #是这个变量记录着上一次的目录 OLDPWD=/tmp
3.一个目录中有很多文件(ls查看时好多屏),想最快速度查看到最近更行的文件。如果看?
解答:
[root@xcn etc]# ls -lrt #执行这个命令后最新的文件会在最下面 #以长格式按照修改时间倒序文件和目录,这样最近创建或修改的文件或目录 -t #按修改时间 -r #翻转
4.在配置apache时,执行了./confiure --prefix=/application/apache2.2.17 来编译apache在make installl完成后,希望用户的访问路径更简单,需要给/application/apache2.2.17目录做一个软链接/application/apache,使得内部开发或管理人员通过/application/apache就可以访问到/apache的安装目录/application/apache2.2.17下的内容,请你给出实现的命令(提示:apache为一个http web服务)
[root@xcn ~]# ln -s /application/apache2.2.17 /application/apache #-s 参数表示为创建软链接
5.已知apache服务的访问日志按天记录在服务器本地目录/data/logs下,由于磁盘空间紧张,现在要求只能保留最近7天访问日志!请问如何解决?请给出解决办法或配置或处理命令。
-rw-r--r-- 1 root root 0 Jun 28 00:00 2017-06-29_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jun 28 00:00 2017-06-29_00-00.backstage_error.log -rw-r--r-- 1 root root 0 Jun 29 00:00 2017-06-30_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jun 29 00:00 2017-06-30_00-00.backstage_error.log -rw-r--r-- 1 root root 0 Jun 30 00:00 2017-07-01_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jun 30 00:00 2017-07-01_00-00.backstage_error.log -rw-r--r-- 1 root root 0 Jul 1 00:00 2017-07-02_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jul 1 00:00 2017-07-02_00-00.backstage_error.log -rw-r--r-- 1 root root 0 Jul 2 00:00 2017-07-03_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jul 2 00:00 2017-07-03_00-00.backstage_error.log -rw-r--r-- 1 root root 0 Jul 3 00:00 2017-07-04_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jul 3 00:00 2017-07-04_00-00.backstage_error.log -rw-r--r-- 1 root root 0 Jul 4 00:00 2017-07-05_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jul 4 00:00 2017-07-05_00-00.backstage_error.log -rw-r--r-- 1 root root 0 Jul 5 00:00 2017-07-06_00-00.backstage_access.log -rw-r--r-- 1 root root 0 Jul 5 00:00 2017-07-06_00-00.backstage_error.log [root@guaji01 ~]# find ./ -type f -name "*.log" -mtime +7|xarge rm -f #删除7天
find 查找与时间有关参数:
-atime n #n为数字,意义为在n天之前的【一天之内】被access过的档案;
-ctime n #n为数字,意义为在n天之前的【一天之内】内change过状态的档案
-mtime n #n 为数字,意思为在n天之前的【一天之内】被modification过的档案
-newer file #file 为一个存在的档案,意思是说,只要档案比file还要新,就会被列出来
find ./ type f -name "*.log" -mtime +7|xargs rm -f
6.调试系统服务时,希望能实时查看系统日志/var/log/messages的更新,如何做?
法一:
[root@xcn ~]# tail -f /var/log/messages Jul 6 14:51:40 localhost yum[29341]: Installed: gnome-vfs2-devel-2.24.2-8.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: dmz-cursor-themes-0.4-4.el6.noarch Jul 6 14:51:41 localhost yum[29341]: Installed: system-gnome-theme-60.0.2-1.el6.noarch Jul 6 14:51:41 localhost yum[29341]: Installed: libgnome-2.28.0-11.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: libgnome-devel-2.28.0-11.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: libbonoboui-2.24.2-3.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libbonoboui-devel-2.24.2-3.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libgnomeui-2.24.1-4.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libgnomeui-devel-2.24.1-4.el6.x86_64 Jul 6 14:51:43 localhost yum[29341]: Updated: glib2-2.28.8-9.el6.i686
法二:
[root@xcn ~]# tail -F /var/log/messages Jul 6 14:51:40 localhost yum[29341]: Installed: gnome-vfs2-devel-2.24.2-8.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: dmz-cursor-themes-0.4-4.el6.noarch Jul 6 14:51:41 localhost yum[29341]: Installed: system-gnome-theme-60.0.2-1.el6.noarch Jul 6 14:51:41 localhost yum[29341]: Installed: libgnome-2.28.0-11.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: libgnome-devel-2.28.0-11.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: libbonoboui-2.24.2-3.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libbonoboui-devel-2.24.2-3.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libgnomeui-2.24.1-4.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libgnomeui-devel-2.24.1-4.el6.x86_64 Jul 6 14:51:43 localhost yum[29341]: Updated: glib2-2.28.8-9.el6.i68
法三:
[root@xcn ~]# tailf /var/log/messages Jul 6 14:51:40 localhost yum[29341]: Installed: gnome-vfs2-devel-2.24.2-8.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: dmz-cursor-themes-0.4-4.el6.noarch Jul 6 14:51:41 localhost yum[29341]: Installed: system-gnome-theme-60.0.2-1.el6.noarch Jul 6 14:51:41 localhost yum[29341]: Installed: libgnome-2.28.0-11.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: libgnome-devel-2.28.0-11.el6.x86_64 Jul 6 14:51:41 localhost yum[29341]: Installed: libbonoboui-2.24.2-3.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libbonoboui-devel-2.24.2-3.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libgnomeui-2.24.1-4.el6.x86_64 Jul 6 14:51:42 localhost yum[29341]: Installed: libgnomeui-devel-2.24.1-4.el6.x86_64 Jul 6 14:51:43 localhost yum[29341]: Updated: glib2-2.28.8-9.el6.i686
7.打印配置文件nginx.conf内容的行号及内容,该如何做?
法1:
[root@guaji01 conf]# nl nginx.conf 1 user nginx; 2 worker_processes 4; 3 4 #pid /var/run/nginx.pid; 5 6 events { 7 worker_connections 5000; 8 #use epoll; 9 } ....................
法2:
[root@guaji01 conf]# cat -n nginx.conf 1 user nginx; 2 worker_processes 4; 3 4 #pid /var/run/nginx.pid; 5 6 events { 7 worker_connections 5000; 8 #use epoll; 9 } 10 ........................
法3:
[root@guaji01 conf]# less -N nginx.conf 1 user nginx; 2 worker_processes 4; 3 4 #pid /var/run/nginx.pid; 5 6 events { 7 worker_connections 5000; 8 #use epoll; 9 } 10 11 http { 12 include mime.types; 13 default_type application/octet-stream; 14
法4:vi 文件 然后执行:set nu, :set nonu 为取消行号
本文出自 “小菜鸟” 博客,请务必保留此出处http://baishuchao.blog.51cto.com/12918589/1945114
原文地址:http://baishuchao.blog.51cto.com/12918589/1945114