Skip to content

Find Avanzado

El comando find es una de las herramientas más poderosas en Linux para búsqueda de archivos y directorios. Esta guía cubre desde uso básico hasta técnicas avanzadas para pentesting y análisis forense.

Terminal window
find [ruta] [expresión]
  • Tests: -name, -type, -size, -perm, etc.
  • Actions: -print, -exec, -delete, etc.
  • Operators: -and, -or, -not
Terminal window
# Buscar por nombre exacto
find /home -name "archivo.txt"
# Búsqueda case-insensitive
find /home -iname "ARCHIVO.txt"
# Patrones con wildcards
find /home -name "*.log"
find /home -name "config*"
find /home -name "*.conf"
# Múltiples patrones
find /home \( -name "*.log" -o -name "*.txt" \)
Terminal window
# Usar regex (requiere -regex)
find /var/log -regex ".*\.[0-9]+"
# Regex case-insensitive
find /var/log -iregex ".*error.*\.log"
# Buscar archivos que NO coincidan
find /home -not -name "*.tmp"
Terminal window
# Archivos regulares
find /home -type f
# Directorios
find /home -type d
# Enlaces simbólicos
find /home -type l
# Dispositivos de bloque
find /dev -type b
# Dispositivos de caracteres
find /dev -type c
# Pipes nombrados (FIFO)
find /tmp -type p
# Sockets
find /tmp -type s
Terminal window
# Archivos mayores a 100MB
find /home -size +100M
# Archivos menores a 1KB
find /home -size -1k
# Archivos de exactamente 512 bytes
find /home -size 512c
# Rangos de tamaño
find /home -size +1M -size -10M
# Archivos vacíos
find /home -empty
# Directorios vacíos
find /home -type d -empty
  • c: bytes
  • k: kilobytes
  • M: megabytes
  • G: gigabytes
Terminal window
# Permisos exactos (octal)
find /home -perm 644
# Al menos estos permisos
find /home -perm -644
# Cualquiera de estos permisos
find /home -perm /644
# Archivos ejecutables por el propietario
find /home -perm -u+x
# Archivos world-writable
find /home -perm -002
# Archivos SUID
find /home -perm -4000
# Archivos SGID
find /home -perm -2000
# Sticky bit
find /home -perm -1000
Terminal window
# Por usuario específico
find /home -user username
# Por UID
find /home -uid 1000
# Por grupo
find /home -group groupname
# Por GID
find /home -gid 100
# Archivos sin propietario
find /home -nouser
# Archivos sin grupo
find /home -nogroup
# Combinaciones
find /home -user root -group wheel
Terminal window
# Accedidos en los últimos 7 días
find /home -atime -7
# Accedidos hace exactamente 7 días
find /home -atime 7
# Accedidos hace más de 7 días
find /home -atime +7
# Accedidos en los últimos 60 minutos
find /home -amin -60
Terminal window
# Modificados en los últimos 24 horas
find /home -mtime -1
# Modificados en los últimos 30 minutos
find /home -mmin -30
# Archivos más nuevos que otro archivo
find /home -newer /etc/passwd
Terminal window
# Metadatos cambiados en los últimos 7 días
find /home -ctime -7
# Metadatos cambiados en los últimos 120 minutos
find /home -cmin -120
Terminal window
# Ejecutar comando para cada archivo
find /home -name "*.log" -exec cat {} \;
# Ejecutar comando con múltiples archivos
find /home -name "*.txt" -exec grep "pattern" {} +
# Confirmar antes de ejecutar
find /home -name "*.tmp" -ok rm {} \;
# Usar xargs para mejor rendimiento
find /home -name "*.log" -print0 | xargs -0 grep "error"
Terminal window
# Formato personalizado
find /home -printf "%p %s %TY-%Tm-%Td\n"
# Solo mostrar nombres de archivo
find /home -name "*.conf" -printf "%f\n"
# Mostrar permisos y tamaño
find /home -printf "%M %s %p\n"
  • %p: ruta completa
  • %f: nombre del archivo
  • %s: tamaño en bytes
  • %M: permisos en formato simbólico
  • %u: nombre del propietario
  • %g: nombre del grupo
  • %T: tiempo (requiere especificador adicional)
