fbpx Linux find Command: Locate Files (Examples) | Certificação Linux

Linux find Command: Locate Files by Name, Type and Date

📅 01/01/2019 ⏱ 3 min ✍️ Uira Ribeiro
Linuxcommands
Linux find Command: Locate Files by Name, Type and Date
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The find command searches for files and directories directly in the file system. Depending on the size of the file system, this command can be very time-consuming.

Its basic syntax is:

$ find [caminho] [expressão]

You must specify a starting point for the search in the Linux directory tree. If you want it to start at the root, specify "/". If you want the search to start from the current directory, use the path "./" or simply dot ".".

The find command allows a wide range of expressions as search options, such as name, size, file creation date, access date, file type, permissions, etc.

Examples:

Searches for the file texto.txt starting from the root (/), of file type (f):

$ find / -name texto.txt -type f  

Searches for all files with .txt extension starting from the current directory (.):

$ find . -name “*.txt”

Searches for the directory "usuarios" starting from the current directory, regardless of whether it is written in uppercase or lowercase letters:

$ find . -iname usuarios -type d   

Searches for files that do not end with .html starting from the current directory:

$ find . -type f -not -name “*.html”

The find command also allows you to execute a command with the list of files it finds. In this example, find copies all .mp3 files found starting from the current directory to /tmp:

$ find . -type f -name “*.mp3” -exec cp {} /tmp/ \;

Deletes all BACKUP directories found starting from the current directory:

$ find . -type d -name BACKUP -exec rm -r {} \;

Searches for files modified in the last 7 days:

$ find . -mtime -7 -type f

Search for files edited more than 5 days ago

$ find -mtime +5

Deletes backup files older than 15 days:

$ find /backup/ -type f -mtime +15 -exec rm -f {} \;

Deletes all object files found starting from the current directory:

$ find . -name "*.o" -type f -exec rm -f {} \;

Copies all files modified in the last 2 days to the /tmp directory:

$ find . -type f -mtime -2 -exec cp {} /tmp \;

It is also possible to search for a file newer than a given file:

$ find -newer arquivo-fonte.c

Searches for files with 0777 permissions:

$ find . -type f -perm 0777 –print

Searches for all files larger than 50MB:

$ find / -size +50M 

You can combine options, such as searching for files with .php extension modified in the last 48 hours:

$ find -name '*.php' -mtime -2

The find command can also be used to create a list of files in a directory, which can be sorted with the sort command:

$ find | sort../CapituloForm.php./CapituloList.php./Form.php./List.php./SubcapituloForm.php./SubcapituloList.php

Conclusion

The find command is a powerful tool for finding files and directories, based on name, permissions, attributes, size, ownership (by user or by group), etc. It can be used to scan the system to find programs with suid permissions, fix permissions, perform backups, etc.

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Liked it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp