El find command It is one of the most important in the world * nix. In Linux it can be used to locate everything you need, such as directories and files. In addition, it is extremely powerful and flexible, since it supports arguments and options to create practical filters (date, size, type, name, extension,…). It can even be a practical tool to audit the security of the distro, since it will also be able to locate files or directories with inappropriate permissions.
However, due to this versatility and number of options, it is not the easiest command to remember, and many users still have certain problems. Therefore, here you will see some practical examples one of the most practical for you to learn to defend yourself with find:
- Search for a file or directory by name (in current directory, in all directories, and case sensitive):
find . -name "ejemplo.txt" find / -name "ejemplo.txt" find . -iname "ejemplo.txt"
- Search for a file or directory by name within a specific directory:
find /home/usuario/prueba -name "ejemplo.txt"
- Find all directories (you can use l for symbolic links, c for character devices, f for files, and b for block devices) and avoid files, or use the name as well:
find /home/usuario/prueba -type d find /home/usuario/prueba -type d -name "ejemplo"
- Search for files with a particular extension:
find . -type f -name "*.txt"
- Search for files by name and delete it:
find . -name "ejemplo.txt" -delete
- Find all accessed files older than 10 years, or you can also do it by modification date in the last 60 min and by date of changes in less than 1 day:
find / -atime 10 find / -mmin -60 find / -ctime -1
- Find files that are larger than 500MB and smaller than 1GB:
find / -size +500M -size -1G
- Find files larger than 10GB and delete them in one go:
find / -size +10G -exec rm -rfv {} \;
- Find files that belong to a user or group:
find / -user nombre find / -group nombre
- Search for files that have specific permissions:
find / -perm 644
- Search for empty files (if you change f to d you can search for empty directories):
find / -type f -empty
- Search for hidden files (d instead of f for hidden directories):
find / -type f -name ".*"
- Search for text within files:
find / -type f -name "*.txt" -exec grep 'texto-a-buscar' {} \;