Are you new to Linux and wondering how to efficiently search for files and directories on your system? Look no further than the powerful “find” command. Finding files and directories is an essential task for any Linux user, and the find command provides a flexible and comprehensive solution. In this article, we will explore 35 practical examples of how to use the find command effectively, making your Linux journey smoother and more productive.

Searching for a file by name:

To locate a file with a specific name, use the following command:

   find /path/to/search -name "filename"

Replace “/path/to/search” with the directory where you want to start the search and “filename” with the name of the file you are looking for.

Finding files with a specific extension:

If you are searching for files with a particular extension, use the following command:

   find /path/to/search -name "*.extension"

Replace “.extension” with the desired file extension, such as “.txt” for text files or “*.jpg” for JPEG images.

Ignoring case sensitivity in file names:

To perform a case-insensitive search, add the “-iname” option to the command:

   find /path/to/search -iname "filename"

This will match files regardless of their case in the name.

Searching for files owned by a specific user:

If you want to find files owned by a particular user, use the “-user” option:

   find /path/to/search -user username

Replace “username” with the username of the desired owner.

Finding files based on their size:

You can locate files based on their size using the following options:

  • To find files larger than a specific size (in kilobytes), use “-size +Nk”:
    bash find /path/to/search -size +Nk
  • To find files smaller than a specific size, use “-size -Nk”:
    bash find /path/to/search -size -Nk
  • To search for files of an exact size, use “-size Nk”:
    bash find /path/to/search -size Nk
    Replace “N” with the desired size in kilobytes.

Searching for empty files or directories:

To find empty files, use the “-empty” option:

   find /path/to/search -type f -empty

To find empty directories, use “-type d” instead:

   find /path/to/search -type d -empty

Finding files modified within a specific time range:

If you need to locate files modified within a certain time frame, use the “-mtime” option:

To find files modified within the last N days:
bash find /path/to/search -mtime -N

To find files modified exactly N days ago:
bash find /path/to/search -mtime N

To find files modified more than N days ago:
bash find /path/to/search -mtime +N
Replace “N” with the desired number of days.

Searching for files based on their permissions:

To find files with specific permissions, use the “-perm” option:

   find /path/to/search -perm mode

Replace “mode” with the desired permission mode, such as “644” for

read/write permissions for the owner and read-only permissions for others.

Finding broken symbolic links:

To locate broken symbolic links, use the “-type l” option with the “-xtype” option:

   find /path/to/search -xtype l

Searching for files with specific content:

To find files containing specific text, utilize the “grep” command in conjunction with the find command: find /path/to/search -type f -exec grep -l "search_term" {} + Replace “search_term” with the text you want to search for.

Executing commands on found files:

The find command allows you to perform actions on the files it finds. For example, to delete all .txt files in a directory and its subdirectories: find /path/to/search -name "*.txt" -exec rm {} + Be cautious when using the “-exec” option, as it executes commands on the found files. Make sure to double-check your command before running it.

Searching for files modified by a specific user:

To find files modified by a particular user, use the “-mtime” and “-user” options together: find /path/to/search -mtime -N -user username Replace “N” with the desired number of days and “username” with the desired user.

Searching for specific file types:

If you are looking for specific file types, you can use the “-type” option. For example, to find only directories: find /path/to/search -type d Replace “d” with the desired file type. You can use “f” for regular files, “l” for symbolic links, and “s” for sockets, among others.

Finding files based on their group ownership:

To search for files owned by a specific group, use the “-group” option: find /path/to/search -group groupname Replace “groupname” with the name of the desired group.

Searching for files with multiple criteria:

You can combine multiple criteria to narrow down your search. For example, to find files with a specific extension and modified within the last 7 days: find /path/to/search -name "*.extension" -mtime -7

Searching for files excluding certain directories:

If you want to exclude specific directories from your search, use the “-not” and “-path” options: find /path/to/search -not -path "/path/to/exclude/*" Replace “/path/to/exclude” with the directory you want to exclude.

Limiting the depth of the search:

By default, the find command searches recursively through all subdirectories. To limit the search depth, use the “-maxdepth” option: find /path/to/search -maxdepth N Replace “N” with the desired depth.

