Saturday, April 15, 2023

Practical examples of Linux Find command

The find command in Linux is a powerful utility used to search for files and directories in a given directory hierarchy. It is a command-line tool that offers many options to filter and refine the search results.


Here's a step-by-step guide on how to use the find command with examples for various switches:


1. Basic usage

The basic usage of the find command is as follows:

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


This command searches for files with the given name in the specified path.

Example: To find all files named example.txt in the current directory and its subdirectories, use the following command:

find . -name "example.txt"


2. Search by file type

You can use the -type option to search for files of a specific type.

Example: To find all directories in the current directory and its subdirectories, use the following command:


find . -type d

To find all files in the current directory and its subdirectories, use the following command:

find . -type f


3. Search by file size

You can use the -size option to search for files of a specific size. The size can be specified in bytes, kilobytes, megabytes, or gigabytes.

Example: To find all files in the current directory and its subdirectories that are larger than 1 MB, use the following command:

find . -size +1M


4. Search by modification time

You can use the -mtime option to search for files based on their modification time. The time can be specified in days.

Example: To find all files in the current directory and its subdirectories that have been modified within the last 7 days, use the following command:

find . -mtime -7


5. Search by user or group

You can use the -user and -group options to search for files owned by a specific user or group.

Example: To find all files in the current directory and its subdirectories that are owned by the user john, use the following command:

find . -user john


To find all files in the current directory and its subdirectories that are owned by the group developers, use the following command:

find . -group developers


6. Search by permissions:

You can use the -perm option to search for files based on their permissions.

Example: To find all files in the current directory and its subdirectories that have read and write permissions for the owner and read permissions for others, use the following command:

find . -perm 644


7. Search by name with wildcard:

You can use the * and ? characters as wildcards to search for files with a pattern in their name.

Example: To find all files in the current directory and its subdirectories that have the word example in their name, use the following command:

find . -name "*example*"


8. Search by exclusion:

You can use the ! character to exclude files or directories from the search results.

Example: To find all files in the current directory and its subdirectories that are not named example.txt, use the following command:

find . ! -name "example.txt"


These are just a few examples of the many options available with the find command in Linux. By combining these options, you can perform complex searches to locate the files or directories you need.

No comments:

Post a Comment