Find tout puissant

Rédigé par Paulo Aucun commentaire
Classé dans : Bash Mots clés : find, xargs

Trouver les fichiers du user zorro et avec les droits rw sur ces fichiers :

find . -user jpcornillon -perm 0600 -exec ls -l {} \;
#pareil mais la commande est executée dans le repertoire du fichier
find . -user jpcornillon -perm 0600 -execdir ls -l {} \;

 

Profondeur de la recherche : maxdepth

find . -maxdepth 2 -iname "*php"

 

Operateurs :
# and (accepte aussi le  !)
find .  -not -iname "*.php"
# or
find . -iname "*.php" or -iname "*.txt"

 

Recherche uniquement sur :
- des fichiers
find . -type f -name 'abc*"
find . -type f -name ".*"   (fichiers cachés)

- des repertoires

find . -type d -name "abc"

- permissions

find . -type f -perm 0664
find . -type f -perm 2664  --> sgid
find . -type f -perm 1664 --> sticky bit
find . -type f -perm /u=r  --> lecture seule 
find . -type f -perm /a=x  --> executable

- proprietaire et group

find . -user bob
find . -user bob -group eponge

- date et heure

find . -atime 50  --> accede dans les 50 derniers jours
find . -mtime +50 -mtime -100 --acces depuis 50 et 100 jours
find . -cmin 60 --> fic modifiés depuis 60mn
find . -amin 60 --> fic accedés depuis 60mn

- supression de fichier de plus d'une semaine

find . -mtime +7 -exec rm -f {} \;


- taille de fichier

find . -size 50M
find . -size +50M -size -100M  --> fic >50mo et <100mo
find . -type f -exec ls -s {} \; | sort -n -r | head -5
find . -type f -exec ls -s {} \; | sort -n  | head -5
find . -type f -empty --> fic vide
find . -type d -empty --> rep vide

- exec
 

find . -exec ls -ld {} \;
find /tmp -type f -name "*.txt" -exec rm -f {} \;
find /home/bob/dir -type f -name *.log -size +10M -exec rm -f {} \;

- grep : recherche fichier contenant..
 

find /etc/ -name '*.conf' -exec grep -l 'infiniband' {} \; 
find /etc/ -name '*.conf' -exec grep -l 'infiniband' {} \;  2>/dev/null 
# recherche d'un mot contenu dans les fichiers du repertoire courant
find . -type f|xargs grep -win # w=mot exact i=numero ligne/position
# alias de cette commande
alias rgrep="find . -type f|xargs grep -win"

 

 

Selection/copie de fichier avec find + xargs :

#recherche des fichiers png/jpg/bmp et copie dans le repertoire /tmp/logos/
find .|egrep -i 'png|jpg|bmp'|xargs -i  cp {} /tmp/logos/

#recherche des fichiers et copie/renommage dans un autre repertoire
find . -name "*.jpg" -print0|xargs -0 -I file cp file /tmp/file_org

 


Source : http://www.binarytides.com/linux-find-command-examples/
                http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/
 





 

Les commentaires sont fermés.