linux管理コマンド6-cut、grep、find
カットって『切る』じゃないの?間違いない!このコマンドはメッセージの一部を『切る』ことができます~処理するメッセージは『行』単位ですよ!次にお話しします.
[root@linux~]#cut-d'区切り文字'-f fields[root@linux ~]#
Cut-c文字区間
パラメータ:
-d:後に区切り文字を付けます.-fとともに使用する.
-f:-dの区切り文字に基づいてメッセージを数段に分割し、-fで数段目の意味を取り出す.
-c:固定文字区間を文字(characters)単位で取り出す.
例1:PATH変数を取り出し、3番目のパスを見つけます.
[root@linux ~]# echo $PATH
/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games:
[root@linux~]#echo$PATH|cut-d':'-f 5そうすると、/usr/local/binというディレクトリ名が表示されます!区切り記号として、5つ目は/usr/local/binですからね.
では、3位と5位をリストアップしたいなら?このようにして、[root@linux ~]# echo $PATH | cut -d ':' -f 3,5
例2:exportから出力されたメッセージを12文字目以降のすべての文字列を取得する[root@linux ~]# export declare -x HISTSIZE="1000"declare -x INPUTRC="/etc/inputrc"declare -x KDEDIR="/usr"declare -x LANG="zh_TW.big5"......その他省略…[root@linux ~]# export | cut -c 12- HISTSIZE="1000"INPUTRC="/etc/inputrc"KDEDIR="/usr"LANG="zh_TW.big5"......その他省略…
何があったか知ってるでしょ?-cで比較フォーマットの出力データを処理できます!12-20番目の文字、cut-c 12-20など、範囲の値を指定することもできます.
例3:lastを用いて今月登録者の情報に利用者名のみを残す[root@linux ~]# last vbird tty1 192.168.1.28 Mon Aug 15 11:55 - 17:48 (05:53) vbird tty1 192.168.1.28 Mon Aug 15 10:17 - 11:54 (01:37) [root@linux ~]# last | cut -d ' ' -f 1
#lastでは、最近1ヶ月でホストにログインしたユーザー情報を取得できますが、スペース記号の間隔を利用して、最初の情報を取り出すことができます.ユーザーアカウントですよ.しかし、vbird tty 1の間にスペースがいくつもあるので、一つだけではないので、tty 1を見つけるにはcut-d'-f 1,2はできませんよ.出力の結果は私たちが望んでいるものではありません.
このcutは本当に使いやすいですね.しかし、本当のことを言うと、logファイルを分析していない限り、cutを使用する機会は多くありません.よし!Cutの主な用途は『同じ行の中のデータを分解!』にあり、最もよく使われるのは、いくつかのデータや文字データを分析するときです.これは、いくつかの文字を分割のパラメータとして使用し、必要なデータを取得するためにデータを切断する場合があるからです.私もこの機能をよく使いますね.特にロゴファイルを分析するとき!ただし、cutは、複数のスペースで接続されたデータを処理する場合、比較的骨が折れる可能性があります~
2、grep
さっきのcutは1行のメッセージの中から、ある部分を取り出して私たちが望んでいるもので、grepは1行のメッセージを分析して、もし中に私たちが必要とする情報があれば、その行を出します~簡単な文法はこのようです:[root@linux~]#grep[-acinv]'検索文字列'filename
パラメータ:-a:binaryファイルをtextファイルでデータを検索-c:「検索文字列」を見つけた回数を計算-i:大文字と小文字の違いを無視するので、大文字と小文字は同じと見なします-n:ついでに行番号を出力-v:逆選択、すなわち「検索文字列」の内容がない行を表示します!
例1:lastの中にrootが現れた行を取り出す.[root@linux ~]# last | grep 'root'
例2:例1とは逆にrootがない限り取り出す![root@linux ~]# last | grep -v 'root'
例3:lastの出力メッセージではrootがあれば取り出し,1番目の欄のみをとる[root@linux~]#last|grep'root'|cut-d'-f 1#rootを取り出した後、前のコマンドcutの処理で、第1欄のみ取得できますよ!grepは素晴らしい指令ですよ!彼の支持する文法は本当に多すぎます~正规の表示法の中で使って、処理することができるデータは本当に多いです~しかし、私达のここは先に正规の表示法を话さないで~次の章は更に说明します~あなたは先に理解して、grepは1行の文字を解析することができて、キーワードを取得して、もしこの行にキーワードが存在するならば、行列を整えます!
3、find
:
find /( ) -name ' ' -type d
find , , 。 ( NFS),find , 。 find , , ( 30G )。
、find
1、find ;
find pathname -options [-print -exec -ok ...]
2、find ;
pathname:find 。 . , / 。 -print: find 。 -exec: find shell 。 'command' { } \;, { } \; 。 -ok: -exec , shell , , , 。
3、find
-name 。 -perm
。 -prune
find , -depth , -prune find 。 -user
。 -group
。 -mtime -n +n
, - n n ,+ n n 。find -atime -ctime , -m time 。 -nogroup
, /etc/groups 。 -nouser
, /etc/passwd 。 -newer file1 ! file2
file1 file2 。 -type , : b - 。 d - 。 c - 。 p - 。 l - 。 f - 。 -size n:[c] n , c 。 -depth: , , 。 -fstype: , /etc/fstab , 。 -mount: mount 。 -follow: find , 。 -cpio: cpio , 。 , :
-amin n N -atime n n*24 -cmin n N -ctime n n*24 -mmin n N -mtime n n*24 4、 exec ok shell
find , , exec find , -exec l s ls -l 。 。 rm , ls , 。
exec , { }, \, 。 exec , print 。 find , 。
: ls -l , ls -l find -exec # find . -type f -exec ls -l { } \;
-rw-r--r-- 1 root root 34928 2003-02-25./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25./conf/magic
-rw-r--r-- 1 root root 180 2003-02-25./conf.d/README
,find , -exec ls -l 。 /logs 5 : $ find logs -type f -mtime +5 -exec rm { } \;
: shell , , ! mv rm , -exec 。 。 , find .LOG 、 5 , , 。 $ find . -name "*.conf" -mtime +5 -ok rm{ } \;
< rm ... ./conf/httpd.conf > ? n
y , n 。
-exec 。 grep 。find “ passwd*” , passwd、passwd.old、passwd.bak, grep sam 。 # find /etc -name "passwd*" -exec grep"sam" { } \;
sam:x:501:501::/usr/sam:/bin/bash
、find ;
1、 :
$ find$HOME -print $ find ~ -print
2、 、 , ;
$ find. -type f -perm 644 -exec ls -l { } \;
3、 0 , ;
$ find/ -type f -size 0 -exec ls -l { } \;
4、 /var/logs 7 , ;
$ find/var/logs -type f -mtime +7 -ok rm { } \;
5、 root ;
$find .-group root -exec ls -l { } \; -rw-r--r-- 1 root root 595 10 3101:09 ./fie1
6、find 7 、 admin.log 。
, 999。 admin.log* ,
$ find. -name "admin.log[0-9][0-9][0-9]" -atime -7 -ok rm { } \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n
7、 ;
$ find. -type d | sort 8、 rmt ;
$ find/dev/rmt -print
、xargs
xargs -build and execute command lines from standard input
find -exec , find exec 。 exec , find , 。 “ ” “ ”。 xargs , find 。
find xargs , xargs , -exec 。 , , 。
, -exec , ; , , ;
xargs 。 , xargs , , , 。
xargs find , 。
, xargs
#find .-type f -print | xargs file ./.kde/Autostart/Autorun.desktop: UTF-8 UnicodeEnglish text
./.kde/Autostart/.directory: ISO-8859 text\
......
(core dump) , /tmp/core.log :
$ find/ -name "core" -print | xargs echo "" >/tmp/core.log
,
#find .-name "file*" -print | xargs echo "" > /temp/core.log # cat /temp/core.log
./file6
、 , :
# ls -l drwxrwxrwx 2 sam adm 4096 10 3020:14 file6 -rwxrwxrwx 2 sam adm 0 10 3101:01 http3.conf -rwxrwxrwx 2 sam adm 0 10 3101:01 httpd.conf # find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x 2 sam adm 4096 10 3020:14 file6 -rwxrwxr-x 2 sam adm 0 10 3101:01 http3.conf -rwxrwxr-x 2 sam adm 0 10 3101:01 httpd.conf
grep hostname :
# find. -type f -print | xargs grep "hostname" ./httpd1.conf:# different IP addresses orhostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want tomaintain multiple domains/hostnames
on your
grep hostnames :
# find. -name \* -type f -print | xargs grep "hostnames" ./httpd1.conf:# different IP addresses orhostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want tomaintain multiple domains/hostnames
on your
, , \ find * shell 。
find exec xargs 。
、find
find , , , , man find
1、 name
find , , 。
, 。
, $HOME *.txt , ~ 'pathname' , ~ $HOME 。
$ find~ -name "*.txt" -print ‘ *.txt’ , : $ find . -name "*.txt" -print
, :
$ find. -name "[A-Z]*" -print
/etc host , :
$ find/etc -name "host*" -print
$HOME , : $ find ~ -name "*" -print find .-print
, 。
$ find/ -name "*" -print
, , .txt , ax37.txt :
$find .-name "[a-z][a-z][0--9][0--9].txt" -print 2、 perm
-perm , 。 。
755 , 、 、 , 、 , : $ find . -perm 755 -print
: -, , -007 777,-006 666 # ls -l
-rwxrwxr-x 2 sam adm 0 10 3101:01 http3.conf -rw-rw-rw- 1 sam adm 34890 10 3100:57 httpd1.conf -rwxrwxr-x 2 sam adm 0 10 3101:01 httpd.conf drw-rw-rw- 2 gem group 4096 10 2619:48 sam -rw-rw-rw- 1 root root 2792 10 3120:19 temp # find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp
-permmode: mode
-perm+mode: mode
-perm-mode: mode
3、
, , -prune 。 -prune , -depth , -prune find 。
/apps , /apps/bin , :
$ find/apps -path "/apps/bin" -prune -o -print 4、 find
/usr/sam dir1 find /usr/sam -path "/usr/sam/dir1"-prune -o -print
find[-path ..] [expression] -path "/usr/sam" -prune -o -print -path"/usr/sam" -a -prune -o -print , -a -o , shell && || -path "/usr/sam" , -prune , -prune , ; -prune, 。 -path "/usr/sam" -a -prune , -print ,-print , ; -print, 。
if -path "/usr/sam" then
-prune
else
-print
find /usr/sam \( -path /usr/sam/dir1 -o -path/usr/sam/file1 \) -prune -o -print
。
\ , shell , find 。
,-name -o
#find/usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name"temp" -print
5、 user nouser
, $HOME sam , :
$ find~ -user sam -print
/etc uucp :
$ find/etc -user uucp -print
, -nouser 。 /etc/passwd 。 -nouser , ; find 。 , /home , :
$ find/home -nouser -print
6、 group nogroup
user nouser , , find , /apps gem , : $ find /apps -group gem -print
, nogroup 。 find $ find / -nogroup-print
7、 , mtime,atime ctime 。 , , mtime 。
- n , + n 。
5 , : $ find / -mtime -5 -print
/var/adm 3 , : $ find /var/adm -mtime +3 -print
8、
, -newer 。 : newest_file_name ! oldest_file_name
,! 。
sam temp :
:
-rw-r--r--1 sam adm 0 10 31 01:07 fiel -rw-rw-rw- 1 sam adm 34890 10 3100:57 httpd1.conf -rwxrwxr-x 2 sam adm 0 10 3101:01 httpd.conf drw-rw-rw- 2 gem group 4096 10 2619:48 sam -rw-rw-rw- 1 root root 2792 10 3120:19 temp # find -newer httpd1.conf ! -newer temp -ls
1077669 0 -rwxrwxr-x 2 sam adm 0 10 3101:01 ./httpd.conf 1077671 4 -rw-rw-rw- 1 root root 2792 10 3120:19 ./temp 1077673 0 -rw-r--r-- 1 sam adm 0 10 3101:07 ./fiel temp :
$ find. -newer temp -print
9、 type
/etc , :
$ find/etc -type d -print
, :
$ find. ! -type d -print /etc ,
$ find/etc -type l -print 10、 size
, (block) , 。 N c; 。
, , , 。 1 M :
$ find . -size +1000000c -print
/home/apache 100 :
$ find/home/apache -size 100c -print
10 ( 512 ):
$ find. -size +10 -print 11、 depth
find , , 。 depth find 。 , find , , 。
, find , CON.FILE 。 。 $ find / -name "CON.FILE" -depth-print
12、 mount ( ), find mount 。
XC :
$ find. -name "*.XC" -mount -print