findコマンド検索の構文を処理し、[テキスト

A、検索コマンドを見つけます

  findコマンドは、指定された条件でファイルを検索するために使用されます。システムワークでfindコマンドが不可欠であるとき、私たちは一般的に他のすべてのfindコマンドは、ファイルの削除や他の操作を見つけるために配合することができる見つけるために、findコマンドを使用して、ディレクトリ内のファイルを見つけます。彼は、画面に表示されるデフォルト情報の成功を一致、条件検索など、さまざまなファイルのプロパティ(ファイル名、サイズ、更新時刻、アクセス権及びその他の情報)を使用することができます。

フォーマット:

検索[ 検索パスを]動作条件を探します

一般的なパラメータ:

-名前の名は、- INAME名:ファイル名は、ファイルの名前と一致します。INAMEの場合無視
 - パーマ     マッチング権限(モード完全一致、- モードが含まれている)
  
ファイル権限見つけるため-perm 444#
  -perm -444# -表現すると、ファイルのアクセス許可を見つけるには、Uは許可をR、及び「G」ビットrはRビットファイルoを特権と権限を持っている
  -perm / 444#/または表現; RビットファイルoをR、Gまたは権利、または許可ビットのルックアップファイルのパーミッションをuは許可ビットをR、
  -perm / 777 #777 = RWX RWX RWXすなわち、条件のいずれかに9が満たされます
- ユーザー    マッチ所有者
 - NOUSERマッチングファイル所有されていない
 - グループ    マッチすべてのグループ
 - すべてのグループのnogroupが一致するファイル
 -のmtime - nは、+ N(時間の内容と一致する- n個のn日以内手段、+ n個の手段のn日前)
 - atimeの- nは、+ nはファイルにマッチタイムのアクセスが(- n個のn日以内を指し、+ n)の前のn日を指し
 - ctimeを- nは、+ n個の変更、ファイルのパーミッションの時間と一致する(- n個のn日以内を指し、+ nはn日を指し前)
 - アミンN:過去n分に読み込まれた
 - CminのN:最後のn分以内に修正されました
 -サイズN:ファイルのサイズと一致する(50キロバイトルックアップファイルに50キロバイトを、+ 50キロバイトを超える50キロバイトビット・ルックアップ・ファイルは、- 50キロバイトルックアップファイルが少ない50キロバイト未満です)。ファイルサイズがn個のB代表512個のバイトのブロック、cが2バイトであり、wは、意味キロバイトをK、文字の数を表します。
- タイプ指定されたタイプCの、Cは、ファイルのファイルタイプです。
    D:ディレクトリファイル
    C:キャラクタデバイス
    B:デバイスファイル
    P:パイプライン
    F:一般的なファイル
    L:シンボリックリンクファイル
-maxdepth:ディレクトリ階層最大深度を探す
-mindepth:最小深さを探す
- 新しいが、古いファイルよりも試合F1、F2、F1、F2よりも新しい - プルーンは、ディレクトリを無視 - Execのコマンド{} \;後次に、コマンドの実行 - OKコマンド{} \;実行命令を受信した後、かどうかのコマンドの実行前に実行される
-aおよび(満たすために同時に)
-oまたは(のみのいずれかの条件を満足することができます)

例:

1)カレントディレクトリの最後の20日以内に更新されたファイルを探します

見つけます。-ctime -20

2)第七普通のファイルまでの時間を変更し、削除する前に依頼するの/ var / logディレクトリを探します

# find /var/log -type f -mtime +7 -ok rm {} \;

3)查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件(只匹配644)

# find . -type f -perm 644 -exec ls -l {} \;

4)模糊匹配644权限(包含644即可)

# find . -type f -perm -644 -exec ls -l {} \;

5)查找系统中所有文件长度为0的普通文件,并列出它们的完整路径

# find . -type f -size 0 -exec ls -l {} \;

6)-exec执行命令

# 删除没有属主的用户
# find / -nouser -exec rm -rf {} \; 

# 删除查找的文件
# find /home -name "*log" | xargs rm -rf {} \;

7)-a和-o参数使用

# 找到所有权限是644的普通文件
# find /usr/local/ -perm 644 -a -type f | head

# 找到以a开头或以a结尾的普通文件
# find . -name "a*" -o -name "*a" -type f

 PS:使用-a的话则需要同时满足

8)查询属主、属组

# 查询属主为root的文件
# find /etc/ -user root -type f | head -n 3 | xargs ls -l;

# 查询属组为root的文件
# find /etc/ -group root -type f | head -n 3 | xargs ls -l;

# 查询没有属主、属组的文件
# find /etc/ -nouser -type f | head -n 3 | xargs ls -l;
# find /etc/ -nogroup | head -n 3 | xargs ls -l;

9)在整个文件系统中找出归属于thy用户的文件复到指定目录

# 模拟一个用户,将备份的passwd文件的属主和属组改为thy
# useradd thy
# chown thy.thy /tmp/passwd 
# ll /tmp/passwd 
-rw-r--r-- 1 thy thy 1708 Nov  4 09:56 /tmp/passwd