Terminal window
# AND (implícito)
find /home -name "*.log" -size +1M
# AND explícito
find /home -name "*.log" -and -size +1M
# OR
find /home \( -name "*.log" -o -name "*.txt" \)
# NOT
find /home -not -name "*.tmp"
find /home ! -name "*.tmp"
# Combinaciones complejas
find /home \( -name "*.log" -o -name "*.txt" \) -and -size +1k
Terminal window
# Solo en el directorio actual
find /home -maxdepth 1 -name "*.txt"
# Mínimo 2 niveles de profundidad
find /home -mindepth 2 -name "*.conf"
# Entre 2 y 4 niveles
find /home -mindepth 2 -maxdepth 4 -type d
Terminal window
# Seguir todos los enlaces
find -L /home -name "*.txt"
# No seguir enlaces (por defecto)
find -P /home -name "*.txt"
# Seguir enlaces solo en línea de comandos
find -H /home -name "*.txt"
Terminal window
# Excluir directorio específico
find /home -path "*/node_modules" -prune -o -name "*.js" -print
# Excluir múltiples directorios
find /home \( -path "*/node_modules" -o -path "*/.git" \) -prune -o -name "*.txt" -print
Terminal window
# Archivos de configuración
find /etc -name "*.conf" -o -name "*.cfg" -o -name "*.ini"
# Archivos de contraseñas
find / -name "*password*" -o -name "*passwd*" -o -name "shadow*" 2>/dev/null
# Claves SSH
find /home -name "id_rsa" -o -name "id_dsa" -o -name "*.pem" 2>/dev/null
# Archivos de backup
find / -name "*.bak" -o -name "*.backup" -o -name "*~" 2>/dev/null
# Archivos de log
find /var/log -name "*.log" -mtime -7
Terminal window
# Archivos SUID
find / -perm -4000 -type f 2>/dev/null
# Archivos SGID
find / -perm -2000 -type f 2>/dev/null
# Archivos world-writable
find / -perm -002 -type f 2>/dev/null
# Directorios world-writable
find / -perm -002 -type d 2>/dev/null
# Archivos sin propietario
find / -nouser -o -nogroup 2>/dev/null
Terminal window
# Ejecutables en PATH
echo $PATH | tr ':' '\n' | xargs -I {} find {} -type f -executable 2>/dev/null
# Scripts con shebang específico
find /home -type f -exec grep -l "^#!/bin/bash" {} \; 2>/dev/null
# Binarios ELF
find /home -type f -exec file {} \; | grep ELF
# Archivos con capabilities
find /usr/bin -type f -exec getcap {} \; 2>/dev/null | grep -v "="
Terminal window
# Archivos modificados recientemente (últimas 24h)
find /home -mtime -1 -type f
# Archivos accedidos recientemente
find /tmp -atime -1 -type f
# Archivos creados después de una fecha específica
find /var/log -newermt "2024-01-01"
# Archivos modificados en un rango de tiempo
find /home -newermt "2024-01-01" ! -newermt "2024-01-31"
Terminal window
# Buscar archivos eliminados recientemente (en /proc)
find /proc -name "deleted" 2>/dev/null
# Archivos con extensiones específicas
find /home -iname "*.doc" -o -iname "*.pdf" -o -iname "*.xls"
# Archivos ocultos
find /home -name ".*" -type f
# Archivos con nombres sospechosos
find /home -name "*tmp*" -o -name "*temp*" -o -name ".*"
Terminal window
# Archivos con timestamps anómalos
find /home -newermt "1970-01-01" ! -newermt "1990-01-01"
# Archivos muy grandes
find /home -size +100M -exec ls -lh {} \;
# Duplicados por tamaño
find /home -type f -exec ls -l {} \; | sort -k5 -n | uniq -f4 -D
#!/bin/bash
echo "=== AUDITORÍA DE SEGURIDAD ==="
echo "Archivos SUID:"
find / -perm -4000 -type f 2>/dev/null
echo -e "\nArchivos world-writable:"
find / -perm -002 -type f 2>/dev/null | head -20
echo -e "\nArchivos sin propietario:"
find / -nouser -o -nogroup 2>/dev/null | head -10
echo -e "\nArchivos de configuración modificados recientemente:"
find /etc -name "*.conf" -mtime -7 2>/dev/null
#!/bin/bash
TARGET_DIR=${1:-/home}
echo "Buscando credenciales en $TARGET_DIR..."
# Archivos con patrones de contraseñas
find "$TARGET_DIR" -type f -exec grep -l -i "password\|passwd\|pwd" {} \; 2>/dev/null
# Claves SSH
find "$TARGET_DIR" -name "id_*" -o -name "*.pem" -o -name "*.key" 2>/dev/null
# Archivos de configuración de bases de datos
find "$TARGET_DIR" -name "*.cnf" -o -name "my.cnf" -o -name "*.conf" 2>/dev/null | xargs grep -l -i "password" 2>/dev/null
Terminal window
# Usar -quit para parar en el primer resultado
find /home -name "target.txt" -quit
# Limitar búsqueda con -maxdepth
find /home -maxdepth 3 -name "*.log"
# Usar -prune para excluir directorios grandes
find /home -path "*/node_modules" -prune -o -name "*.js" -print
Terminal window
# Suprimir errores de permisos
find /home -name "*.txt" 2>/dev/null
# Redirigir errores a archivo
find /home -name "*.txt" 2>errors.log
# Separar salida y errores
find /home -name "*.txt" >results.txt 2>errors.log
Terminal window
# Archivos temporales antiguos
find /tmp -type f -atime +7 -delete
# Logs antiguos
find /var/log -name "*.log" -mtime +30 -exec gzip {} \;
# Archivos de core dump
find /home -name "core.*" -size +10M -delete
Terminal window
# Archivos modificados para backup incremental
find /home -mtime -1 -type f -exec cp {} /backup/ \;
# Crear lista de archivos para backup
find /home -name "*.doc" -o -name "*.pdf" > backup_list.txt

Nota de Seguridad: El comando find es extremadamente poderoso. Siempre verifica los comandos antes de ejecutarlos, especialmente cuando uses -delete o -exec. En entornos de producción, prueba primero con -print para verificar qué archivos serán afectados.