Find Command Usage

August 27 2015

အခုနောက်ပိုင်း Linux မှာက GUI သုံးပြီးအလွယ်တကူ Search လုပ်လို့ရနေတော့ find ဆိုတဲ့ command ကို သိပ်မသုံးဖြစ်ကြပါဘူး။ သို့ပေမယ့် ပိုမိုတိကျတဲ့ result ရရှိဖို့အတွက်ကျတော့ find ကအရမ်းအသုံးဝင်ပါတယ်။ Linux Admin တွေအနေနဲ့ သိထားသင့်တဲ့ command လေးဖြစ်တဲ့အတွက် အသုံးပြုပုံတချို့ကို ဖော်ပြပေးလိုက်ပါတယ်။

File Name ဖြင့်ရှာဖွေခြင်း

  • /etc အောက်မှာရှိတဲ့ .conf နဲ့ဆုံးတဲ့ ဖိုင်တွေကိုပဲရှာချင်တယ်ဆိုလျှင်တော့
$ find /etc -name "*.conf"

command မှာပါတဲ့ * (Asterik) လေးကတော့ wildcard character ပါ။ .conf ရှေ့မှာ ဘာ character ရှိရှိလို့ ဆိုလိုခြင်းဖြစ်ပါတယ်။

  • File name စာလုံး အကြီး၊ အသေး မရွေးစေချင်လျှင်တော့ -iname ဆိုတဲ့ option ကိုသုံးနိုင်ပါတယ်။ -name နဲ့ကွာခြားပုံကို အောက်မှာဖော်ပြထားတဲ့ နမူနာအတိုင်း စမ်းကြည့်နိုင်ပါတယ်။ -name option သုံးရင် hello ဆိုတဲ့တစ်ဖိုင်ပဲတွေ့ရမှာဖြစ်ပြီး -iname နဲ့ဆိုလျှင်တော့ ၃ ဖိုင်လုံးတွေ့ရပါလိမ့်မယ်။
$ mkdir testing
$ cd testing
$ touch hello HelloGuys HelloThere
$ find . -name "hel*"
./hello
$ find . -iname "hel*"
./hello
./HelloGuys
./HelloThere

Folder နှင့် File ခွဲခြားရှာဖွေခြင်း

  • Folder သီးသန့်ရှာချင်တယ်ဆိုလျှင်တော့ -type d option ကိုအသုံးပြုရပြီး File ဆိုလျှင်တော့ -type f ကိုသုံးရပါတယ်။ နမူနာမှာတော့ testing folder အောက်မှာ HelloDir ဆိုတဲ့ folder ထပ်ဆောက်ပြီး ကွာခြားချက်ကို မြင်နိုင်အောင်ယှဉ်ပြပေးထားပါတယ်။
$ mkdir HelloDir
$ find . -type d -iname "hel*"
./HelloDir
$ find . -type f -iname "hel*"
./hello
./HelloGuys
./HelloThere

အချိန်ဖြင့်ရှာဖွေခြင်း

  • လွန်ခဲ့တဲ့ ၅ရက်လောက်အတောအတွင်းမှာ edit (သို့) modify လုပ်ထားတဲ့ File တွေ Folder တွေကိုရှာချင်လျှင်တော့ -mtime ဆိုတဲ့ option ကိုသုံးနိုင်ပါတယ်။ Default Parameter ကတော့ Day နဲ့ သတ်မှတ်ပါတယ်။ လက်ရှိနေ့မှ ၅ရက်အတွင်း ရှာဖွေမှာဖြစ်တဲ့အတွက် -5 လို့သတ်မှတ်ပေးရပါမယ်။ 5 လို့ပဲသတ်မှတ်လိုက်မယ်ဆိုလျှင်တော့ ၅ရက်အတွင်းမဟုတ်တော့ဘဲ ၅ရက်မြောက်နေ့မှာ modify လုပ်ထားတဲ့ ဖိုင်တွေကိုပဲထုတ်ပြပေးမှာဖြစ်ပါတယ်။
$ find /home -mtime -5
$ find /home -mtime 5
  • access လုပ်ထားတဲ့ date နဲ့ရှာချင်တယ်ဆိုလျှင်တော့ -atime option ကိုသုံးနိုင်ပါတယ်။ ပြီးခဲ့တဲ့ ၁၅ရက်မြောက်နေ့မှာ access လုပ်ထားခဲ့တဲ့ File တွေ Folder တွေကို ရှာချင်ရင်တော့ 30 ဆိုတဲ့ parameter ပေးလိုက်ရင်ရပါတယ်။ အကယ်၍ ၁၅ရက်လောက် access မလုပ်ဖြစ်တဲ့ File တွေ Folder တွေကိုရှာချင်တာဆိုလျှင်တော့ +15 ဆိုပြီး ရက်အရေအတွက် ထည့်ပေးရပါမယ်။
