Skip to Content

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]

Boost Your Website Speed!

If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!
  • 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.

 

Option Description
-name Search for files by name
-iname Case-insensitive search for files by name
-type Search for files by type (e.g., f for regular files)
-size Search for files by size
-mtime Search for files by modification time
-atime Search for files by access time
-ctime Search for files by change time
-user Search for files by owner
-group Search for files by group
-perm Search for files by permissions
-empty Search for empty files or directories
-not Invert the following expression or conditions
-and Combine multiple expressions or conditions with logical AND
-or Combine multiple expressions or conditions with logical OR
-exec Execute a command on each matching file
-execdir Execute a command on the directory containing the file
-print Print the path of the matched files (default action)
-delete Delete the matched files
-maxdepth Set the maximum depth of the search
-mindepth Set the minimum depth of the search
-follow Follow symbolic links during the search
-regex Search for files using a regular expression pattern
-printf Print 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

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 Type Symbol Example
Regular file file.txt, image.jpg, program.exe
Directory d Documents/, Downloads/
Symbolic link l mylink -> /path/to/target
Socket s mysql.sock, apache2.sock
Character device c ttyUSB0, console
Block device b sda, sdb1
FIFO (named pipe) p mypipe
Type Option Example
Regular file -type f find . -type f (finds all regular files in the current directory and its subdirectories)
Directory -type d find . -type d (finds all directories in the current directory and its subdirectories)
Symbolic link -type l find . -type l (finds all symbolic links in the current directory and its subdirectories)
Socket -type s find . -type s (finds all sockets in the current directory and its subdirectories)
Character device -type c find . -type c (finds all character devices in the current directory and its subdirectories)
Block device -type b find . -type b (finds all block devices in the current directory and its subdirectories)
FIFO -type p find . -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.

Permission Symbol Description
Read r The ability to read the contents of a file or view the contents of a directory
Write w The ability to modify the contents of a file or create, rename, or delete files within a directory
Execute x The 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.

2 ways to check 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: