How to Find Files in Linux From Terminal - Many Examples With Different Options

1- Introduction

Especially when we work on a Linux server, sometimes there is a need to find some files on the server.

In this article, we will examine the find UNIX command that we can easily use in the terminal for searching files and directories in Linux system.

2- Basic Usage Template

find [start_directory] [options] [expression]
  • [start_directory]: This parameter is a directory where the search will start to look at from. We can specify any directory as the starting point.
  • [options]: Additional parameters (options) that change the behavior of the "find" command. They can be size, file type, created, modified or accessed time and ownership (user, group, perm), etc.
  • [expression]: This is a pattern or condition that specifies what we are looking for.

2.1 - Usage Example To Find Only Files

find / -type f -name "log-file.log"

This find command searches for files from the root directory ("/"), for the named by "log-file.log".

  • The / directory is where the search will begin.
  • Using -type parameter to search only files.
  • Using -name parameter to search pattern. (The match is case sensitive)
Note: name parameter is case sensitivity. Use -iname parameter to ignore case sensitivity Instead of -name parameter.
Note 2 : We can use asterisks if we do not know the exact name of the file. and we will see in the examples.


Note: We can specify that only searches for files or directories with options parameter.

3 - Filtering Types

We can specify the type for searching.

Using the -type option;

  • f: To search any type of file (including hidden files)
  • d: To Search directories.
  • s: socket
  • c : Character Device
  • s : Socket
  • p : Named pipe
  • l Symbolic link

Not: We can use multiple types for searching.

find / -type f,d -name "something"

For example, we will use f and d type options for the examples of directories and files searching.

4- Before Examples

Let me create some directories and files in my local machine.

On my local machine;

  • /home/volkan/find-example
    - "text-file.txt" (10 byte)
    - "test-note.txt" (10 byte)
    - log-file.gz (10 byte)
    - text-file.gz (10 byte)
  • /home/volkan/find-example/log-files
    - 5mb-log-file.log (Appx. 5.3 MB)
    - 10mb-log-file.log (Appx. 10.3 MB)
  • /home/volkan/find-example/dump-files
    - 5mb-dump-file.log (Appx. 5.3 MB)

5- How to find Files in the Linux System

5. 1 - Find Only Files With Exact Name Match

Let's find;

  • Directory to start searching "/home/volkan"
  • Using -type f just for searching the types of files.
  • Using -name parameter with the value"5mb-log-file.log" for searching file names with exact matches.
find /home/volkan -type f -name "5mb-log-file.log"

This find command is looking for "5mb-log-file.log" file from the starting point of /home/volkan directory and search only for files with "-type f" parameter.

Output:

find /home/volkan -type f -name "5mb-log-file.log"
/home/volkan/find-example/log-files/5mb-log-file.log

The first line is our find search command and the second line is about to show the found file where located.

5.2 - Find Only Files With Containing Name Value (Wildcards)
5.2.1 - Example

Let us look for all files which contain "log-file" naming starting from "/home/volkan" directory

find /home/volkan -type f -name "*log-file*"
  • Directory to start searching "/home/volkan"
  • Using -type f just for searching the types of files.
  • Using -name parameter with the value "log-file" to search file names containing the specified value.

Output:

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/log-file.gz
5.2.2 Find Files With Containing Name Value (Case-Sensitivity and Case-Insensitivity)

Now, let us look for all files which contain "log-file" naming starting from "/home/volkan/find-example" directory with case-sensitivity and case-insensitivity.

Firstly if we look at

find /home/volkan/find-example -type f -name "*LOG-FILE*"

Output: Result will be nothing to see.

We converted -name parameter to -iname

find /home/volkan/find-example -type f -iname "*LOG-FILE*"

Output:

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/log-file.gz
5.3 - Find Only Files With Starting/Ending Name Value (Wildcards)
5.3.1 - Find Only Files Starting With

Let us look for all files starting from "/home/volkan" directory and starting with "5mb" a case-sensitive value using the -name parameter.