$ find /home -atime 15
$ find /home -atime +15
  • ရက် ၅၀ကျော်နောက်ပိုင်းကနေ ရက် ၁၀၀မြောက်နေ့အတွင်း ကိုရှာချင်တယ်ဆိုလျှင်တော့ -mtime နှစ်ခုဆင့်သုံးနိုင်ပါတယ်။
$ find /home -mtime +50 -mtime -100

Time မှာ Default က day ဆိုပေမယ့် s (second), m (minute), h (hour), d (day), w (week) ဆိုတဲ့ unit တွေနဲ့လည်း ထည့်သုံးနိုင်ပါတယ်။

Size ဖြင့်ရှာဖွေခြင်း

  • File နှင့် Folder တို့ရဲ့ Size အကြီးအသေးပေါ်မူတည်ပြီး ရှာဖွေ မယ်ဆိုလျှင်တော့ -size option ရှိပါတယ်။ 5MB ပမာဏရှိတဲ့ File တွေ Folder တွေကိုရှာချင်တယ်ဆိုလျှင် -
$ find /home -size 5M
  • 5MB ကနေ 20MB ပမာဏကြားရှိတဲ့ ဖိုင်တွေဆိုလျှင်တော့ -
$ find /home -size +5M -size -20M

ဒီနေရာမှာတော့ + ကို Greater Than အနေနဲ့သတ်မှတ်အသုံးပြုပြီး - ကိုတော့ Less Than အနေနဲ့အသုံးပြုပါတယ်။

User (သို့) Group ဖြင့်ရှာဖွေခြင်း

  • User တစ်ယောက်ယောက်ပိုင်တဲ့ ဖိုင်တွေကိုရှာချင်တယ်ဆိုလျှင်တော့ -user ဆိုတဲ့ option ကိုသုံးနိုင်ပြီး Group ဆိုလျှင်တော့ -group ကိုသုံးနိုင်ပါတယ်။
$ find / -user user1
$ find / -group group1

user1 နဲ့ group1 နေရာမှာ ကိုယ်ရှာဖွေလိုတဲ့ user နဲ့ group ရဲ့ နာမည်ကို အစားထိုးအသုံးပြုရပါမယ်။

Permission ဖြင့်ရှာဖွေခြင်း

  • File Permission နဲ့ရှာဖွေတဲ့နေရာမှာ Octal Numbering ဖြင့် ရှာဖွေနိုင်ပါတယ်။ Option ကတော့ -perm ဖြစ်ပါတယ်။ File Permission အကြောင်းကိုတော့ ဒီနေရာမှာမပြောတော့ပါဘူး။
$ find /etc -perm 0755
$ find /etc -perm 0777

Find Result အား Process ပြုလုပ်ခြင်း

  • အထက်မှာဖော်ပြခဲ့တဲ့ find command တွေရဲ့ result မှာပါဝင်တဲ့ File တွေ Directory တွေကို အခြား command တွေနဲ့တွဲပြီး Process ပြုလုပ်နိုင်ပါသေးတယ်။ အဲဒီအတွက် -exec ဆိုတဲ့ option နဲ့ တွဲဖက်အသုံးပြုရပါတယ်။

  • /etc directory နဲ့ သူ့အောက်မှာရှိတဲ့ sub-directory တွေထဲက .conf ဖိုင်တွေကို Home Directory အောက်က Config ဆိုတဲ့ folder ထဲထည့်မယ်ဆိုလျှင်တော့ -

$ mkdir ~/Config
$ find /etc -type f -iname "*.conf" -exec cp {} ~/Config \;
  • /home directory နဲ့ သူ့အောက်မှာရှိတဲ့ sub-directory တွေထဲက .tmp extension နဲ့ဆုံးတဲ့ File တွေကိုရှင်းထုတ်ချင်တယ်ဆိုလျှင် -
$ find /home -type f -name "*.tmp" -exec rm -f {} \;
  • /home directory အောက်မှာရှိတဲ့ directory တွေနဲ့ sub-directory တွေထဲက Permission 777 ပေးထားတဲ့ directory တွေကိုရှာပြီး 755 အနေနဲ့ပြောင်းမယ်ဆိုလျှင်တော့ -
$ find /home -type d -perm 777 -exec chmod 755 {} \;

စသဖြင့် အသီးသီး ကွန့်မြူးချဲ့ထွင်အသုံးပြုနိုင်ပါတယ်။

တကယ်တမ်း find command က သုံးမယ်ဆို သုံးလို့ရတဲ့နည်းလမ်းတွေ အများကြီးရှိပါသေးတယ်။ အပြည့်အစုံကိုတော့ Terminal ကနေ man find နဲ့ manual page ခေါ်ပြီး လေ့လာနိုင်ပါတယ်။ အောက်က link မှာလည်း နမူနာ တချို့ကိုလေ့လာနိုင်ပါသေးတယ်။

Reference : 35 Practical Examples Of Linux Find Command


comments powered by Disqus