Blog / How to find a file in Linux from the command line

How to find a file in Linux from the command line

by SW Team

The find command in Linux is a very versatile tool used to search for files and directories in the file system.

You can search for files based on different criteria such as name, type, size, modification date, among others. It is also possible to process the found files directly from the same command, without needing to use other tools or pass the results through tedious pipes or text files.

Basic syntax of the find command:

find [ruta] [opciones] [expresión]

Usage examples:

Search for files by name:

find /ruta/donde/buscar -name "nombre_archivo"

This searches for a file named "filename" within the specified path.

Search for files by extension:

find /ruta/donde/buscar -name "*.txt"

Searches for all files with the .txt extension in the specified path.

Search for files by size:

find /ruta/donde/buscar -size +50M

Finds files larger than 50 MB.

Search for files by type (directory or file):

find /ruta/donde/buscar -type d

Finds all directories within the specified path.

Or, conversely:

find /ruta/donde/buscar -type f

Finds all files within the specified path.

Search for files modified in the last n days:

find /ruta/donde/buscar -mtime -7

Finds files that were modified in the last 7 days.

Execute a command on each found file:

find /ruta/donde/buscar -name "*.log" -exec rm {} \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;

Finds all files with the .log extension and deletes them.

Common options:

-name search by file name. -type search by type (f for file, d for directory). -size search by size. -mtime search by modification date. -exec to execute a command on the found files.

The flexibility of the find command allows for very specific searches.

i