1. 程式人生 > >Linux下基礎命令(一)

Linux下基礎命令(一)

linux find 實戰操作

每天積累知識就會有成長,只要付出在不久的將來一定會有收獲,可能回來的晚點,但是一定要貴在堅持,今天第一次發博客,如不出意外情況我每天都會發的,這個是我總結的find命令的一些實戰,希望我總結的知識點在今後會對大家有幫助。




find




-name "*a" #以a結尾的文件或目錄

-name "a*" #以a開頭的文件或目錄

-name "*a*" #文件名中帶有a字符的文件或目錄




命令的形式


格式:find pathname –options [-print]

命令字 路徑名稱 選項 輸出

參數:

pathname: find命令所查找的目錄路徑。例如用 . 來表示當前目錄,用 / 來表示系統根目錄。

-print: find命令將匹配的文件輸出到標準輸出。

find命令選項:

-name 按照文件名查找文件。 “名稱”

-perm 按照文件權限來查找文件。666 777 等

-prune 使用這一選項可以使find命令不在當前指定的目錄中查找(排除)如果同時使用-depth選項,那麽-prune將被find命令忽略

-depth在查找文件時,首先查找當前目錄中的文件,然後再在其子目錄中查找

-user 按照文件屬主來查找文件

-nouser 查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在

-group 按照文件所屬的組來查找文件

-nogroup 查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在


-mtime –n / +n 按照文件的更改時間來查找文件,

- n表示文件更改時間距現在n天以內

+ n表示文件更改時間距現在n天以前


-type 查找某一類型的文件

b - 塊設備文件

d - 目錄

c - 字符設備文件

p - 管道文件

l- 符號鏈接文件

f - 普通文件

-size n [c] 查找文件長度為n塊的文件,帶有c時表示文件長度以字節計

-exec對匹配的文件執行該參數所給出的其他linux命令, 相應命令的形式為‘ 命令 {} \;,註意{ }和 \;之間的空格,{}代表查到的內容

-fstype查找位於某一類型文件系統中的文件,這些文件系統類型通常可以在配置文件/etc/fstab中找到,該配置文件中包含了本系統中有關文件系統的信息。

-mount在查找文件時不跨越文件系統mount點。

-follow如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件。


1.查找/目錄下的以a開頭的文件


[[email protected] ~]# find / -name a.*

/a.txt

[[email protected] ~]#


2.查找當前目錄下文件權限為644的文件


[[email protected] ~]# find ./ -perm 644

/root/ac

/root/.tcshrc

/root/install.log.syslog

/root/.bashrc

/root/.cshrc

/root/install.log

/root/.bash_logout

/root/a10

/root/.bash_profile

[[email protected] ~]#


3.查找/目錄下名稱為aaa的文件或目錄


[[email protected] ~]# find / -name aaa

/aaa

/root/aaa

[[email protected] ~]#


4.查找/etc/目錄下以文件權限為644 的文件或目錄,先查看當前目錄然後再查其次子目錄


[[email protected] ~]# find /etc/ -depth -perm 644

/etc/login.defs

/etc/adjtime

/etc/rwtab

/etc/printcap

/etc/xdg/autostart/imsettings-start.desktop

/etc/xdg/autostart/restorecond.desktop

/etc/xdg/autostart/gnome-keyring-daemon.desktop

...........


5.查找/etc 目錄下所屬主是root的文件或目錄,只顯示前10行


[[email protected] ~]# find /etc/ -user root | head -10

/etc/

/etc/login.defs

/etc/rc

/etc/adjtime

/etc/rwtab

/etc/xinetd.d

/etc/printcap

/etc/sudoers.d

/etc/xdg

/etc/xdg/autostart

[[email protected] ~]#


後10行


[[email protected] ~]# find /etc/ -user root | tail -10

/etc/rc.d/rc4.d/S10network

/etc/rc.d/rc4.d/K89netconsole

/etc/rc.d/rc4.d/S80postfix

/etc/rc.d/rc4.d/S11auditd

/etc/rc.d/rc4.d/S08iptables

/etc/rc.d/rc4.d/K89rdisc

/etc/rc.d/rc4.d/S26udev-post

/etc/rc.d/rc4.d/S55sshd

/etc/rc.d/rc4.d/K10saslauthd

/etc/rc.d/rc4.d/S50kdump

[[email protected] ~]#



6.查找/etc/目錄下 所屬組和所屬主都是root 先查看當前目錄然後再查其次子目錄 顯示前10行


[[email protected] ~]# find /etc/ -depth -user root -group root | head -10

/etc/login.defs

/etc/rc

/etc/adjtime

/etc/rwtab

/etc/xinetd.d

/etc/printcap

/etc/sudoers.d

