sy4文件、目录操作命令-补充find

本文详细介绍了Linux中的find命令,包括其功能、语法、可用选项,以及如何从命令行查找文件。通过实例展示,学习者可以掌握如何根据名称、大小、时间等条件精确搜索和管理文件,提升Linux文件操作效率。
摘要由CSDN通过智能技术生成

补充下find的命令实例把,我搜了下发现这篇文章的笔记符合课程的实例:

参考< How to Find a File in Linux | Find Command - GeeksforGeeks>

这里做了实验,给大家参考:

Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the “find” command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This article explores the prowess of the find command, shedding light on its capabilities and how it serves as a go-to tool for Linux users aiming to locate files swiftly and effectively.

Linux 以其强大的命令行界面而闻名,它提供了一套强大的工具,用于高效的文件和目录管理。其中,“查找”命令是不可或缺的资产,在根据不同标准搜索文件时提供了无与伦比的多功能性。本文探讨了 find 命令的强大功能,阐明了它的功能,以及它如何作为旨在快速有效地定位文件的 Linux 用户的首选工具。

Table of Content

目录

  • Linux 中的 Find 命令是什么?
  • Linux 中 Find 命令的语法:
  • Linux 中 Find 命令中可用的选项
  • 如何从命令行在 Linux 中查找文件
  • Linux 中查找命令的示例

What is the Find Command in Linux?

The find command in Linux is a dynamic utility designed for comprehensive file and directory searches within a hierarchical structure. Its adaptability allows users to search by name, size, modification time, or content, providing a flexible and potent solution. As a pivotal component of the Linux command-line toolkit, the find command caters to the nuanced needs of users, ensuring precision in file exploration and retrieval. Discover the diverse functionalities of the find command and enhance your file management efficiency on the Linux platform.

Linux 中的查找命令是什么? Linux 中的 find 命令是一个动态实用程序,专为在分层结构中进行全面的文件和目录搜索而设计。它的适应性允许用户按名称、大小、修改时间或内容进行搜索,提供灵活而有效的解决方案。作为 Linux 命令行工具包的关键组件,find 命令可以满足用户的细微需求,确保文件探索和检索的精度。了解 find 命令的各种功能,并在 Linux 平台上提高文件管理效率。

Syntax of Find Command in Linux :

linux命令语法:

Here is the syntax for the find command in Linux:

find [path] [options] [expression]

Here,

  • path: Starting directory for the search.
    • Example: find /path/to/search
  • options: Additional settings or conditions for the search.
    • Example: find /path/to/search -type f -name "*.txt"
  • expression: Criteria for filtering and locating files.
    • Example: find /path/to/search -type d -name "docs"

This syntax allows you to customize your file search by specifying the path, adding options, and defining search criteria using expressions.

Options Available in Find Command in Linux

Here are the `find` command options along with brief descriptions of their purposes.

Command

Description

-name pattern

Searches for files with a specific name or pattern.

-type type

Specifies the type of file to search for (e.g., f for regular files, d for directories).

-size [+/-]n

Searches for files based on size. `+n` finds larger files, `-n` finds smaller files. ‘n‘ measures size in characters.

-mtime n

Finds files based on modification time. `n` represents the number of days ago.

-exec command {} \;

Executes a command on each file found.

-print

Displays the path names of files that match the specified criteria.

-maxdepth levels

Restricts the search to a specified directory depth.

-mindepth levels

Specifies the minimum directory depth for the search.

-empty

Finds empty files and directories.

-delete

Deletes files that match the specified criteria.

-execdir command {} \;

Executes a command on each file found, from the directory containing the matched file.

-iname pattern

Case-insensitive version of `-name`. Searches for files with a specific name or pattern, regardless of case.

How to Find a File in Linux from the Command Line

使用find命令查找文件

Using the find command is straightforward. To find a file in Linux, open a terminal and use the followingbasic syntax:基本语法:

find /path/to/search -options criteria

Replace “/path/to/search" with the directory where you want to start the search and customize the options and criteria based on your requirements.

For example :

To find a file named “example.txt” in the home directory, you would use:

find ~ -name "example.txt"

This command will locate and display the path to the file if it exists in the specified directory or its subdirectories.

Examples of Find Command in Linux 命令find的实例

1. How to Find A Specific File Using `find` Command in Linux

怎么查找指定的文件

This query is designed to pinpoint a file within a designated directory. In the provided example, it seeks a file named “sample.txt” within the “linux” directory.

find ~ -name "hiboy.txt"

Find ../linux/  -name helloworld.txt

这里用linux用户登录自己的目录下,查找hiboy.txt文件;查找helloworld.txt文件。

截图显示:

2. How to Search Files with a Pattern Using `find` Command in Linux

通过通配符查找文件。

This command is tailored for discovering files within a directory that adhere to a specific naming pattern. In this case, it identifies files ending with ‘.txt’ within the “linux” directory.

find ./linux -name *.txt

