今天又有人問我linux下查找檔案的一些問題,幹脆直接幫他貼個老文章出來:
find常用示例
·find path -option [ -print ] [ -exec -ok command ] {} /;
#-print 将查找到的檔案輸出到标準輸出
#-exec command {} /; -----将查到的檔案執行command操作,{} 和 /;之間有空格
#-ok 和-exec相同,隻不過在操作前要詢使用者
====================================================
-name filename #查找名為filename的檔案
-perm #按執行權限來查找
-user username #按檔案屬主來查找
-group groupname #按組來查找
-mtime -n +n #按檔案更改時間來查找檔案,-n指n天以内,+n指n天以前
-atime -n +n #按檔案通路時間來查GIN: 0px">-perm #按執行權限來查找
-atime -n +n #按檔案通路時間來查找檔案,-n指n天以内,+n指n天以前
-ctime -n +n #按檔案建立時間來查找檔案,-n指n天以内,+n指n天以前
-nogroup #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在
-nouser #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存
-newer f1 !f2 找檔案,-n指n天以内,+n指n天以前
-newer f1 !f2 #查更改時間比f1新但比f2舊的檔案
-type b/d/c/p/l/f #查是塊裝置、目錄、字元裝置、管道、符号連結、普通檔案
-size n[c] #查長度為n塊[或n位元組]的檔案
-depth #使查找在進入子目錄前先行查找完本目錄
-fstype #查更改時間比f1新但比f2舊的檔案
-fstype #查位于某一類型檔案系統中的檔案,這些檔案系統類型通常可 在/etc/fstab中找到
-mount #查檔案時不跨越檔案系統mount點
-follow #如果遇到符号連結檔案,就跟蹤連結所指的檔案
-cpio %; #查位于某一類型檔案系統中的檔案,這些檔案系統類型通常可 在/etc/fstab中找到
-cpio #對比對的檔案使用cpio指令,将他們備份到錄音帶裝置中
-prune #忽略某個目錄
$find ~ -name "*.txt" -print #在$HOME中查.txt檔案并顯示
$find . -name "*.txt" -print
$find . -name "[A-Z]*" -pri26nbsp; #對比對的檔案使用cpio指令,将他們備份到錄音帶裝置中
=====================================================
$find . -name "[A-Z]*" -print #查以大寫字母開頭的檔案
$find /etc -name "host*" -print #查以host開頭的檔案
$find . -name "[a-z][a-z][0--9][0--9].txt" -print #查以兩個小寫字母和兩個數字開頭的txt檔案
$find . -perm 755 -print
$find . -perm -007 -exec ls -l {} /; #查所有使用者都可讀寫執行的檔案同-perm 777
$find . -type d -print
$find . ! -type d -print
$find . -type l -print
$find . -size +1000000c -print #查長度大于1Mb的檔案
$find . -size 100c -print # 查長度為100c的檔案
$find . -size +10 -print #查長度超過期廢棄10塊的檔案(1塊=512位元組)
$cd /
$find etc home apps -depth -print | cpio -ivcdC65536 -o /dev/rmt0
$find /etc -name "passwd*" -exec grep "cnscn" {} /; #看是否存在cnscn使用者
$find . -name "yao*" | xargs file
$find . -name "yao*" | xargs echo "" > /tmp/core.log
$find . -name "yao*" | xargs chmod o-w
======================================================
find -name april* 在目前目錄下查找以april開始的檔案
find -name april* fprint file 在目前目錄下查找以april開始的檔案,并把結果輸出到file中
find -name ap* -o -name may* 查找以ap或may開頭的檔案
find /mnt -name tom.txt -ftype vfat 在/mnt下查找名稱為tom.txt且檔案系統類型為vfat的檔案
find /mnt -name t.txt ! -ftype vfat 在/mnt下查找名稱為tom.txt且檔案系統類型不為vfat的檔案
find /tmp -name wa* -type l 在/tmp下查找名為wa開頭且類型為符号連結的檔案
find /home -mtime -2 在/home下查最近兩天内改動過的檔案
find /home -atime -1 查1天之内被存取過的檔案
find /home -mmin +60 在/home下查60分鐘前改動過的檔案
find /home -amin +30 查最近30分鐘前被存取過的檔案
find /home -newer tmp.txt 在/home下查更新時間比tmp.txt近的檔案或目錄
find /home -anewer tmp.txt 在/home下查存取時間比tmp.txt近的檔案或目錄
find /home -used -2 列出檔案或目錄被改動過之後,在2日内被存取過的檔案或目錄
find /home -user cnscn 列出/home目錄内屬于使用者cnscn的檔案或目錄
find /home -uid +501 列出/home目錄内使用者的識别碼大于501的檔案或目錄
find /home -group cnscn 列出/home内組為cnscn的檔案或目錄
find /home -gid 501 列出/home内組id為501的檔案或目錄
find /home -nouser 列出/home内不屬于本地使用者的檔案或目錄
find /home -nogroup 列出/home内不屬于本地組的檔案或目錄
find /home -name tmp.txt -maxdepth 4 列出/home内的tmp.txt 查時深度最多為3層
find /home -name tmp.txt -mindepth 3 從第2層開始查
find /home -empty 查找大小為0的檔案或空目錄
find /home -size +512k 查大于512k的檔案
find /home -size -512k 查小于512k的檔案
find /home -links +2 查硬連接配接數大于2的檔案或目錄
find /home -perm 0700 查權限為700的檔案或目錄
find /tmp -name tmp.txt -exec cat {} /;
find /tmp -name tmp.txt -ok rm {} /;
find / -amin -10 # 查找在系統中最後10分鐘通路的檔案
find / -atime -2 # 查找在系統中最後48小時通路的檔案
find / -empty # 查找在系統中為空的檔案或者檔案夾
find / -group cat # 查找在系統中屬于 groupcat的檔案
find / -mmin -5 # 查找在系統中最後5分鐘裡修改過的檔案
find / -mtime -1 #查找在系統中最後24小時裡修改過的檔案
find / -nouser #查找在系統中屬于廢棄使用者的檔案
find / -user fred #查找在系統中屬于FRED這個使用者的檔案
查目前目錄下的所有普通檔案
# 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
查目前目錄下的所有普通檔案,并在- e x e c選項中使用ls -l指令将它們列出
=================================================
在/ l o g s目錄中查找更改時間在5日以前的檔案并删除它們:
$ find logs -type f -mtime +5 -exec -ok rm {} /;
查詢當天修改過的檔案
[root@book class]# find ./ -mtime -1 -type f -exec ls -l {} /;
查詢檔案并詢問是否要顯示
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} /;
< ls ... ./classDB.inc.php > ? y
-rw-r--r-- 1 cnscn cnscn 13709 1月 12 12:22 ./classDB.inc.php
< ls ... ./classDB.inc.php > ? n
[root@book class]#
查詢并交給awk去處理
[root@book class]# who | awk '{print $1"/t"$2}'
cnscn pts/0
awk---grep---sed
[root@book class]# df -k | awk '{print $1}' | grep -v 'none' | sed s"///dev////g"
檔案系統
sda2
sda1
[root@book class]# df -k | awk '{print $1}' | grep -v 'none'
/dev/sda2
/dev/sda1
1)在/tmp中查找所有的*.h,并在這些檔案中查找“SYSCALL_VECTOR",最後列印出所有包含"SYSCALL_VECTOR"的檔案名
A) find /tmp -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} /; -print
2)find / -name filename -exec rm -rf {} /;
find / -name filename -ok rm -rf {} /;
3)比如要查找磁盤中大于3M的檔案:
find . -size +3000k -exec ls -ld {} ;
4)将find出來的東西拷到另一個地方
find *.c -exec cp '{}' /tmp ';'
如果有特殊檔案,可以用cpio,也可以用這樣的文法:
find dir -name filename -print | cpio -pdv newdir
6)查找2004-11-30 16:36:37時更改過的檔案
# A=`find ./ -name "*php"` | ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37"
<a></a>
方法一:簡單實用
usermod -s /usr/lib/ssh/sftp-server user
方法二:安裝rssh
root使用者
1)./configure --prefix=/usr --sysconfdir=/etc
2)make
3) make install
4) sh mkchroot.sh /home/ftpuser/ftpdoc ftpuser 077
5) vi /etc/rssh.conf
allowscp
allowsftp
umask = 022
chrootpath = /home/ftpuser/ftpdoc
6)/etc/rc.d/init.d/sshd restart
這樣我們原來建立的ftpuser就隻能通過sftp來通路了。
通過設定rssh.conf,還可以給每個使用者分别建立通路規則
##################################
#test0:x:501:502::/home/test0:/usr/local/bin/rssh scp可, sftp可
#test1:x:502:503::/home/test1:/usr/local/bin/scpsh scp可, sftp不可
#test2:x:503:504::/home/test2:/usr/local/bin/sftpsh scp可, sftp不可
#testno:x:504:505::/home/testno:/sbin/nologin 都不可
###################################
方法三 scponly
1) 解壓縮安裝包
2) 安裝
cd scponly-4.6
./configure --enable-chrooted-binary
make
make install
3 )增加使用者
make jail
Username to install [scponly] #輸入登入系統的使用者名:jack
home directory you wish to set for this user [/home/jack] #使用者目錄預設為/home/jack
name of the writeable subdirectory [incoming] #輸入使用者目錄下能讀寫的目錄
New password: #輸入使用者密碼
Retype new password: #确認使用者密碼
用 rpm 安裝 rssh,預設的幾個比較重要的檔案應該是安裝在
/usr/bin/rssh
/usr/libexec/rssh_chroot_helper
/usr/share/doc/rssh-2.3.2/mkchroot.sh
/etc/rssh.conf
或者您自己編譯源碼:
./configure --prefix=/usr --sysconfdir=/etc
修改使用者的 shell 為 /usr/bin/rssh , 這樣這個使用者就不能 ssh 登入了
usermod -s /usr/bin/rssh test
然後建立使用者 sftp 的 chroot 目錄。
給對應的 test 帳号設定一個 chroot 目錄的指令:
/usr/libexec/rssh_chroot_helper /home/test test 0755
最後修改 /etc/rssh.conf:
首先确認 allowsftp 被設定
再添加一行配置:user=test:077:00010:/home/rssh_chroot/test
077 是給 sftp 上來的使用者配置設定預設 umask
00010 是 access bits, 倒數第二個 '1' 表示給該使用者 sftp 權限