1. 程式人生 > >Linux運維工程師必備(命令)

Linux運維工程師必備(命令)

linux 運維 工程師

  1. 如何過濾出以下目錄的中的所有一級目錄

[[email protected] ~]# 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
[[email protected] ~]# 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:通過給目錄加標識,然後通過過濾標識,過濾出目錄

[[email protected] ~]# ls -F   #把目錄結尾加上"/",便於區分和過濾出來,ls -p也可以讓目錄帶上斜線
111  222  333  aaa/  bbb/  ccc/  ddd/
[[email protected] ~]# ls -F|grep /$  #把包含斜線"/"的過濾出來就是目錄
aaa/
bbb/
ccc/
ddd/

方法3:find的應用

[[email protected] ~]# find . -type d    #.代表當前目錄
./aaa
./ccc
./bbb
./ddd

方法4:tree

[[email protected] ~]# tree -dl
.
|-- aaa
|-- bbb
|-- ccc
`-- ddd

過濾空行及#開頭的行

[[email protected] ~]# grep -Ev "^#|^$" /etc/init.d/sshd 
[[email protected] ~]# egrep -v "^#|^$" /etc/init.d/sshd 
註釋:
-E  #egrep
-v  #過濾
^#  #井號開頭
^$  #空行

方法5:sed

[[email protected] ~]# 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

[[email protected] ~]#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.假如當前目錄是

[[email protected] ~]# pwd   #這是打印當前目錄
/root

現在因為需要進入到了/tmp目錄下進行操作,執行的命令如下:

[[email protected] ~]# pwd
/root
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# cd -
/root
[[email protected] ~]#

原理:

[[email protected] ~]#  env|grep -i oldpwd   #是這個變量記錄著上一次的目錄
OLDPWD=/tmp


3.一個目錄中有很多文件(ls查看時好多屏),想最快速度查看到最近更行的文件。如果看?

解答:

[[email protected] 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服務)

[[email protected] ~]# 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

[[email protected] ~]# 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的更新,如何做?

法一:

[[email protected] ~]# 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

法二:

[[email protected] ~]# 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

法三:

[[email protected] ~]# 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:

[[email protected] 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:

[[email protected] 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:

[[email protected] 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

Linux運維工程師必備(命令)