La commande find est très utilisée dans Linux, mais les résultats obtenus ne servent pas toujours juste à les visualiser. Dans ce cas, l'option exec devient utile.
Description de l'option exec :
L'argument -exec accepte une commande qui se termine par un point-virgule. Cet élément est indispensable, car sur certains systèmes le point-virgule peut avoir une signification différente. Pour éviter les conflits, il faut précéder le point-virgule d'une barre oblique inverse.
Les accolades {} représentent les fichiers trouvés par la commande find.
En combinant un script personnalisé avec l'option exec, il est possible d'automatiser des actions sur les fichiers identifiés. Sur certains systèmes, seules les commandes comme ls ou ls -l sont autorisées avec cette option. La plupart des utilisateurs l'utilisent pour supprimer des fichiers obsolètes. Il est recommandé d'utiliser la commande ls avant d'exécuter rm pour vérifier les éléments à supprimer. L'option exec doit être associée à l'option print pour fonctionner correctement. Lorsque l'on teste la commande find, elle affiche uniquement les chemins relatifs à partir du répertoire courant.
Exemple 1 : Utilisation de la commande ls -l avec l'option exec
Commande :
find . -type f -exec ls -l {} \;
Résultat :
1 [root@localhost test]# find . -type f -exec ls -l {} \;
2 -rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
3 -rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
4 -rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
5 -rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
6 -rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
7 -rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
8 -rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
9 -rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
10 -rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
11 -rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
12 -rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
13 [root@localhost test]#
Explication :
L'exemple ci-dessus identifie tous les fichiers ordinaires dans le répertoire actuel et les affiche en utilisant la commande ls -l via l'option exec.
Exemple 2 : Recherche de fichiers modifiés il y a plus de n jours et suppression
Commande :
find . -type f -mtime +14 -exec rm {} \;
Résultat :
1 [root@localhost test]# ll
2 total 328
3 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
4 -rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
5 -rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
6 lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
7 -rw-r--r-- 1 root root 25 10-28 17:02 log.log
8 -rw-r--r-- 1 root root 37 10-28 17:07 log.txt
9 drwxr-xr-x 6 root root 4096 10-27 01:58 scf
10 drwxrwxrwx 2 root root 4096 10-28 14:47 test3
11 drwxrwxrwx 2 root root 4096 10-28 14:47 test4
12 [root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
13 [root@localhost test]# ll
14 total 312
15 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
16 lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
17 drwxr-xr-x 6 root root 4096 10-27 01:58 scf
18 drwxrwxrwx 2 root root 4096 11-12 19:32 test3
19 drwxrwxrwx 2 root root 4096 11-12 19:32 test4
20 [root@localhost test]#
Explication :
Avant de supprimer des fichiers avec des commandes comme mv ou rm, il est impératif de vérifier leur existence. L'option exec permet d'utiliser un mode sécurité qui demande une confirmation avant chaque opération.
Exemple 3 : Recherche de fichiers modifiés il y a plus de n jours et suppression avec confirmatino
Commande :
find . -name "*.log" -mtime +5 -ok rm {} \;
Résultat :
1 [root@localhost test]# ll
2 total 312
3 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
4 lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
5 drwxr-xr-x 6 root root 4096 10-27 01:58 scf
6 drwxrwxrwx 2 root root 4096 11-12 19:32 test3
7 drwxrwxrwx 2 root root 4096 11-12 19:32 test4
8 [root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;
9 < rm ... ./log_link.log > ? y
10 < rm ... ./log2012.log > ? n
11 [root@localhost test]# ll
12 total 312
13 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
14 drwxr-xr-x 6 root root 4096 10-27 01:58 scf
15 drwxrwxrwx 2 root root 4096 11-12 19:32 test3
16 drwxrwxrwx 2 root root 4096 11-12 19:32 test4
17 [root@localhost test]#
Explication :
Dans cet exemple, la commande find recherche tous les fichiers se terminant par .log modifiés depuis plus de 5 jours et les supprime après confirmation. La touche y valide la suppression, la touche n l'annule.
Exemple 4 : Utilisation de la commande grep avec exec
Commande :
find /etc -name "passwd*" -exec grep "root" {} \;
Résultat :
1 [root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;
2 root:x:0:0:root:/root:/bin/bash
3 root:x:0:0:root:/root:/bin/bash
4 [root@localhost test]#
Explication :
Toute commande peut être utilisée avec l'option exec. Dans cet exemple, nous utilisons grep. La commande find identifie d'abord tous les fichiers nommés "passwd*" (comme passwd, passwd.old, passwd.bak), puis exécute grep pour vérifier s'il existe un utilisateur root dans ces fichiers.
Exemple 5 : Déplacement de fichiers vers un répertoire spécifique
Commande :
find . -name "*.log" -exec mv {} .. \;
Résultat :
1 [root@localhost test]# ll
2 total 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
3 drwxrwxr-x 2 root root 4096 11-12 22:49 test3
4 drwxrwxr-x 2 root root 4096 11-12 19:32 test4
5 [root@localhost test]# cd test3/
6 [root@localhost test3]# ll
7 total 304
8 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
9 -rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
10 -rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
11 [root@localhost test3]# find . -name "*.log" -exec mv {} .. \;
12 [root@localhost test3]# ll
13 total 0[root@localhost test3]# cd ..
14 [root@localhost test]# ll
15 total 316
16 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
17 -rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
18 -rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
19 drwxr-xr-x 6 root root 4096 10-27 01:58 scf
20 drwxrwxr-x 2 root root 4096 11-12 22:50 test3
21 drwxrwxr-x 2 root root 4096 11-12 19:32 test4
22 [root@localhost test]#
Exemple 6 : Exécution de la commande cp avec l'option exec
Commande :
find . -name "*.log" -exec cp {} test3 \;
Résultat :
1 [root@localhost test3]# ll
2 total 0[root@localhost test3]# cd ..
3 [root@localhost test]# ll
4 total 316
5 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
6 -rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
7 -rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
8 drwxr-xr-x 6 root root 4096 10-27 01:58 scf
9 drwxrwxr-x 2 root root 4096 11-12 22:50 test3
10 drwxrwxr-x 2 root root 4096 11-12 19:32 test4
11 [root@localhost test]# find . -name "*.log" -exec cp {} test3 \;
12 cp: "./test3/log2014.log" et "test3/log2014.log" sont le même fichier
13 cp: "./test3/log2013.log" et "test3/log2013.log" sont le même fichier
14 cp: "./test3/log2012.log" et "test3/log2012.log" sont le même fichier
15 [root@localhost test]# cd test3
16 [root@localhost test3]# ll
17 total 304
18 -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
19 -rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
20 -rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
21 [root@localhost test3]#