Categories: File Management

Find command on Linux (find files) [Basic Guide]

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

Its basic syntax is:

$ find [path] [expression]

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

Find allows a multitude of expressions such as search options, such as name, size, file creation date, access date, by file type, permissions, etc.

Examples:

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

$ find/-name texto.txt -type f

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

$ find. -name “*.txt”

Search for the “users” directory from the current directory, regardless of whether it is written with capital or lower case letters:

$ find. -iname users -type d

Search for files that don’t end with .html from the current directory:

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

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

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

Delete all BACKUP directories found from the current directory:

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

Search for files modified in the last 7 days:

$ find. -mtime -7 -type f

Search for files edited before 5 days

$ find -mtime +5

Delete backup files older than 15 days:

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

Delete all object files found from the current directory:

$ find. -name “*.o” -type f -exec rm -f {};

Copy all files changed in the last 2 days to the /tmp directory:

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

You can also search for a file that is more recent than a particular file:

$ find -newer source-file.c

Search for files with permission 0777:

$ find. -type f -perm 0777 —print

Search for all files larger than 50Mb:

$ find/-size +50M

You can combine the options, such as to search for files with a .php extension changed in the last 48 hours:

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

Find can also be used to create a list of files in a directory, which can be ordered 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, directories, based on name, permissions, attributes, size, ownership (per user or per group), etc. It can be used to scan the system to find programs with suid permissions, correct permissions, make backups, etc.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.