#! /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 +10000k -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 IFS= read -r -d '' file; do
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 file, needs you to specify an output file on the command line, it gets more complicated.
This assumes you specify the output file with -o
find / -name '*.jpg' | while read filename; do
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' filename; do
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 d’utilisation 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 -0 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 0 -name "*m2ts" -print0 | xargs -0 -i encoder_Nokia_HQ.sh {}
# nokia-iser fichiers m2ts récursif
find "$(pwd)" -name "*m2ts" -print0 | xargs -0 -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 -0 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 -0 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 Mo, afficher
leur taille + emplacement (3 possibilités)
##################################################################
find . -type f -size +50M -exec du {} \; | sort -n
find . -size +50000k -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 2 -n
#################################################################
Rechercher les 5 derniers fichiers modifiés
de l arbre courant
##################################################################
find "$(pwd)" -type f -printf "%T:%h/%f\n"
find /m -type f -printf "%TY%Tm%Td_%TH%TM:%h/%f\n" | sort
find /m -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 /m -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 0; find -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 -1
##################################################################
Rechercher et SAUVEGARDER les fichiers accédés dans les 24 dernières heures
##################################################################
find . -type f -atime -1 | 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 3 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 3 h
#######################################################
find ./ -name "*.mp3" -cmin -180 -printf "%AY%Am%Ad%AH%AI%AM\t%f\n" | sort
#######################################################
Rechercher les fichiers modifiés entre 1 et 5 minutes
#######################################################
find . -mmin +0 -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 -0 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 +10000k -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'
find ~ -type f -size +100000k -print0 | xargs -0 ls -l
find ~/Vidéos/ -type f -size +100000k -print0 | xargs -0 ls -l | sort -k 5 -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 -0 /bin/rm -f
Find files named core in or below the directory /tmp and delete them, processing 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 them, but more efficiently than in the previous example (because
we avoid the need to use fork(2) and exec(2) to 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 needed, one after the other, to edit the files listed on xargs\' standard input.
This example achieves the same effect as BSD\'s -o option, but in a more flexible and portable way.
#################
# find et mp3gain
#################
dans /media/truecrypt3/Musique :
find ./ -type f -iname '*.mp3' -print0 | xargs -0 mp3gain -r
a duré 520 mn,
8400 fichiers traités (ou 8400 modifiés sur 8600)
###########################################################################
# compacter_fichiers_d_une_certaine_extension_recursivement_find_tar_xargs
###########################################################################
find ~/Documents/ -name '*.txt' -print0 | xargs -0 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