• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

find command

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Introduction to the find Command in Linux


The find command helps you search for files and directories across your filesystem based on name, type, size, modification time, permissions, and more.

Unlike grep, which searches inside files, find searches for files or directories themselves.

Here’s how to get comfortable with it.

Table of Contents

  • Basic Syntax of find
  • 1. Find by Exact File Name
  • 2. Case-Insensitive File Name Search
  • 3. Find Files by Type
  • 4. Find Files by Extension
  • 5. Find Empty Files or Directories
  • 6. Find Files by Size
  • 7. Find Files by Modification Time
  • 8. Find Files Accessed Recently
  • 9. Find Files with Specific Permissions
  • 10. Find Files by Owner
  • 11. Find and Copy Files Owned by a User
  • 12. Find and Delete Files Owned by a User
  • 13. Find and Delete Files
  • 14. Find and List with Full Path
  • 15. Combine Multiple Conditions
  • Conclusion
  • Let's Connect on LinkedIn
Basic Syntax of find


find [starting_point] [options] [expression]
  • starting_point: where the search begins (e.g., . for current directory or /home/user)
  • options / expression: what to look for (e.g., -name, -type, etc.)
1. Find by Exact File Name


find <directory> -name <filename>


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • <directory> means search from the current directory.
  • -name tells find to look for a file with the exact name "notes.txt".
  • If it finds the file, it will show you the full path.
2. Case-Insensitive File Name Search


find <directory> -iname <filename>


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • -iname is just like -name but ignores case. So it will match CASEi.txt, Casei.TXT, etc.
3. Find Files by Type


In the find command, -type is used to specify the type of file you're looking for.

Common values for -type:

LetterMeaning
fRegular file (e.g., .txt, .log, .sh)
dDirectory
lSymbolic link
cCharacter device file (like /dev/tty)
bBlock device file (like /dev/sda)
Examples:


To find files only:


find <directory> -type f


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


To find directories only:


find . -type d

This is useful if you're searching inside complex folders.

4. Find Files by Extension


find <directory> -name "*.log"


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


This finds all files ending in .log.

You can use -iname "*.log" to do it case-insensitively.

5. Find Empty Files or Directories


find <directory> -empty


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Returns empty files or folders
  • Useful for cleanup.
6. Find Files by Size


Find files smaller than 100 MB:


find <directory> -size -500k


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Find files larger than 10 MB:


find <directory> -size +10M

To find files between 50MB and 100MB:


find <directory> -size +50M -size -100M

Size units:

  • c = bytes
  • k = kilobytes
  • M = megabytes
  • G = gigabytes

Examples:

  • -size -500k = smaller than 500 KB
  • -size +1G = larger than 1 GB
7. Find Files by Modification Time


find <directory> -mtime -1


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Finds files modified in the last 1 day.

-1 means less than 1 day ago.

Use +7 for files older than 7 days.

8. Find Files Accessed Recently


find <directory> -atime -2


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • -atime = accessed time.
  • Finds files accessed within the last 2 days.
9. Find Files with Specific Permissions


find <directory> -perm 644


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Finds files with exact permission 755 (owner can read/write & execute, group and others can read & write).

Other modes:

  • -perm -mode = must have all permission bits
  • -perm /mode = match any of the bits
10. Find Files by Owner


find <directory> -user <username>


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Finds files owned by a specific user.

Example:


find /var/log -user john

Useful for checking user activity or auditing.

11. Find and Copy Files Owned by a User


find <directory> -user <username> -exec cp {} <location> \;
  • Finds files owned by a user and copies each one to a backup folder.
  • Replace secteam with your desired backup path.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



12. Find and Delete Files Owned by a User


find <directory> -user <username> -exec rm {} \;
  • Searches for files owned by the user and deletes them. Be cautious, test first using ls instead of rm.
13. Find and Delete Files


find <directory> -name "filename" -exec rm {} \;


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • -exec lets you run a command on each found file.
  • {} is replaced with the filename.
  • \; ends the -exec command.

Test safely:


find <directory> -name "filename" -exec ls {} \;
14. Find and List with Full Path


find "$(pwd)" -name "*.sh"
  • $(pwd) gives full path of current directory.
  • Useful to know exact locations of found files.
15. Combine Multiple Conditions


find . -type f -name "*.log" -size +1M
  • Finds all .log files that are regular files and larger than 1 MB.
Conclusion


From everyday maintenance to system-wide audits, the find command proves its worth in countless scenarios.

Knowing how to use it efficiently can save you time and prevent costly mistakes.

Start with simple queries, build your confidence, and you'll soon be a find command power user.


Let’s connect on LinkedIn


(

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

)

As I automate my journey into RHCE and Ansible, I’d love to connect with fellow learners and professionals. Feel free to reach out and join me as I share tips, resources, and insights throughout this 30-day challenge.

cloudwhistler #30daysLinuxchallenge


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу