35 Examples of Linux Find Command in Practice

February 27, 2022
lgvps

Linux Find Command  is an essential command-line utility that Unix-like operating system users use frequently. The find command can be used to search and locate files and directories using the conditions you set for files matching the given arguments.

The find command in linux can be used under various conditions, such as to find files by permissions, user groups, file types, date, size, or other criteria.

This article will discuss the most commonly used 35 find commands examples in Linux. The section has been divided into five parts containing basic and advanced find command usage.

  • 1 Basic Command to Find Files with Names
  • 2 Files Based on Their Permissions
  • 3 Search files based on owners and groups
  • 4 Search Files and Directories Based On Date and Time
  • 5 Search Files and Directories Based On Size

Basic Commands to Find Files with Names

Basic Commands to Find Files with Names

If you’ve ever wondered how to find files, then this basic tutorial will show you how. Using the find command, you can find files anywhere on your system. You don’t have to know the entire filename, just the part with the name. And you can use ff -p to find other things, too. For example, if you want to find a ” Sarah” file, you can use the command ff -p.

Locate Files using Name in the Current Directory

Locate all files with lgvps.txt within a current directory.

# find . -name lgvps.txt

./lgvps.txt

Search Files under the Home Directory

All files found under /home directory are listed with the Name.

# find /home -name lgvps.txt

/home/lgvps.txt

Search for Files by Name and ignore Case

All files named lgvps.txt containing capital and smaller letters in the /home directory will be found.

# find /home -iname lgvps.txt

./lgvps.txt
./lgvps.txt

Search Directory Using Your Name

All directories with lgvps names in / directory.

# find / -type d -name lgvps

/lgvps

Search PHP Files by Name

Locate all PHP files with the name lgvps.php within a current directory.

# find . -type f -name lgvps.php

./lgvps.php

All PHP Files are found in the Directory

All PHP files within a directory.

# find . -type f -name "*.php"

./lgvps.php
./login.php
./index.php

Files Based on Their Permissions

To run a search for all files owned by a particular group, you can use the find files command Based on Their Permissions. This command matches files with write permission, writable by everyone, or no permissions. You can specify a group or user in the match expression, too. But keep in mind that it’s not a great idea to use this command if you’re unsure of who owns a file.

  • Locate Files with 777 Permissions

Locate all files with 777 permissions.

# find . -type f -perm 0777 -print
  • Locate Files without 777 Permissions

All files found without permission 777.

# find / -type f ! -perm 777
  • Find SGID Files with 644 Permissions

All SGID bits files with permissions set to 644.

# find / -perm 2644
  • Get 551 Permissions for Sticky Bit Files

All Sticky Bit set file names with 551 permission.

# find / -perm 1551

Locate SUID Files

All SUID set files are available.

# find / -perm /u=s

Locate SGID Files

All SGID set files are available.

# find / -perm /g=s
  • Browse Read-Only Files

All Read-Only file locations.

# find / -perm /u=r
  • Locate executable files

All Executable files.

# find / -perm /a=x
  • Locate Files with 777 Permissions & Chmod to 644

To set permissions to 644, locate all 777 permissions files and use CHMOD.

# find / -type f -perm 0777 -print -exec chmod 644 {} \;
  • Directory Search with 777 Permissions & Chmod to 755

To set permissions to 755, locate all 777 permission directories.

# find / -type d -perm 777 -print -exec chmod 755 {} \;

Locate and delete single files

You can find in lgvps.txt, and then delete it.

# find . -type f -name "lgvps.txt" -exec rm -f {} \;

Find and Remove Multiple Files

You can use this tool to locate and delete multiple files, such as .mp3 or .txt.

# find . -type f -name "*.txt" -exec rm -f {} \;

OR

# find . -type f -name "*.mp3" -exec rm -f {} \;
  • All empty files are listed

To locate all files that are not needed under a given path.

# find /tmp -type f -empty
  • All empty directories

To delete all empty directories that are not under a specific path.

# find /tmp -type d -empty
  • File all Hidden Files

Use the following command to locate hidden files

# find /tmp -type f -name ".*"

Search files based on owners and groups

You can use the find command in Linux to search files and directories that belong to a particular user or group. For example, you can use the command to find all files owned by the user root or the group centos. The find command will not return files and directories owned by other users and groups. But if you need to find specific files that belong to a group, you can use the -name parameter.

  • locate File Based on User

To locate all or a single file called lgvps.txt in / root directory, owner root.

# find / -user root -name lgvps.txt
  • All Files Based On User

To locate all files belonging to user lgvps, go to /home directory.

# find /home -user lgvps
  • All Files Based On Group

To locate all files belonging to the group Designer, go to the directory.

# find /home -group developer
  • Locate Specific Files of Users

To locate all .txt files for user , use lgvps in the /home directory.

# find /home -user lgvps -iname "*.txt"

Search Files and Directories Based On Date and Time

To find files or directories based on date and time, you can use the find command. You just need to add a date or time parameter to the command. The date or time parameter can be found by using the -atime, -mtime, or -ctime options. With the -atime option, you need to enter the number of days in the past, with a plus or minus symbol prepended. The -ctime option will return files modified more recently than the reference file. Both of these options can match files with specific permissions.

  • Find the Last 50 Days Modified Files

To locate all files that were modified 50 Days ago.

# find / -mtime 50
  • Find the Last 50 Days of Accessed Files

To locate all files accessed 50 Days ago.

# find / -atime 50
  • Find the Last 50-100 days Modified Files

To locate all files modified more than 50 day back and less that 100 day.

# find / -mtime +50 –mtime -100
  • Search for Changed Files within the Last 1 Hour

To locate all files that were modified in the last hour.

# find / -cmin -60
  • Modified Files Found in the Last 1 Hour

To locate all files modified within the last hour.

# find / -mmin -60
  • Locate Accessed Files within the Last 1 Hour

To locate all files accessed within the last 1h.

# find / -amin -60

Search Files and Directories Based On Size

Search Files and Directories Based On Size

If you want to search for files but aren’t sure where to start, you can search by size. This command enables you to target specific locations and make your search more meaningful. You can specify the maximum file size in the size field, a number between zero and one trillion bytes. You can also specify a specific folder to search for, a path, or a file pattern. 

  • Find 50MB files

Use to search for 50MB files.

find / -size 50M
  • Choose Size Between 50MB and 100MB

To locate all files greater than 50MB or less than 100MB.

# find / -size +50M -size -100M
  • Locate and Delete 100MB files

You can search for all 100MB files and delete them with one command.

# find / -type f -size +100M -exec rm -f {} \;
  • Find and delete specific files.

Locate all .mp3 files larger than 10MB, and delete them with one command.

# find / -type f -name *.mp3 -size +10M -exec rm {} \;

Read More:

How To List All Running Services Under Systemd In Linux

The right decision!
If you're ready to make an impact in your business, it's time to get started now! Discover our offers and make the right decision!
Get Started Now!
payment gateway method for lgvps company
payment gateway method for lgvps company
Stripe paypal method for lgvps
A BeinCart LLC Company
Copyright © 2022 BeinCart LLC | Designed by LGVPS Team