howtouselinux

6 Examples to Find Files in Linux with Find Command

Table of Contents

The Linux find command is a powerful tool that enables system administrators to locate and manage files and directories based on a wide range of search criteria. 

Find command searches for files and directories in a directory hierarchy based on a user-given expression and can perform user-specified action on each matched file.

It can find directories and files by their name, their type, or extension, size, permissions, etc.

Find Command Syntax

The general syntax for the find command is as follows:

find [options] [path…] [expression]

  • The options attribute controls the treatment of the symbolic links, debugging options, and optimization method.
  • The path… attribute defines the starting directory or directories where find will search the files.
  • The expression attribute is made up of options, search patterns, and actions separated by operators.

 

What the find Command Does

The “find” command is a powerful tool that allows you to search for files based on a variety of criteria.

You can use the “find” command to search for files by name, by type, by modification time, and by file owner. You can also use the “find” command to search for files in a specific directory or in a specific file system.

How to Use the find Command

To search for files based on a specific filename, you can use the “find” command with the “-name” option. For example, if you want to find all of the files that have the word “file” in their name, you can run the following command:

“find . -name ‘*file*'”

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name.

If you want to find all of the files that have the word “file” at the beginning of their name, you can use the following command:

“find . -name ‘file*'”

This command will search through the current directory and all of its subdirectories for files that have the word “file” at the beginning of their name.

Advanced options in Find command

The “find” command also allows you to use advanced search options to filter results.

  • “find” command with the “-type” option to search for files of a specific type.
  • “find” command with the “-mtime” option to search for files that have been modified in a certain amount of time.
  •  “find” command with the “-maxdepth” option to specify how deep you want to search into directories.

 

For example, if you want to find all of the PDF files that are older than one week, you can run the following command: “find . -type f -mtime +7”

This command will search through the current directory and all of its subdirectories for PDF files that have been modified more than seven days ago.

Excluding Files and Directories in Find command

You can use the “find” command with the “-exclude” option to exclude certain files from your search. For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the PDF files, you can run the following command:

find . -name ‘file*’ -exclude *.pdf

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the PDF files.

You can also use the “-exclude-dir” option to exclude certain directories from your search. For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the files in the “tmp” directory, you can run the following command:

“find . -name ‘file*’ -exclude-dir tmp

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the files in the “tmp” directory.

Find Files with Name in Linux

  • find . -name howtouselinux.txt Find Files Using Name in Current Directory
  • find /home -name howtouselinux.txt Find Files Under Home Directory
  • find / -type d -name howtouselinux Find Directories Using Name
  • find . -type f -name “*.txt” Find all txt Files in Current Directory

Find Files with Permission in Linux

  • find . -type f -perm 0777 -print Find Files With 777 Permissions
  • find / -type f ! -perm 777 Find Files Without 777 Permissions
  • find / -perm 2644 Find SGID Files with 644 Permissions
  • find / -perm 1551 Find Sticky Bit Files with 551 Permissions

Find Files with User and Group in Linux

  • find /home -user howtouselinux Find all Files Based on User
  • find /home -group howtouselinux Find all Files Based on Group

Find Files and Directories Based on Date and Time in Linux

  • find / -mtime 50 Find Last 50 Days Modified Files
  • find / -atime 50 Find Last 50 Days Accessed Files
  • find / -mtime +50 –mtime -100 Find Last 50-100 Days Modified Files
  • find / -cmin -60 Find Changed Files in Last 1 Hour

Find Files and Directories Based on Size in Linux

  • find / -size 50M Find 50MB Files
  • find / -size +50M -size -100M Find Size between 50MB – 100MB
  • find / -type f -size +100M find larger than 100MB files
  • find / -type f -name *.mp3 -size +10M Find all .mp3 files with more than 10MB

Find Files with Not operator in Linux

  • find . -type f -not -name “*.html” Find files not ending with the .html file extension
  • find . -type f ! -name “*.html” Find files not ending with the .html file extension

Related:

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.