Searching for files based on their access time:

The “-atime” option allows you to find files based on their last access time. For example, to find files accessed within the last 30 days: find /path/to/search -atime -30

Finding files with specific permissions and ownership:

You can combine permission and ownership criteria using the “-perm” and “-user” options:
bash find /path/to/search -perm mode -user username
Replace “mode” with the desired permission mode and “

username” with the desired owner.

Searching for files based on their group permissions:

To find files with specific group permissions, use the “-perm” and “-group” options together: find /path/to/search -perm mode -group groupname Replace “mode” with the desired permission mode and “groupname” with the desired group.

Finding files with a specific file size:

If you want to search for files with a specific size, use the “-size” option: find /path/to/search -size N Replace “N” with the desired size. You can use suffixes like “k” for kilobytes, “M” for megabytes, and “G” for gigabytes.

Searching for files modified before a specific date:

The “-newermt” option allows you to find files modified before a specific date. For example, to find files modified before January 1, 2023: find /path/to/search -newermt "2023-01-01"

Finding files based on their inode number:

To search for files using their inode number, use the “-inum” option: find /path/to/search -inum inode_number Replace “inode_number” with the desired inode number.

Searching for files with specific permissions, excluding others:

You can find files with specific permissions while excluding others using the “-perm” and “-not” options: find /path/to/search -perm mode -not -perm other_mode Replace “mode” with the desired permission mode and “other_mode” with the permission mode to exclude.

Finding the newest or oldest files:

To find the newest or oldest files in a directory, use the “-newer” or “-older” options with a reference file: find /path/to/search -newer reference_file find /path/to/search -older reference_file Replace “reference_file” with the file you want to use as a reference.

Searching for files with specific file permissions:

If you need to find files with specific file permissions, use the “-perm” option with octal values: find /path/to/search -perm 644

Finding files owned by a specific user or group:

You can search for files owned by a specific user or group using the “-user” and “-group” options: find /path/to/search -user username find /path/to/search -group groupname Replace “username” with the desired user and “groupname” with the desired group.

Searching for files by their last modification time:

The “-mtime” option allows you to find files based on their last modification time. For example, to find files modified within the last 24 hours: find /path/to/search -mtime 0

Finding files with specific permissions and modification times:

You can combine permission and modification time criteria using the “-perm” and “-mtime” options: find /path/to/search -perm mode -mtime -N Replace “mode” with the desired permission mode and “N” with the desired number of days.

Searching for empty files or directories based on their size:

To find empty files or directories based on their size, use the “-empty” and “-type” options together:
“`bash
find /path/to/search –

type f -empty
find /path/to/search -type d -empty
“`

Finding files based on their file type and permissions:

You can combine file type and permission criteria using the “-type” and “-perm” options: find /path/to/search -type f -perm mode Replace “mode” with the desired permission mode.

Searching for files using regular expressions:

The find command allows you to search for files using regular expressions. For example, to find files with names starting with “file” and ending with a number: find /path/to/search -regex ".*/file[0-9]+$"

Finding files excluding specific patterns:

If you want to exclude files matching specific patterns, use the “-not” and “-name” options: find /path/to/search -not -name "pattern" Replace “pattern” with the pattern you want to exclude.

Searching for files and performing case-insensitive matching:

To perform a case-insensitive search, use the “-iname” option with the “-name” option: find /path/to/search -iname "filename" This will match files regardless of their case in the name.

Searching for files and limiting the number of results:

If you want to limit the number of search results, you can use the “head” command in combination with the find command:
bash find /path/to/search -name "filename" | head -n N
Replace “filename” with the desired file name and “N” with the number of results to display.

With these 35 practical examples of the Linux find command, you now have a powerful tool at your disposal to search for files and directories on your Linux system. Experiment with these commands to find the files you need efficiently and save time. Remember to double-check your commands before executing them to avoid accidental file deletions or modifications. Happy searching!

Comments to: 35 Practical Examples of the Linux Find Command: Simplified Guide for Beginners

    Your email address will not be published. Required fields are marked *