find /home/volkan -type f -name "5mb*"

Or

find /home/volkan -type f -iname "5mb*"

to ignore case sensitivity.

Output:

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/dump-files/5mb-dump-file.log
5.3.2 - Find Only Files Ending With

Furthermore, let us look for all files starting from "/home/volkan/find-example" directory and ending with a case-insensitive ".gz" value using the -iname parameter.

find /home/volkan/find-example/ -type f -iname "*.gz"

Output:

/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
5.3.3 - Find Only Files Starting and Ending With

Now, let us look for all files starting to search from the "/home/volkan/find-example" directory and starting with "log" value and ending with ".gz" value with a case-insensitive using the -iname parameter

find /home/volkan/find-example/ -type f -iname "log*.gz"

Output

/home/volkan/find-example/log-file.gz
5.3.4 - Find Only Files Containing and Ending With

Morever, let us look for all files starting from "/home/volkan/find-example" directory and containing "og" naming value and ending with a case-insensitive ".gz" value using the -iname parameter.

find /home/volkan/find-example/ -type f -iname "*og*.gz"

Output:

/home/volkan/find-example/log-file.gz
5.3.5 - Find Only Files With Any Single Character (?)

Now, let's find all files containing "te (any-value) t) naming value using the ? wildcard expression and starting to search from "/home/volkan/find-example" directory.

find /home/volkan/find-example/ -type f -iname "*te?t*"

Output:

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/text-file.txt
5.4 - Find Only Files With Greater/Less Size Parameter Value

Let's look for all files which, are greater than 1MB in size starting to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -size +1M

Output:

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/dump-files/5mb-dump-file.log

Notes:

  • We can use
    + for greater than sizes
    - for less than sizes
  • Some letters are;
    - G for filter by gigabytes
    - M for filter by megabytes
    - k for filter by kilobytes
    - c for filter by bytes
Example 2;

Let's look for all files which, are greater than 6MB in size starting the search from "/home/volkan/find-example" directory.

find /home/volkan/find-example/ -type f -size +6M

Output:

/home/volkan/find-example/log-files/10mb-log-file.log
Example 3

Let's look for all files which, are less than 10MB in size starting to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -size -10M

Output;

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log
5.5 - Find Only Files With Created Time
5.5.1 - ctime option

Let's find all files that were created in 1 day using the -ctime parameter and start to search from "home/volkan/find-example" directory.

find /home/volkan/find-example/ -type f -ctime -1

Output

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log
5.5.2 - cmin option

Let's create a new file with the name of "test-file-for-time-examples.txt". Then, look at the files created in 10 minute and starting search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -cmin -10

Output

/home/volkan/find-example/test-file-for-creation-time.txt
5.5.3 - Example -newerct option

Let's look at the files with created time greater than using the -newerct and starting to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -newerct "2023-08-13 17:00:00"

Output:

/home/volkan/find-example/test-file-for-creation-time.txt
5.5.4 - Example of -neverct option - Between Dates

Let's look at the files created time between two dates using the -newerct option and start to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -newerct "2023-08-13 17:00:00" ! -newerct "2023-08-13 17:30:00"

Output:

/home/volkan/find-example/test-file-for-creation-time.txt
5.6 - Find Only Files With Modification Time
5.6.1 - find mtime parameter option

Let's find all files that were modified in 1 day and start to search from "home/volkan/find-example" directory.

find /home/volkan/find-example/ -type f -mtime -1

Output

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/test-file-for-creation-time.txt
/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log
5.6.2 - mmin option

We created a new file with the name "test-file-for-time-examples.txt" in the before example. Then, look at the files that are modified in 1 minute and start to search from "/home/volkan/find-example" directory

Firstly

find /home/volkan/find-example/ -type f -mmin -1

And nothing to see.

Then let's modify the "test-file-for-time-examples.txt" then again execute the same find command;

