Find File By Name in Linux

Updated: 6 days ago

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 post will cover how to find file by name in Linux.



Find Syntax

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 File By Name in Current Directory and Subdirectory

To list all files in the current directory, we can use this command. ./ means current directory here.

find ./

If we need to list the file which name is 'test', we can use this command.

find ./ -name 'test'

If there is more than one file which name has 'test' in it like test1, test2, we can use this command.

find ./ -name '*test*'



Find File By Name 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 directory, 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 File by Name with Wildcards

We can use basic shell wildcard characters to broaden our search. For instance, the asterisk (*) represents any number of characters:

$ find ~ -iname "foo*"
/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

A question mark (?) represents a single character:

$ find ~ -iname "foo*.???"
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

This isn't regular expression syntax, so the dot (.) represents a literal dot in this example.


Find File by Name in Absolute path

Absolute path is a complete path from the start of the actual filesystem from / directory.


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

find /opt  -name .profile -print

This command searches the /opt directory and prints the complete path names of all files named .profile.


The /opt (slash) instructs the find command to search the /opt 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 Multiple Files by Names

Here is a little complex example. This command will remove all files named a.out or *.o that are not accessed for a week and that are not mounted by using nfs.


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 File by Name with not operator

This Linux find command using the “not” operator creates a list of all files not ending with the .html file extension (filename pattern).

find . -type f -not -name "*.html"

We can also use the following command to get this.

find . -type f ! -name "*.html"

Find File by Name and other options

  • find / -name "file.txt" -size +4M

  • find /dev/ -type b -name "sda*"

  • find / -type d -name "a.txt"

  • find /opt -type f -name 'howtouselinux' -mtime +1

Related:

20 Advanced Linux Find Command Examples

How to use Find Command in Linux

10 Linux Find Exec examples - Advanced Part




259 views
屏幕快照 2021-08-08 下午5.16.32.png