# 使用find命令将属主为thy的文件复制到指定目录
# find / -user thy -type f -exec cp -a {} /tmp/thy/ \;

10)综合案例:按文件所有者和所有组查找 

# 创建文件
[root@localhost ~]# cd /tmp/test
[root@localhost test]# touch file{1..5}
[root@localhost test]# ls
file1  file2  file3  file4  file5 

# 实时监听这个目录(可以另外开一个shell窗口)
[root@VM_0_10_centos ~]# watch -n 1 ls -l /tmp/test/

# 添加用户
[root@VM_0_10_centos test]# useradd teacher
[root@VM_0_10_centos test]# useradd student
[root@VM_0_10_centos test]# id teacher
uid=1008(teacher) gid=1011(teacher) groups=1011(teacher)
[root@VM_0_10_centos test]# id student
uid=1009(student) gid=1012(student) groups=1012(student)
# 修改用户主和属组
[root@VM_0_10_centos test]# chown teacher.teacher /tmp/test/file1
# chown和chgrp修改属组效果是一样的
[root@VM_0_10_centos test]# chown .teacher /tmp/test/file2 
[root@VM_0_10_centos test]# chgrp student /tmp/test/file2 
[root@VM_0_10_centos test]# chown teacher.student /tmp/test/file3

# 按文件所有者查找
[root@VM_0_10_centos test]# find / -user teacher -type f
/home/teacher/.bashrc
/home/teacher/.bash_logout
/home/teacher/.bash_profile
/var/spool/mail/teacher
/tmp/test/file3
/tmp/test/file1

# 按文件所有组查找
[root@VM_0_10_centos test]# find / -group teacher -xtype f
/home/teacher/.bashrc
/home/teacher/.bash_logout
/home/teacher/.bash_profile
/tmp/test/file1
[root@VM_0_10_centos test]# find / -group student -type f
/home/student/.bashrc
/home/student/.bash_logout
/home/student/.bash_profile
/tmp/test/file2
/tmp/test/file3

# 查找所有者为root,所属组为student的文件(默认表示且关系)
[root@VM_0_10_centos test]# find / -user root -group student -type f
/tmp/test/file2
等价于
[root@VM_0_10_centos test]# find / -user root -a -group student -type f
/tmp/test/file2

# 查找所有者为teacher或所属组为student的文件(表示或关系)
[root@VM_0_10_centos test]# find / -user teacher -o -group student -type f
/home/student/.bashrc
/home/student/.bash_logout
/home/student/.bash_profile
/home/teacher
/home/teacher/.bashrc
/home/teacher/.bash_logout
/home/teacher/.bash_profile
/var/spool/mail/teacher
/tmp/test/file2
/tmp/test/file3
/tmp/test/file1

# 查找所属者不是student的用户
[root@VM_0_10_centos test]# find /tmp/ -not -user root -type f -exec ls -l {} \;
-rw-r--r-- 1 thy thy 1708 Nov  4 09:56 /tmp/passwd
-rw-r--r-- 1 teacher student 0 Nov  5 11:16 /tmp/test/file3
-rw-r--r-- 1 teacher teacher 0 Nov  5 11:16 /tmp/test/file1

 11)按文件所在深度(层次)查找

# 最大深度
[root@VM_0_10_centos test]# find /etc/ -maxdepth 2 | head -n 5
/etc/
/etc/group
/etc/rc6.d
/etc/audit
/etc/audit/audit.rules

# 最小深度
[root@VM_0_10_centos test]# find /etc/ -maxdepth 1 | head -n 5
/etc/
/etc/group
/etc/rc6.d
/etc/audit
/etc/mailcap

# 查找/etc目录下最少层次为1最多层次为2的以.conf结尾的文件(这里将最大和最小深度的位置对换就会查不出结果)
[root@VM_0_10_centos test]# find /etc/ -mindepth 1 -maxdepth 2 -name *.conf | head -n 3
/etc/audit/auditd.conf
/etc/sysctl.conf
/etc/depmod.d/kvdo.conf

12)对查找到的文件执行某些动作

# 对查询到的文件进行分权限操作
[root@VM_0_10_centos test]# find /tmp/test/ -perm 644 -exec chmod g+w {} \;
[root@VM_0_10_centos test]# ll
total 0
-rw-rw-r-- 1 teacher teacher 0 Nov  5 11:16 file1
-rw-rw-r-- 1 root    student 0 Nov  5 11:16 file2

# 对查询的文件进行备份
[root@VM_0_10_centos test]# find /tmp/test/ -type f -exec cp {} /tmp/test/test/  \;
cp: ‘/tmp/test/test/file2’ and ‘/tmp/test/test/file2’ are the same file
cp: ‘/tmp/test/test/file3’ and ‘/tmp/test/test/file3’ are the same file
cp: ‘/tmp/test/test/file5’ and ‘/tmp/test/test/file5’ are the same file

 

 

 

 

 

 

 

おすすめ

転載: www.cnblogs.com/HeiDi-BoKe/p/11792477.html