find /home/volkan/find-example/ -type f -mmin -1

and output:

/home/volkan/find-example/test-file-for-time-examples.txt
5.6.3 - Example -newermt option

Let's look at the files with created time greater than by using the -newermt parameter and starting to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -newermt "2023-08-13 17:00:00"

Output:

/home/volkan/find-example/test-file-for-time-examples.txt
5.6.4 - Example of -neverct option - Modification Date Between Two Dates

Let's look at the files with created time between two dates by using the -newermt option and starting to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -newerct "2023-08-13 17:00:00" ! -newerct "2023-08-13 17:30:00"

Output:

/home/volkan/find-example/test-file-for-time-examples.txt
5.7- Find Only Files With Access Time
5.7.1 - find atime parameter option

Let's find all files that were accessed within 1 day by using the -atime parameter and start to search from "home/volkan/find-example" directory.

find /home/volkan/find-example/ -type f -atime -1

Output:

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/test-file-for-time-examples.txt
/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log
  • -atime parameter for access date in days.
  • -amin parameter for access date in minutes.
5.7.2 - amin option

We created a new file with the name of "test-file-for-time-examples.txt" in the before example. Then, look at the files that were accessed in 1 minute and starting search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -amin -1

Output: Nothing to see

Then let's open (not modify just open) the "test-file-for-time-examples.txt" then again execute the same find command;

find /home/volkan/find-example/ -type f -amin -1

Output:

/home/volkan/find-example/test-file-for-time-examples.txt
5.7.3 - Example -newerat option

Let's look at the files with accessed time greater than using the -newerat and start the searching from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -newerat "2023-08-13 17:00:00"

Output:

/home/volkan/find-example/test-file-for-creation-time.txt
5.7.4 - Example of -neverat option - Between Dates

Let's look at the files with accessed time between two dates using the -newerat option and starting to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -newerat "2023-08-13 17:00:00" ! -newerat "2023-08-13 17:30:00"

Output:

/home/volkan/find-example/test-file-for-creation-time.txt
5.8 - Find All Files using with -user Option

Let's find all files with "user volkan" and start to search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -user volkan

Output

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/test-file-for-time-examples.txt
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log
5.9 - Find All Files With Linux Permission ( 777 e.g)

Let's find all files with "774" permission and start the search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -perm 664

Output: Nothing to see

Note: In this example directories' permissions are 775 and files' permissions are 664

Now, let's find all files with "664" permission and start the search from "/home/volkan/find-example" directory

find /home/volkan/find-example/ -type f -perm 664

Output

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/test-file-for-time-examples.txt
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log
5.10 - Find All Files With Linux Group

Let's find all files using -group with value of "volkan" and start to search from "/home/volkan/find-example" directory.

find /home/volkan/find-example/ -type f -group volkan

Output:

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/test-file-for-time-examples.txt
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log
5.11 Find All Files With Boolean Operators

We can use -not, -and, -or, etc. operators

5.11.1 - Find command with -not

Let's find all files using;

  • using -iname to ignore case sensitive with containing "FILE"
  • using -iname to ignore case sensitive with -not for ".txt"
  • Starting the search from "/home/volkan/find-example" directory.
find /home/volkan/find-example/ -type f -iname "*FILE*" -not -iname "*.txt"
/home/volkan/find-example/test-note.txt
/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/dump-files/5mb-dump-file.log

All files are found with by containing the case-insensitive "FILE" value and do not end with ".txt" value from starting the "/home/volkan/find-example" directory.

5.11.2 - Find command with -or

Let's find all files using;

  • using -iname to ignore case sensitive with containing "FILE"
  • using -iname to ignore case sensitive with -or for ".txt"
  • Starting the search from "/home/volkan/find-example" directory.
find /home/volkan/find-example/ -type f -iname "*FILE*" -or -iname "*.txt"

Output:

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/test-file-for-time-examples.txt
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt
/home/volkan/find-example/dump-files/5mb-dump-file.log

