20 Advanced Linux Find Command Examples

Updated: Aug 2

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 and also to any directories and files within them.


This post covers 20 advanced Linx Find command examples.




When using find, we would follow the syntax below.

find [options] [path] [expression]
  • options: This is optional. We can leave this out most of the time. We can read the manual page for all the options the find command has.

  • path: This is the directory we want to search.

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


Find Files with a specific file name

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

find /  -name .profile -print

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 a specific permission

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

find . -perm 0600  -print


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

Find files in multiple directories

If we want to search and list all files with a given name in multiple directories, we can either start the search at root folder, or if we know the directories, we can specify them.

Example:

find ./test ./logs -name failed*.* -type f

Sample output:

./test/failed_tests.txt
./logs/failed_tests.log

Find Files with change time

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

To search for regular files with multiple links. This command lists the names of the ordinary files (-type f) that have more than one link (-links +1).


Note: Every directory has at least two links: the entry in its parent directory and its own . (dot) entry. The ln command explains multiple file links.

find .  -type f  -links +1  -print

Find Files with names

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

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

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

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. We can check more about Linux find exec command here.


Find Files with Links

In this example, dirlink is a symbolic link to the directory dir. To list the files in dir by referring to the symbolic link dirlink on the command line, type:


find -H dirlink -print


Find files with ls command

To produce a listing of files in the current directory in ls format with expanded user and group name, type:


find . -exec ls {} \; 

Find Files with ACL

To list the files with ACL/EA set in current directory, type:


find . -ea

Find Files with modifying time

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


find . -mmin -60



Related:

Find File By Name in Linux

20 Advanced Linux Find Command Examples

How to use Find Command in Linux

3 Ways to find the largest files in Linux

10 Linux Find Exec examples - Advanced Part

225 views