Find - Tutoriel linux

Exemples d'utilisation de la commande find

Mise à jour : Debian 9.0

1 - Critères de recherche

1.1 - name, maxdepth, type, perm, user

(1) Recherche, dans le répertoire /sys et ses sous-répertoires, des fichiers, répertoires et liens ayant pour nom rfkill

# find /sys -name rfkill
/sys/devices/platform/asus-nb-wmi/rfkill
/sys/devices/virtual/misc/rfkill
/sys/class/misc/rfkill
/sys/class/rfkill
/sys/module/rfkill
# find /sys -maxdepth 2 -name rfkill
/sys/class/rfkill
/sys/module/rfkill
# find /sys -type d -name rfkill 
/sys/devices/platform/asus-nb-wmi/rfkill
/sys/devices/virtual/misc/rfkill
/sys/class/rfkill
/sys/module/rfkill
# find /sys -type f -name rfkill
# find /sys -type l -name rfkill 
/sys/class/misc/rfkill
# find /sys -name rfkill -perm 755
/sys/devices/platform/asus-nb-wmi/rfkill
/sys/devices/virtual/misc/rfkill
/sys/class/rfkill
/sys/module/rfkill
# find /sys -name rfkill -user root
/sys/devices/platform/asus-nb-wmi/rfkill
/sys/devices/virtual/misc/rfkill
/sys/class/misc/rfkill
/sys/class/rfkill
/sys/module/rfkill

1.2 - size, xtype, empty

Recherche des fichiers, répertoires ou liens de /sys

# find /sys -size +2k -size -4k
/sys/firmware/dmi/tables/DMI
/sys/firmware/acpi/tables/SSDT2
/sys/firmware/acpi/tables/SSDT1
# find /sys -xtype l
# find /sys/class -type d -empty
/sys/class/dma
/sys/class/spi_master
/sys/class/ppdev
/sys/class/devfreq
/sys/class/watchdog
/sys/class/printer
/sys/class/phy
/sys/class/iommu
/sys/class/tpm

2.3 - ctime, atime, mtime, cmin, amin, mmin

# find /etc -type m -ctime 0
# find /etc -type f -mtime 100
# find /etc -type f -mtime +10 -ctime -12

Les mêmes recherches peuvent être effectués sur

2 - Caractères spéciaux, combinaisons

2.1 - Chemins

# find /sys/*/drm* -name taint
/sys/module/drm/taint
/sys/module/drm_kms_helper/taint
# find /sys /var -name rfkill
/sys/devices/platform/asus-nb-wmi/rfkill
/sys/devices/virtual/misc/rfkill
/sys/class/misc/rfkill
/sys/class/rfkill
/sys/module/rfkill
/var/lib/systemd/rfkill

2.2 - Noms

# find /sys -type l -iname RFkill
/sys/class/misc/rfkill
# find /sys -type l -name "rfkill?"
/sys/class/rfkill/rfkill2
/sys/class/rfkill/rfkill0
/sys/class/rfkill/rfkill1
# find /sys/*/drm* -not -name taint |head -5
/sys/class/drm
/sys/class/drm/card0-LVDS-1
/sys/class/drm/version
/sys/class/drm/renderD128
/sys/class/drm/card0-VGA-1

Critères multiples :

# find /sys -name asus* -not -name *nb*
/sys/module/asus_wmi
/sys/module/video/holders/asus_wmi
/sys/module/rfkill/holders/asus_wmi
/sys/module/sparse_keymap/holders/asus_wmi
/sys/module/wmi/holders/asus_wmi
# find /sys/kernel -name asus* -or -name intel*
/sys/kernel/debug/asus-nb-wmi
/sys/kernel/debug/intel_powerclamp
/sys/kernel/debug/tracing/events/i915/intel_gpu_freq_change

3 - Action sur le résultat de la recherche

3.1 - exec

# find /sys -name taint -exec ls -l {} \;
-r--r--r-- 1 root root 4096 mars 3 22:05 /sys/module/lp/taint
-r--r--r-- 1 root root 4096 mars 3 22:05 /sys/module/asus_wmi/taint
-r--r--r-- 1 root root 4096 mars 3 22:05 /sys/module/ac/taint
-r--r--r-- 1 root root 4096 mars 3 22:05 /sys/module/snd_hda_intel/taint
-r--r--r-- 1 root root 4096 mars 3 22:05 /sys/module/iwlwifi/taint
...........
# find /sys -name name -exec cat {} \; |sort
acpitz
asus
asus-bluetooth
ASUS USB2.0 Webcam
ASUS USB2.0 Webcam
.......
# find /sys -type f -name name -exec grep -l asus {} \; -exec cat {} \;
/sys/devices/platform/asus-nb-wmi/hwmon/hwmon2/name
asus
/sys/devices/platform/asus-nb-wmi/rfkill/rfkill2/name
asus-bluetooth
/sys/devices/platform/asus-nb-wmi/rfkill/rfkill1/name
asus-wlan

3.2 - Autres actions

# find /sys -name rfkill -ls
19277 0 drwxr-xr-x 4 root root 0 mars 5 14:25 /sys/devices/platform/asus-nb-wmi/rfkill
15568 0 drwxr-xr-x 3 root root 0 mars 5 13:26 /sys/devices/virtual/misc/rfkill
15571 0 lrwxrwxrwx 1 root root 0 mars 5 14:24 /sys/class/misc/rfkill -> ../../devices/virtual/misc/rfkill
15567 0 drwxr-xr-x 2 root root 0 mars 5 13:26 /sys/class/rfkill
12591 0 drwxr-xr-x 6 root root 0 mars 5 13:26 /sys/module/rfkill
# find /sys -name rfkill -fprint test.txt
$ cat test
/sys/devices/platform/asus-nb-wmi/rfkill
/sys/devices/virtual/misc/rfkill
/sys/class/misc/rfkill
/sys/class/rfkill
/sys/module/rfkill