#! /bin/sh

#############################
# Exemple de commandes find #
#############################

# tree like
find . | sed 's/[^/]*\//|   /g;s/| *\([^| ]\)/+--- \1/'


# Helpful hints for finding large files and folders
Find large files (10MB+):
sudo find / -type f -size +10000-exec ls -lh {} \; awk '{ print $NF ": " $5 }'

Find the largest folders (in current working directory):
sudo du -hsx * | sort -rh head -10


# This reads one filename at a time from the find command and renames the file, replacing spaces with underscores. 
find . -type f -print0 while IFSread -r -d '' filedo
    mv "$file" "${file// /_}"
done

# pour trouver des noms de fichiers dans une archive tar gz (.tgz)
tar -ztvf *.tgz

# http://askubuntu.com/questions/270625/how-to-fast-convert-mp4-to-webm-using-ffmpeg
# avec substition extension 
find ./ -name '*.mp4' -exec bash -c 'ffmpeg -i "$0" "${0%%.mp4}.webm"' {} \;


# find et la substitution d'extension
# https://plus.google.com/u/0/+UrsuIrinel/posts/AZb7wa3BhxB
    find / -name '*.jpg' -exec optijpeg {} +
If it can only do one file per execution of the optijpeg command:
    find / -name '*.jpg' -exec optijpeg {} \;
If it instead of replacing the fileneeds you to specify an output file on the command lineit gets more complicated.
This assumes you specify the output file with -o
    find / -name '*.jpg' while read filenamedo
        optijpeg "$filename" -o "${filename/.jpg/-optimized.jpg}"
    done
This will run into issues if you have spaces in your filenames or directories. This can be solved with some bashisms and -print0
    find / -name '*.jpg' -print0 while read -d $'\0' filenamedo
        optijpeg ...
    done
These find commands were run on the assumption that you wanted every jpeg on your computer to be optimised. You can save a lot of time by listing the directories instead of /.


Exemples dutilisation courante de la commande find (shell UNIX/Linux) :
http://www.piprime.fr/1378/exemples-utilisation-shell-find/

# chercher deux mots (OU) dans le contenu des fichiers txt
find /media/truecrypt7 -iname "*txt" -print0 xargs -grep -l "verre\|poulet"
# chercher deux mots (OU) dans le nom des fichiers txt
find /media/truecrypt7 -iname "*txt"  grep  "verre\|poulet"


# nokia-iser fichiers m2ts dossier courant
find "$(pwd)" -maxdepth -name "*m2ts" -print0 xargs --i encoder_Nokia_HQ.sh {}

# nokia-iser fichiers m2ts récursif
find "$(pwd)" -name "*m2ts" -print0 xargs --i encoder_Nokia_HQ.sh {}

# chercher des fichiers seulement dans le dossier courant
find *m2ts -maxdepth 0
find *m2ts -maxdepth 1

# optimiser/réduire taille tous fichiers jpeg arborescence
time find ./ -iname "*.jpg" -exec jpegoptim -m40 {} \;


##############################################
# Exclure des dossiers d'une recherche find
# avec l'option -print0 (utile pour passer à xargs -0)
##############################################
# essayer d'utiliser tr '\n' '\0' 
find grep -v dossier1_a_exclure grep -v dossier2_a_exclure tr '\n' '\0' xargs -commande_a_utiliser


##############################################
# Rechercher un dossier/fichier d'un nom exact
##############################################
# ex : chercher le dossier A
find -name A -type d
# ex : chercher les dossiers A ou B
find -name A -o -name B -type d
# ex : chercher les dossiers d'un caractère
find -name ? -type d


##############################################
# Jouer les mp3 d'un dossier (récurrence)
# avec mpg123
##############################################
# http://lindesk.com/2008/02/mpg123mpg321-the-command-line-mp3-players/
find . -name "*.mp3"|sed 's/\.\///;'|sort>List.lst
mpg123 --list List.lst


##############################################
# Rechercher le mot xsel
# dans les fichiers du répertoire courant
##############################################
find -type f -iname "*.txt" -print0  xargs -grep -l xsel nl

##############################################
# rechercher fichiers 
# déplacés récemment
##############################################
find . -type f -amin -5

##############################################
# Lister les mp3 et leur durée avec mp3info
# et trier du plus petit au plus grand
##############################################
find -name "*mp3*" -exec mp3info -p %S-%F\\n {} \; sort -n


#################################################################
Rechercher les fichiers de plus de 50 Moafficher
leur taille emplacement (possibilités)
##################################################################
find . -type f -size +50-exec du {} \; sort -n
find . -size +50000-exec du -h {} \;
find / -type f -size +50000 -printf "%s:%h/%f\n"
find / -type f -size +50000 -printf '%CY%Cm%Cd.%CH%CM\t%s\t%f\n'
find / -type f -size +50000 -printf '%CY%Cm%Cd.%CH%CM\t%s\t%f\n' sort -k -n

#################################################################
Rechercher les derniers fichiers modifiés
de l arbre courant
##################################################################
find "$(pwd)" -type f -printf "%T:%h/%f\n"
find /-type f -printf "%TY%Tm%Td_%TH%TM:%h/%f\n" sort
find /-type f -printf "%TY%Tm%Td_%TH%TM:%h/%f\n" sort -n nl less


#################################################################
Rechercher derniers fichiers modifiés
de l arbre courant
##################################################################
find /-type f -printf "%TY%Tm%Td_%TH%TM:%h/%f\n" sort -r nl less



#################################################################
Sum size of files returned from FIND 
##################################################################
find [path] [expression] -exec du -ab {} \; awk '{total+=$0}END{print total}'
echo 0find -iname "*avi*" -printf '%s +\n'echo p) | dc