/etc/xdg/autostart/imsettings-start.desktop

/etc/xdg/autostart/restorecond.desktop

/etc/xdg/autostart/gnome-keyring-daemon.desktop


[[email protected] ~]# ll /etc/login.defs

-rw-r--r--. 1 root root 1816 2月 9 2016 /etc/login.defs

[[email protected] ~]#




無讀寫執行文件的權限也可以刪除,前提是要備份


[[email protected] proc]# ll /etc/shadow

----------. 1 root root 612 6月 7 18:05 /etc/shadow

[[email protected] proc]# rm -rf /etc/shadow

[[email protected] proc]# ls




7.查看/apps目錄下文件但不查看/apps/bin目錄


[[email protected] ~]# find /apps -path "/apps/bin/" -prune -o -print

/apps

/apps/aaa

/apps/bin

[[email protected] ~]#


8.查看/目錄下類型為目錄,名稱為aaa 並查看


[[email protected] ~]# find / -type d -name aaa -ls

3670017 4 drwxr-xr-x 2 root root 4096 6月 7 18:54 /aaa

3276811 4 drwxr-xr-x 2 root root 4096 6月 19 14:34 /root/aaa

[[email protected] ~]#


9.刪除/目錄下aaa文件


[[email protected] ~]# find / -name aaa -ls

3538947 0 -rw-r--r-- 1 root root 0 6月 20 15:01 /apps/aaa

3670017 4 drwxr-xr-x 2 root root 4096 6月 7 18:54 /aaa

3276811 4 drwxr-xr-x 2 root root 4096 6月 19 14:34 /root/aaa

[[email protected] ~]#

[[email protected] ~]#

[[email protected] ~]#

[[email protected] ~]# find / -type f -name aaa -delete

[[email protected] ~]#

[[email protected] ~]#

3670017 4 drwxr-xr-x 2 root root 4096 6月 7 18:54 /aaa

3276811 4 drwxr-xr-x 2 root root 4096 6月 19 14:34 /root/aaa

[[email protected] ~]#


10.查看目錄名稱為aaa並查看目錄下所有文件或目錄


[[email protected] ~]# find / -type d -name aaa | xargs ls

/aaa:

CentOS-Base.repo CentOS-Debuginfo.repo CentOS-fasttrack.repoCentOS-Media.repo CentOS-Vault.repo


/root/aaa:

[[email protected] ~]#


11.查看/etc/下 以a開頭的文件或目錄 並以長格式顯示


[[email protected] ~]# find /etc/ -depth -name "a*" -exec ls -l {} \;

-rw-r--r--. 1 root root 46 6月 19 17:28 /etc/adjtime

總用量 12

-rw-r--r--. 1 root root 2733 10月 4 2012 gnome-keyring-daemon.desktop

-rw-r--r--. 1 root root 2178 6月 22 2012 imsettings-start.desktop

-rw-r--r--. 1 root root 173 5月 11 2016 restorecond.desktop

總用量 0

lrwxrwxrwx. 1 root root 23 6月 7 18:02 bin-iptables-xml.x86_64 -> /bin/iptables-xml-1.4.7

lrwxrwxrwx. 1 root root 21 6月 7 18:02 ip6tables.x86_64 -> /sbin/ip6tables-1.4.7

lrwxrwxrwx. 1 root root 20 6月 7 18:02 iptables.x86_64 -> /sbin/iptables-1.4.7

lrwxrwxrwx. 1 root root 30 6月 7 18:02 libip4tc000.x86_64 -> /lib64/libip4tc.so.0.0.0-1.4.7

lrwxrwxrwx. 1 root root 26 6月 7 18:02 libip4tc0.x86_64 -> /lib64/libip4tc.so.0-1.4.7

lrwxrwxrwx. 1 root root 30 6月 7 18:02 libip6tc000.x86_64 -> /lib64/libip6tc.so.0.0.0-1.4.7

lrwxrwxrwx. 1 root root 26 6月 7 18:02 libip6tc0.x86_64 -> /lib64/libip6tc.so.0-1.4.7


返回提示信息是否


[[email protected] ~]# find / -depth -name "*a" -ok ls -l {} \;

< ls ... /usr/lib64/nss/unsupported-tools/btoa > ? y

-rwxr-xr-x. 1 root root 10408 5月 11 2016 /usr/lib64/nss/unsupported-tools/btoa

< ls ... /usr/lib64/krb5/plugins/authdata > ?


12.查看3天以內修改過的文件


[[email protected] ~]# find /root/ -mtime -3

/root/

/root/ac

/root/.bash_history

/root/aaa

/root/a10

/root/.Xauthority

[[email protected] ~]#


13.查看1天以前修改過的文件


[[email protected] ~]# find /root/ -mtime +1

