20 Advanced Linux Find Command Examples

Linux find command is a powerful tool that can be used to locate and manage files and directories based on a wide range of search criteria.

This means that we can provide it with a set of directories (or files) or filters, and it will apply appropriate actions to them.

This post covers 20 advanced Linx Find command examples. When using find, we would follow the syntax below.

find  [path] [expression]

  • path: This is the directory we want to search.
  • options: This is where we place our search criteria for what we want to find whether by name, or size etc.

 

OptionDescription
-nameSearch for files by name
-inameCase-insensitive search for files by name
-typeSearch for files by type (e.g., f for regular files)
-sizeSearch for files by size
-mtimeSearch for files by modification time
-atimeSearch for files by access time
-ctimeSearch for files by change time
-userSearch for files by owner
-groupSearch for files by group
-permSearch for files by permissions
-emptySearch for empty files or directories
-notInvert the following expression or conditions
-andCombine multiple expressions or conditions with logical AND
-orCombine multiple expressions or conditions with logical OR
-execExecute a command on each matching file
-execdirExecute a command on the directory containing the file
-printPrint the path of the matched files (default action)
-deleteDelete the matched files
-maxdepthSet the maximum depth of the search
-mindepthSet the minimum depth of the search
-followFollow symbolic links during the search
-regexSearch for files using a regular expression pattern
-printfPrint specific file information using a format specifier

Find Comand in Linux

These options provide a variety of ways to customize and narrow down your search criteria when using the find command in Linux.

Remember to consult the man page for find (man find) for more detailed information on each option and additional options available.

Find Files with a specific file name in Linux

To list all files in the file system with a specified base file name, type:

find / -name .profile

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

This command searches the entire file system and writes the complete path names of all files named .profile.

The / (slash) instructs the find command to search the root directory and all of its subdirectories.

In order not to waste time, it is best to limit the search by specifying the directories where we think the files might be.

Find Files with file type in Linux

In Linux, every file is associated with a type, which indicates the kind of data that the file contains.

The file type is represented by a single character that appears as the first character in the output of the ls -l command.

For example, a regular file is represented by a hyphen (-), a directory is represented by the letter d, and a symbolic link is represented by the letter l.

File TypeSymbolExample
Regular filefile.txt, image.jpg, program.exe
DirectorydDocuments/, Downloads/
Symbolic linklmylink -> /path/to/target
Socketsmysql.sock, apache2.sock
Character devicecttyUSB0, console
Block devicebsda, sdb1
FIFO (named pipe)pmypipe
TypeOptionExample
Regular file-type ffind . -type f (finds all regular files in the current directory and its subdirectories)
Directory-type dfind . -type d (finds all directories in the current directory and its subdirectories)
Symbolic link-type lfind . -type l (finds all symbolic links in the current directory and its subdirectories)
Socket-type sfind . -type s (finds all sockets in the current directory and its subdirectories)
Character device-type cfind . -type c (finds all character devices in the current directory and its subdirectories)
Block device-type bfind . -type b (finds all block devices in the current directory and its subdirectories)
FIFO-type pfind . -type p (finds all FIFOs in the current directory and its subdirectories)

These are some of the most common file types that you may want to search for using the find command in Linux.

Find Files with a specific permission in Linux

In Linux, each file has a set of permissions that determine who can access the file and what actions they can perform on it.

The permissions are represented by a series of symbols that appear in the output of the ls -l command.

For example, rwxr-xr– indicates that the file owner has read, write, and execute permissions, users in the file’s group have read and execute permissions, and all other users have only read permissions.

PermissionSymbolDescription
ReadrThe ability to read the contents of a file or view the contents of a directory
WritewThe ability to modify the contents of a file or create, rename, or delete files within a directory
ExecutexThe ability to execute a file or enter a directory

To list files that have a specific permission code in the current directory tree, type:

find . -perm 0600

This command lists the names of the files that have only owner-read and owner-write permission. The . (dot) instructs the find command to search the current directory and its subdirectories.

To search several directories for files with certain permission codes, type:

find dira dirb dirc -perm -0600 -print

We highly recommend the following article to get more info about file permissions in Linux.

Find files in multiple directories in Linux

You can specify multiple directories to search for files by providing their paths separated by spaces. For example, to search for files named file.txt in the /home/user1 and /home/user2 directories, you can use the following command:

find /home/user1 /home/user2 -name file.txt

This command will search for files named file.txt in both /home/user1 and /home/user2 directories.

Example:

find ./test ./logs -name failed*.* -type f
./test/failed_tests.txt
./logs/failed_tests.log

Find Files with change time in Linux

To list all files in the current directory that are changed during the current 24-hour period, type:

find . -ctime 1 -print

Find Files with multiple Links in Linux

 This command lists the names of the ordinary files (-type f) that have more than one link (-links +1).

find . -type f -links +1 -print

Note: Every directory has at least two links: the entry in its parent directory and its own . (dot) entry.

Find Files with names in Linux

To find all accessible files whose path name contains find, type:

find . -name '*find*' -print

To remove all files named a.out or *.o that are not accessed for a week and that are not mounted by using nfs, type:

find / \( -name a.out -o -name '*.o' \) -atime +7 ! -fstype nfs -exec rm {} \;

Note: The number that is used within the -atime expression is +7. It is the correct entry if we want the command to act on files that are not accessed for more than a week (seven 24-hour periods).

Find Files with Files size in Linux

To search for all files that are exactly 414 bytes long, type:

find . -size 414c -print

And to find files that are greater than a certain size, we use:

find ./test -size +2M

The above will find all the files which are greater than 2MB in the ./test folder. When looking for files within a specific range such as between 100 and 200 MB

find / -size +100M -size -200M

Find Large files in Linux

The first step of extracting files larger than 200 MB was a success. The next target is to get the files sorted according to their sizes. This can be done by:

find / -xdev -type f -size +200M | xargs du | sort -k 1 -rh

Find exec command Combination in Linux

To find and remove every file in our home directory with the .c suffix, type:

find /u/arnold -name "*.c" -exec rm {} \;

Every time the find command identifies a file with the .c suffix, the rm command deletes that file.

The rm command is the only parameter that is specified for the -exec expression. The {} (braces) represent the current path name.

You can check more about Linux find exec command here.

Find Files with modifying time in Linux

In Linux, you can use the find command to search for files based on their modification time using the -mtime option. Here is the syntax of the command:

find [path] -mtime [n]

Here is a brief explanation of each element of the command:

[path]: This is the starting point for the search. You can specify a directory or file to start the search from. If you don’t specify a path, the search starts from the current directory.

-mtime [n]: This option allows you to search for files based on their modification time. The [n] parameter specifies the time frame in days. For example, -mtime 0 will search for files that were modified within the last 24 hours, while -mtime +7 will search for files that were modified more than 7 days ago.

For example, to find all files in the current directory and its subdirectories that were modified within the last 24 hours, you can use the following command:

find . -mtime 0

This command starts the search from the current directory (.) and searches for files that were modified within the last 24 hours (-mtime 0).

To list the files that are modified within 60 minutes, type:

find . -mmin -60

Related:

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275

Leave a Reply

Your email address will not be published. Required fields are marked *