#################################################################
Rechercher les fichiers et affichage taille totale
##################################################################
# echo Taper la ligne suivante pour obtenir la liste des fichiers et leur taille :
find "$source" -type f -name "*.krk.avi" -exec du {} \; cut -f1 -d\t awk '{ total+=$1 } END { print "Total des fichiers .krk.avi " total/1024 " Mo" }'
find "$source" -type f -name "*.mp3" -printf %s'\n' awk '{ total+=$1 } END { print "Total des mp3 " total*.000001 " Mo" }'
find "$source" -type f -regex '.*\(.krk.avi\|.mp3\)' -printf %s'\n' awk '{ total+=$1 } END { print "Total des mp3 et krk.avi " total*.000001 " Mo" }'

Rechercher les fichiers .avi mais pas les krk.avi :
find ./ -type f ! -regex '.*krk.*avi' -regex '.*.avi'

##################################################################
Rechercher les fichiers modifiés dans les 24 dernières heures
##################################################################
find . -type f -mtime -

##################################################################
Rechercher et SAUVEGARDER les fichiers accédés dans les 24 dernières heures
##################################################################
find . -type f -atime -cpio -ocv bzip2 -c fichiers_modifiés_ces_dernières_24_heures.bz2

#######################################################
Rechercher les fichiers modifiés il y a moins de 10 mn
#######################################################
find . -type f -mmin -10

#######################################################
Rechercher les fichiers modifiés il y a moins de h
afficher leur date de modif et trier
#######################################################
find -name "*.avi" -mmin -180 -printf "%AY%Am%Ad%AH%AI%AM\t%f\n" sort

#######################################################
Rechercher les fichiers créés il y a moins de h
#######################################################
find ./ -name "*.mp3" -cmin -180 -printf "%AY%Am%Ad%AH%AI%AM\t%f\n" sort

#######################################################
Rechercher les fichiers modifiés entre et minutes
#######################################################
find . -mmin +-mmin -5

################################################
Rechercher des fichiers selon plusieurs motifs
################################################
find . -regex '.*\(scripts_console\|_p_\).*'
Inverse :
find . -not -regex '.*\(nouveau\).*'

##################################################
Rechercher les fichiers contenant le mot chombier
##################################################
find -type f -name "*.php" -exec grep -il 'chombier' {} \;
ou bien
find -type f -name "*.php" -print0 xargs -egrep -l chombier

##################################################
 Rechercher des fichiers d une certaine extension
 et traiter tous ceux de meme radical
##################################################
extension=mp3
find . -type f -regex ".*\.$extension$" liste_extension
n=0
while read ligne
do
 n=$((n+1))
 chemin=$(echo $(dirname "$ligne"))
 fichier=$(echo $(basename "$ligne"))
 radical=$(echo $fichier|sed "s/\.$extension$//g")
 # la ligne suivante ne fonctionne pas
 # radical=$(echo $fichier|sed 's/\.$extension$//g')
 echo "$n. Chemin  : $chemin"
 echo "Fichier : $fichier"
 echo "Radical : $radical"
 echo 
done liste_extension



################################################
# find chercher texte dans tous fichiers disque
################################################
chercher le mot chaplin dans tous les fichiers du disque /media/IOMEGA :
find /media/IOMEGA_HDD -print0 xargs --null grep chaplin


##################################
# find files trouver gros fichiers
##################################
sudo find / -type f -size +10000-exec ls -lh {} \; awk '{ print $NF ": " $5 }'
find ~ -type f -size +100000-print0 xargs -ls -l
find ~/Vidéos/ -type f -size +100000-print0 xargs -ls -l sort -k -nr

###########
 man xargs
###########
find /tmp -name core -type f -print xargs /bin/rm -f
Find files named core in or below the directory /tmp and delete them.  Note that this will work  incorrectly  if  there  are  any
filenames containing newlines or spaces.

find /tmp -name core -type f -print0 xargs -/bin/rm -f
Find  files  named core in or below the directory /tmp and delete themprocessing filenames in such a way that file or directory
names containing spaces or newlines are correctly handled.

find /tmp -depth -name core -type f -delete
Find files named core in or below the directory /tmp and delete thembut more efficiently than in the previous example  (because
we avoid the need to use fork(2and exec(2to launch rm and we don\'t need the extra xargs process).

cut -d: -f1 < /etc/passwd sort xargs echo
Generates a compact listing of all the users on the system.

xargs sh -c 'emacs "$@" < /dev/tty' emacs
Launches  the  minimum  number of copies of Emacs neededone after the otherto edit the files listed on xargs\' standard input.
This example achieves the same effect as BSD\'s -o optionbut in a more flexible and portable way.


#################
# find et mp3gain
#################
 dans /media/truecrypt3/Musique :
 find ./ -type f -iname '*.mp3' -print0 xargs -mp3gain -r
 a duré 520 mn,
 8400 fichiers traité(ou 8400 modifiés sur 8600)
 
###########################################################################
# compacter_fichiers_d_une_certaine_extension_recursivement_find_tar_xargs
###########################################################################
find ~/Documents/ -name '*.txt' -print0 xargs -tar zcvf -> /tmp/archive_txt.tar.gz

 
####################################################
# enlever le ./ qui précède les résultats d'un find
####################################################
find $(pwd) ....
find /path/absolu/ ...

####################################################
# calculer md5 pour les fichiers de plus de 10M
####################################################
find $(pwd) -type f -size +10M  while read f do echo $(date +%Y%m%d_%H%M%S) md5sum "$f" \> "$f.md5" md5sum "$f" "$f.md5" done