The command looks for files with names ending in ‘.txt’ within the “linux” directory, presenting a list of matching files.

以下查找后缀为txt的文件。

Output:

Search a file with pattern

3. How to Find and Confirm File Deletion Using `find` Command in Linux

This command not only locates a specified file but also prompts the user for confirmation before initiating its removal. The example seeks to delete a file named “sample.txt” within the “linux” directory.

find ./linux -name sample.txt -exec rm -i {} \;

The -exec option executes the rm command on the located file, and the -i flag prompts the user for confirmation before deletion. When this command is entered, a prompt will come for confirmation, if you want to delete sample.txt or not. if you enter ‘Y/y’ it will delete the file.

此命令不仅会查找指定的文件,还会在启动删除文件之前提示用户进行确认。该示例试图删除“linux”目录中名为“sample.txt”的文件。 find ./linux -name sample.txt -exec rm -i {} \;  -exec 选项对找到的文件执行 rm 命令,-i 标志在删除之前提示用户进行确认。输入此命令时,将提示确认是否要删除sample.txt。 如果输入“Y/y”,它将删除该文件。 

我的实例删除hiboy.txt文件。

find and delete a file with confirmation

4. Search for Empty Files and Directories Using `find` Command in Linux

查找空文件

This query is tailored for discovering and listing empty files and directories within a specified directory.

find ./linux -empty

 The `find` command identifies and lists all empty folders and files within the “linux” directory or its subdirectories.

Output:

查看空文件,然后cat验证下是否为空

Search for empty files and directories

5. Search Files with Specific Permissions Using `find` Command in Linux

查找对应权限的文件

This command is used to locate files within a directory that have specific permissions. In the provided example, it identifies files with permissions set to 664 within the “linux” directory.

find ./linux -perm 664

The command searches for files within the “linux” directory with the specified permissions (664) and displays the results.

Output:

Search for file with entered permissions

6. Display Repository Hierarchy Using `find` Command in Linux

查找对应的文件类型

This command is utilized to display the hierarchical structure of repositories and sub-repositories within a given directory.

find . -type d

This command displays all the repositories and sub-repositories present in the current repository. In the below example, we are currently in a repository namely “GeeksforGeeks” which contains a repo “Linux”, which contains sub-repo “LinuxCmds” which further contains a repo “FindCmd”. The ouput of below cmd is simply displaying this info. Please note that in this case if you will use “ls” cmd then it will only show “/Linux”.

Output:

7. Search Text Within Multiple Files Using `find` Command in Linux

This command is tailored for finding lines containing specific text within multiple files. The example looks for lines containing the word ‘Geek’ within all ‘.txt’ files in the current directory and its subdirectories.

find ./ -type f -name "*.txt" -exec grep 'hi'  {} \;

The command searches for ‘.txt’ files (-type f and -name "*.txt") and uses grep to print lines containing the specified text (‘Geek’).

7. 在 Linux 中使用“find”命令搜索多个文件中的文本 此命令专为查找多个文件中包含特定文本的行而量身定制。该示例在当前目录及其子目录的所有“.txt”文件中查找包含单词“hi”的行。 find ./ -type f -name “*.txt” -exec grep 'hi' {} \; 该命令搜索“.txt”文件(-type f 和 -name “*.txt”),并使用 grep 打印包含指定文本的行 ('hi')。

Output:

Search text within multiple files

8. Find Files by When They Were Modified Using `find` Command in Linux

The -mtime option is handy for finding files based on their modification time. To find files modified within the last 7 days, you can use:

find /path/to/search -mtime -7

This command will list files modified in the last week.

Finding Last modifications

In this example we are searching changes in directory “/home/linux/” which are done is past 7 days.

9. Use Grep to Find Files Based on Content Using `find` Command in Linux

find命令使用 grep 根据内容查找文件

Combining the find command with grep allows you to search for files based on their content. For example, to find files containing the word “pattern” in the current directory and its subdirectories, you can use:

find . -type f -exec grep -l "pattern" {} \;

This command will display the names of files containing the specified content.

在 Linux 中使用“find”命令 将 find 命令与 grep 结合使用,您可以根据文件的内容搜索文件。例如,要在当前目录及其子目录中查找包含单词“pattern”的文件,可以使用:find 。-type f -exec grep -l “模式” {} \; 此命令将显示包含指定内容的文件的名称。

Breakdown of the Command:

  • find .: Initiates the search from the current directory (.).
  • -type f: Specifies that the search is for files only, excluding directories.
  • -exec grep -l "pattern" {} \;: Executes the grep command on each found file ({}) to search for the specified content (“pattern”). The -l option in grep ensures that only the names of files containing the pattern are displayed.

Command Execution:

  1. The find command starts the search from the current directory, including all its subdirectories.
  2. For each file (-type f) found in the search, the -exec option executes the grep command.
  3. The grep command searches for the specified content (“pattern”) in each file.
  4. If a file contains the specified content, its name is displayed due to the -l option in grep.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个在高校打杂的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值