/root/.tcshrc

/root/install.log.syslog

/root/.bashrc

/root/.cshrc

/root/install.log

/root/.bash_logout

/root/anaconda-ks.cfg

/root/.bash_profile

[[email protected] ~]#


14.查看/etc 目錄下 除了文件以外的類型都顯示,顯示前10行


[[email protected] ~]# find /etc -depth ! -type f | head -10

/etc/rc

/etc/xinetd.d

/etc/sudoers.d

/etc/xdg/autostart

/etc/xdg

/etc/gnupg

/etc/pkcs11/modules

/etc/pkcs11

/etc/statetab.d

/etc/cron.d

[[email protected] ~]#




15.查看/etc目錄下 大於1M的文件


[[email protected] ~]# find /etc/ -size +1M

/etc/selinux/targeted/policy/policy.24

/etc/selinux/targeted/modules/active/policy.kern

[[email protected] ~]#


[[email protected] ~]# du -sh /etc/selinux/targeted/policy/policy.24

8.1M/etc/selinux/targeted/policy/policy.24

[[email protected] ~]#

[[email protected] ~]# du -sh /etc/selinux/targeted/modules/active/policy.kern

8.1M/etc/selinux/targeted/modules/active/policy.kern

[[email protected] ~]#



16.查看/etc/目錄下 大於10塊的文件 前10個 (1b=512字節)一塊等於512字節


[[email protected] ~]# find /etc/ -depth -size +10b | head -10

/etc/sound/events/gtk-events-2.soundlist

/etc/sound/events/gnome-2.soundlist

/etc/rdma/fixup-mtrr.awk

/etc/sysconfig/network-scripts/ifup-ib

/etc/sysconfig/network-scripts/ifup-ippp

/etc/sysconfig/network-scripts/network-functions

/etc/sysconfig/network-scripts/ifdown-eth

/etc/sysconfig/network-scripts/ifup-eth

/etc/sysconfig/network-scripts/network-functions-ipv6

/etc/sysconfig/network-scripts/ifup-aliases

[[email protected] ~]#


17.查看/etc 目錄下 大於20k小於50k的文件或目錄


[[email protected] ~]# find /etc/ -size +20k -size -50k

/etc/sound/events/gnome-2.soundlist

/etc/sysconfig/network-scripts/network-functions-ipv6

/etc/postfix/main.cf

/etc/ld.so.cache

/etc/makedev.d/01linux-2.6.x

/etc/selinux/targeted/modules/active/modules/cups.pp

/etc/selinux/targeted/modules/active/modules/samba.pp

/etc/selinux/targeted/modules/active/modules/unprivuser.pp

/etc/selinux/targeted/modules/active/modules/virt.pp

/etc/selinux/targeted/modules/active/modules/apache.pp

/etc/selinux/targeted/modules/active/modules/postfix.pp

/etc/selinux/targeted/modules/active/modules/xguest.pp

/etc/selinux/targeted/modules/active/modules/nagios.pp

/etc/selinux/targeted/modules/active/modules/rhcs.pp

/etc/selinux/targeted/modules/active/modules/staff.pp

/etc/selinux/targeted/modules/active/modules/unconfineduser.pp

/etc/rc.d/init.d/kdump

/etc/rc.d/init.d/functions


[[email protected] ~]# du -sh /etc/sound/events/gnome-2.soundlist

28K/etc/sound/events/gnome-2.soundlist

[[email protected] ~]#


18.查找/etc/目錄下以文件權限為644 或者 文件名為dsafadwfwqer


[[email protected] ~]# find /etc/ -depth -perm 644 -o -name "dsafadwfwqer" | head -10

/etc/login.defs

/etc/adjtime

/etc/rwtab

/etc/printcap

/etc/xdg/autostart/imsettings-start.desktop

/etc/xdg/autostart/restorecond.desktop

/etc/xdg/autostart/gnome-keyring-daemon.desktop

/etc/sysctl.conf

/etc/cron.d/0hourly

/etc/logrotate.conf

[[email protected] ~]#


19.查找/etc/目錄下以文件權限為644 而且要滿足 文件名為dsafadwfwqer


[[email protected] ~]# find /etc/ -perm 644 -a -name "dsafadwfwqer"

/etc/dsafadwfwqer

[[email protected] ~]#


20.第一個命令不成功則輸出hahaha


[[email protected] ~]# dsafdsafqewrqewr || echo "hahaha"

-bash: dsafdsafqewrqewr: command not found

hahaha

[[email protected] ~]# echo $?

0

[[email protected] ~]#


本文出自 “linuxtouch” 博客,請務必保留此出處http://linuxtouch.blog.51cto.com/12429083/1941017

Linux下基礎命令(一)