All files are found by containing the case-insensitive "FILE" value or ending with ".txt" value from starting the "/home/volkan/find-example" directory.

5.11.3 - Find command with -and

Let's find all files using;

  • using -iname to ignore case sensitivity and searching for containing the "FILE" value.
  • using -iname to ignore case sensitivity and searching for ends with ".txt" using -and operation
  • Starting the search from "/home/volkan/find-example" directory.
find /home/volkan/find-example/ -type f -iname "*FILE*" -and -iname "*.txt"

Output:

/home/volkan/find-example/test-file-for-time-examples.txt
/home/volkan/find-example/text-file.txt

All files are found by containing the case-insensitive "FILE" value and ending with ".txt" value from starting the "/home/volkan/find-example" directory.

5.12 - Find Only Files in Hidden Folder and Hidden Files

In this example, we just make sure that find command can look for the hidden files in the hidden folders.

For this example, Let's create a new folder under the "/home/volkan/find-example" directory with ".hidden-folder". And then inside of .hidden-folder, let's create the ".hidden-file.log" file.

find /home/volkan/find-example/ -type f -iname "*hidden*"

Output:

/home/volkan/find-example/.hidden-folder/.hidden-file.log

The hidden ".hidden-file.log" file is found in the hidden ".hidden-folder" directory with "find" command.

5.13 - Find Only Empty Files

In the previous example, we created a new empty file under the "/home/volkan/find-example" directory with ".hidden-folder"

Let's find all empty files and start to search from "/home/volkan/find-example" directory.

find /home/volkan/find-example/ -empty

Output:

/home/volkan/find-example/.hidden-folder/.hidden-file.log

6 - Combining Find Command Example

Let's search for;

  • Starting directory of "/home/volkan/find-example/"
  • Only for files -type f
  • User volkan -user volkan
  • Permission 664 -perm 664
  • Created in one day. ctime -1
  • Ignore case sensitivity -iname and Contains "FILE" value and ends with ".log"
find /home/volkan/find-example/ -type f -perm 664 -user volkan -ctime -1 -iname "*FILE*.log"

Or

find /home/volkan/find-example/ -type f -perm 664 -user volkan -ctime -1 -iname "*FILE*" -and -iname "*.log"

Output

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/log-files/10mb-log-file.log
/home/volkan/find-example/.hidden-folder/.hidden-file.log
/home/volkan/find-example/dump-files/5mb-dump-file.log
6.2 Example 2
  • Add -size parameter for the above examle.
find /home/volkan/find-example/ -type f -size -8M -perm 664 -user volkan -ctime -1 -iname "*FILE*.log"

Output:

/home/volkan/find-example/log-files/5mb-log-file.log
/home/volkan/find-example/dump-files/5mb-dump-file.log
6.3 Example - 3

We added this not parameter too in addition to the above example.

find /home/volkan/find-example/ -type f -size -8M -perm 664 -user volkan -ctime -1 -not -iname "*FILE*.log"

Output:

/home/volkan/find-example/test-note.txt
/home/volkan/find-example/test-file-for-time-examples.txt
/home/volkan/find-example/text-file.gz
/home/volkan/find-example/log-file.gz
/home/volkan/find-example/text-file.txt

7 - How to find Directories in Linux System

The difference between file and directory searching in the find command is a about the -type parameter value.

  • -type f for the searching any type of file.
  • -type d for the searching any directory.

All above examples for search file are similar for directory searching in the Linux system.

Example:

Let us search directories to contain "file" using -type d with ignore case sensitivity and starting to search from "/home/volkan/find-example" directory.

find /home/volkan/find-example/ -type d -iname "*file*"

Output

/home/volkan/find-example/log-files
/home/volkan/find-example/dump-files

Conclusion

In this article, we covered and explain basically the "find" command and its some parameters to find the files in the Linux system. Also, we tried some examples for them. More examples could be made and the document can be viewed for more information. (In the reference)

Reference