VnLamp

Go Back   VnLamp > Linux Resources > Linux Tutorials, HOWTO's & Reference Material

Trả lời
 
LinkBack Ðiều Chỉnh Xếp Bài
10 cách dùng ‘find’
Old
  (#1 (permalink))
 
Status: Offline
Posts: 469
Tham gia: Dec 2007
^..^
 
hautp's Avatar
Default 10 cách dùng ‘find’ - 27-10-2008, 17:27 27-10-2008

Find là một lệnh cơ bản mà hầu như ai sử dụng Linux cũng biết. Nhưng không phải ai cũng khai thác hết công dụng của lệnh này. Sau đây là 10 cách dùng lệnh find một cách hữu ích. Ở đây tôi giả sử ta cần tìm kiếm trong thư mục $HOME.

1. Tìm những thư mục rỗng

find $HOME -depth -type d -empty

2. Tìm những file rỗng

find $HOME -depth -type f -empty

3. Tìm một file với tên xác định

find $HOME -name [name_of_file]

4. Tìm file với phần mở rộng xác định

find $HOME -name “*.[given_extension]”

5. Tìm file với những quyền truy cập xác định

find $HOME -perm [permission bits]

VD: Tìm những file .txt với quyền truy cập 644

find $HOME -name ‘*.txt’ -perm 644

6. Tìm file với một số bit permission biết trước, không quan tâm phần còn lại

find $HOME -perm -[permision_bits]

VD:

find $HOME -name ‘*.txt’ -perm -644

Lệnh này sẽ tìm những file với permission bit là 644 nhưng cũng có thể là 777 hay những giá trị “lớn hơn” 644.

7. Tìm file với kích thước xác định

find -size n[cwbkMG]

Phần trong [] là đơn vị tính kích thước. Lệnh trên sẽ tìm những file có kích thước từ (n-1) đến n.

‘b’ for 512-byte blocks (this is the default if no suffix is used)

‘c’ for bytes

‘w’ for two-byte words

‘k’ for Kilobytes (units of 1024 bytes)

‘M’ for Megabytes (units of 1048576 bytes)

‘G’ for Gigabytes (units of 1073741824 bytes)

VD:

find $HOME -name ‘*.txt’ -size 4k -exec ls -lh {} \;

Sẽ liệt kê những file có kích thước từ 3-4k có đuôi .txt.

Tùy chọn -exec sẽ thực thi những lệnh theo sau (ls).

8. Tìm file với tên xác định và phần mở rộng tùy ý

find -name ‘[given_name].*’

9. Tìm file được chỉnh sửa trong thời gian gần đây (theo từng block 24h)

find -mtime n

Với n có giá trị:

0 for the last 24 hours
1 for the last 48 hours
2 for the last 72 hours

và cứ vậy tính lên.

10. Tìm những file được truy cập trong thời gian gần đây (theo từng block 24h)

find -atime n

Với n tính như trên.

Tham khảo “man find” để biết thêm chi tiết.
   
Trả Lời Với Trích Dẫn
Old
  (#2 (permalink))
 
Status: Offline
Posts: 61
Tham gia: Dec 2007
Member
 
buffalo's Avatar
Thumbs down 28-10-2008, 17:28 28-10-2008

Thêm vài example với find command

Code:
sudo find / -type f -name *.jpg -exec cp {} . \;
find . -type f -size +10000 -exec ls -al {} \;
find . -atime +1 -type f -exec mv {} TMP \; # mv files older then 1 day to dir TMP
find . -name “-F” -exec rm {} \; # a script error created a file called -F
find . -exec grep -i “vds admin” {} \;
find . \! -name “*.Z” -exec compress -f {} \;
find . -type f \! -name “*.Z” \! -name “.comment” -print | tee -a /tmp/list
find . -name *.ini
find . -exec chmod 775 {} \;
find . -user xuser1 -exec chown -R user2 {} \;
find . -name ebtcom*
find . -name mkbook
find . -exec grep PW0 {} \;
find . -exec grep -i “pw0″ {} \;
find . -atime +6
find . -atime +6 -exec ll | more
find . -atime +6 -exec ll | more \;
find . -atime +6 -exec ll \;
find . -atime +6 -exec ls \;
find . -atime +30 -exec ls \;
find . -atime +30 -exec ls \; | wc -l
find . -name auth*
find . -exec grep -i plotme10 {};
find . -exec grep -i plotme10 {} \;
find . -ls -exec grep ‘PLOT_FORMAT 22′ {} \;
find . -print -exec grep ‘PLOT_FORMAT 22′ {} \;
find . -print -exec grep ‘PLOT_FORMAT’ {} \;
find . -print -exec grep ‘PLOT_FORMAT’ {} \;
find ./machbook -exec chown 184 {} \;
find . \! -name ‘*.Z’ -exec compress {} \;
find . \! -name “*.Z” -exec compress -f {} \;
find /raid/03c/ecn -xdev -type f -print
find /raid/03c/ecn -xdev -path -type f -print
find / -name .ssh* -print | tee -a ssh-stuff
find . -name “*font*”
find . -name hpmcad*
find . -name *fnt*
find . -name hp_mcad* -print
find . -grep Pld {} \;
find . -exec grep Pld {} \;
find . -exec grep Pld {} \;
find . -exec grep PENWIDTH {} \; | more
find . -name config.pro
find . -name config.pro
find /raid -type d “.local_sd_customize” -print
find /raid -type d -name “.local_sd_customize” -print
find /raid -type d -name “.local_sd_customize” -ok cp /raid/04d/MCAD-apps/I_Custom/SD_custom/site_sd_customize/user_filer_project_dirs {} \;
find /raid -type d -name “.local_sd_customize” -exec cp /raid/04d/MCAD-apps/I_Custom/SD_custom/site_sd_customize/user_filer_project_dirs {} \;
find . -name xeroxrelease
find . -exec grep xeroxrelease {} \;
find . -name xeroxrelease
find . -name xeroxrelease* -print 2>/dev/null
find . -name “*release*” 2>/dev/null
find / -name “*xerox*” 2>/dev/null
find . -exec grep -i xeroxrelease {} \;
find . -print -exec grep -i xeroxrelease {} \;
find . -print -exec grep -i xeroxrelease {} \; > xeroxrel.lis
find . -exec grep -i xeroxrel {} \;
find . -print -exec grep -i xeroxrel {} \;
find . -print -exec grep -i xeroxrel {} \; | more
find /raid/03c/inwork -xdev -type f -print >> /raid/04d/user_scripts/prt_list.tmp
find . -exec grep ‘31.53′ {} \;
find . -ls -exec grep “31/.53″ {} \; > this.lis
find . -print -exec grep “31/.53″ {} \; > this.lis
find . -print -exec grep 31.53 {} \; > this.lis
find . -exec grep -i pen {} /;
find . -exec grep -i pen {} \;
find . -print -exec grep -i pen {} \; | more
find . -exec grep -i pen {} \;
find . -atime +6 -exec ll | more \;
find . -atime +6 -exec ll \;
find . -atime +6 -exec ls \;
find . -atime +30 -exec ls \;
find . -atime +30 -exec ls \; | wc -l
find . \! -name ‘*.Z’ -exec compress -f {} \;
find . -name ‘cache*’ -depth -exec rm {} \;
find . -name ‘cache*’ -depth -print | tee -a /tmp/cachefiles
find . -name ‘cache[0-9][0-9]*’ -depth -print | tee -a /tmp/cachefiles
find . -name ‘hp_catfile’ ‘hp_catlock’ -depth -print | tee -a /tmp/hp.cats
find . -name ‘hp_catfile’ -name ‘hp_catlock’ -depth -print | tee -a /tmp/hp.cats
find . -name ‘hp_cat*’ -depth -print | tee -a /tmp/hp.cats
find . -name ‘hp_cat[fl]*’ -depth -print | tee -a /tmp/hp.cats
find /raid -name ‘hp_cat[fl]*’ -depth -print
find . \! -name ‘*.Z’ -exec compress -f {} \;
find . -name ‘*’ -exec compress -f {} \;
find . -xdev -name “wshp1*” -print
find . -xdev -name “wagoneer*” -print
find . -name “xcmd” -depth -print
find /usr/contrib/src -name “xcmd” -depth -print
find /raid -type d -name “.local_sd_customize” -exec ls {} \;
find /raid -type d -name “.local_sd_customize” \
-exec cp /raid/04d/MCAD-apps/I_Custom/SD_custom/site_sd_customize/user_filer_project_dirs {} \
;


HTML Code:
|_________VNLAMP_______|__\
|______________________|___||\*__
|______________________|___||""|""\___,
|______________________|___||""|""|___||
"([ (@)''(@)"""""""' ""(|*(@)(@)********(@)*
  Send a message via Yahoo to buffalo  
Trả Lời Với Trích Dẫn
Old
  (#3 (permalink))
 
Status: Offline
Posts: 469
Tham gia: Dec 2007
^..^
 
hautp's Avatar
Default 17-12-2008, 09:17 17-12-2008

để tìm nội dung trong 1 file ta có thể dùng lệnh sau

grep -R "hautp" vnlamp.txt
   
Trả Lời Với Trích Dẫn
Trả lời


Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt
Trackbacks are Mở
Pingbacks are Mở
Refbacks are Mở


Múi giờ GMT. Hiện tại là 12:9 09-02-2010.