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

补充下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

  • What is the Find Command in Linux?
  • Syntax of Find Command in Linux :
  • Options Available in Find Command in Linux
  • How to Find a File in Linux from the Command Line
  • Examples of Find Command in Linux

目录

  • 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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/478697.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

根据图片识别车牌号

群里有人发的毕设要求&#xff0c;本身不是专业的后端&#xff0c;也就没敢接&#xff0c;但对车牌识别挺感兴趣的&#xff0c;于是自己就做了下 看了网上别人的做法&#xff0c;基本都是调用大厂提供的API。这里我就选择百度的了&#xff0c;因为有一月1000次的免费额度 首先打…

C++:类的6大默认成员函数(拷贝构造函数篇)

文章目录 1、拷贝构造函数的概念const用途 2、拷贝构造函数的特性浅拷贝/值拷贝 前言:Hello,大家好&#xff0c;咱这篇博客继续默认成员函数&#xff0c;今天的笔记分享为拷贝构造函数~ 1、拷贝构造函数的概念 在创建对象时&#xff0c;我们能否创建一个与已存在对象一某一样的…

C/C++代码性能优化——数据结构和算法

1. 数据结构 合适的数据结构&#xff0c;对代码的性能提升非常明显。针对数据结构&#xff0c;我们不需要可以做到白板手写的程度。只要熟知其特点&#xff0c;然后推导出其应用场景&#xff0c;等到了真正需要时&#xff0c;再查找示例代码来修改应用即可。 1.1. 数组 固定…

Nginx离线安装(保姆级教程)

1、下载与安装gcc-c环境 获取rpm包的方式很多&#xff0c;在这里推荐使用yum工具获取&#xff0c;因为手动从官网下载&#xff0c;手动执行rpm -Uvh *.rpm --nodeps --force命令进行安装&#xff0c;可能会缺少某个依赖&#xff0c;我们也不确定到底需要哪些依赖。 因此需要准…

Java毕业设计 基于springboot医院挂号系统 医院管理系统

Java毕业设计 基于springboot医院挂号系统 医院管理系统 springboot医院挂号系统 医院管理系统 功能介绍 用户&#xff1a;登录 首页 个人资料 修改密码 门诊管理 用户挂号 医生&#xff1a;登录 首页 个人资料 修改密码 门诊管理: 用户挂号 处方划价 项目划价 项目缴费 项目…

【机器学习300问】43、回归模型预测效果明明很好,为什么均方根误差很大?

一、案例描述 假设我们正在构建一个房地产价格预测模型&#xff0c;目标是预测某个城市各类住宅的售价。模型基于大量房屋的各种特征&#xff08;如面积、地段、房龄、楼层等&#xff09;进行训练。 回归模型在大部分情况下对于住宅价格预测非常精准&#xff0c;用户反…

【教程】深入探究 JS代码混淆与加密技术

&#x1f512; 引言 在网络世界中&#xff0c;保护代码安全是至关重要的一环。JS代码混淆与加密技术则成为了开发者们常用的手段之一。本文将深入探讨混淆和加密的概念&#xff0c;以及其实现原理和应用方法&#xff0c;帮助读者更好地了解并运用这些技术。 ✨ 概念介绍 &quo…

调用百度通用翻译API进行中文翻译(附python代码)

文章目录 1. 百度API2. API接口3. 大规模使用4. AcknowledgmentReference彩蛋&#xff1a;百度大脑 AI开放平台 1. 百度API 在百度翻译开放平台&#xff08;http://api.fanyi.baidu.com/api/trans/product/desktop&#xff09;注册账号&#xff0c;可以免费使用基本版翻译功能…

C语言复杂度(个人笔记)

时间复杂度主要衡量一个算法的运行快慢. 空间复杂度主要衡量一个算法运行所需要的额外空间. 时间复杂度 算法中的基本操作的执行次数&#xff0c;为算法的时间复杂度. 只需要大概执行次数&#xff0c;我们使用大O的渐进表示法。(看谁对数学表达式的影响最大) 空间复杂度 是…

论文笔记:Contrastive Multi-Modal Knowledge GraphRepresentation Learning

论文来源&#xff1a;IEEE Transactions on Knowledge and Data Engineering 2023 论文地址&#xff1a;Contrastive Multi-Modal Knowledge Graph Representation Learning | IEEE Journals & Magazine | IEEE Xplorehttps://ieeexplore.ieee.org/abstract/document/9942…

可变形卷积颠覆式创新!新SOTA提速80%,更高性能,更强几何适应能力

在传统的卷积神经网络中&#xff0c;固定模式的卷积核在处理图像时可能会限制网络对不规则形状特征的提取能力。为了解决这个问题&#xff0c;研究者提出了可变形卷积。 可变形卷积是一种改进的卷积操作&#xff0c;它通过引入可学习的偏移量来增强模型对几何变化的适应能力&a…

qt+ffmpeg 实现音视频播放(三)之视频播放

一、视频播放流程 &#xff08;PS&#xff1a;视频的播放流程跟音频的及其相似&#xff01;&#xff01;&#xff09; 1、打开视频文件 通过 avformat_open_input() 打开媒体文件并分配和初始化 AVFormatContext 结构体。 函数原型如下&#xff1a; int avformat_open_inpu…

python 教你如何创建一个自定义库 colorlib.py

目录 Colorlib 生成代码 模块代码 导入测试 测试一 测试二 应用测试 颜色列表 colorList 随机颜色元组 randcolorTuples 随机颜色字串 randcolorStrings Color类测试 测试一 测试二 题外话 Colorlib 有没有碰到过这样的场景&#xff1a;写代码时想要用上丰富的色…

C#混淆心得

C#混淆心得 近期遇到混淆C#代码的需求&#xff0c;在网上找了很多办法&#xff0c;在此记录一下。 混淆的本质就是让代码变丑&#xff0c;让别人看不懂。 为什么要混淆&#xff1a; 1.保护核心代码 可以在一定程度上避免别人偷代码&#xff0c;从而保护重要的部分&#xf…

3.3 RK3399项目开发实录-板载Ubuntu系统的使用(wulianjishu666)

嵌入式物联网常用90款传感器开发例程。链接&#xff1a;https://pan.baidu.com/s/1oisHMZXDzKqa4EspY83V-A?pwdo5f4 1. 介绍 Ubuntu 使用手册是针对 Firefly 官方发布的 Ubuntu 系统固件特性所编写&#xff0c;适用于 Ubuntu Desktop 与 Minimal 系统&#xff0c;部分与 UI 显…

适用于智能语音小家电的语音ic类型有哪些?

适用于智能语音小家电的语音ic类型有哪些&#xff1f; 1. 语音播放芯片&#xff1a;这种芯片主要用于实现语音提示和报警功能。例如&#xff0c;当按下某个按钮时&#xff0c;它可以发出语音提醒&#xff0c;或者在出现故障时发出报警声音。这种芯片的应用非常广泛&#xff0…

Halcon 条码读取

一维码读取 create_bar_code_model 创建条码读取器的模板 set_bar_code_param 配置解码方式 find_bar_code 读取条码 clear_bar_code_model 清除条码匹配模板 * 1.创建条码读取器的模板 * 参数一&#xff1a;通用参数的名称&#xff0c;针对条形码模型进行调整。默认值为空 * 参…

Java实用经验总结

前言&#xff1a;以下为笔者在工作中总结的好用且简洁代码的经验 文章目录 1、多判断代替if2、通配符替换内容&#xff08;常见于邮件、短信等模版&#xff09;3、spring获取bean对象4、动态获取nacos配置5、优雅校验请求入参 1、多判断代替if 针对多个是和否的问题&#xff0…

如何安装和卸载SFP光模块

SFP光模块的安装和拆卸是简单直接的过程。然而&#xff0c;任何非标准操作都可能导致隐式损坏甚至永久故障。您需要参考及时更新的光模块的数据表或用户手册&#xff0c;以熟悉其特性和锁定机制。 准备工作 常见事项 拆卸和插入SFP光模块可能会缩短其使用寿命&#xff0c;因…

搜索二维矩阵

题目链接 搜索二维矩阵 题目描述 注意点 每行中的整数从左到右按非严格递增顺序排列每行的第一个整数大于前一行的最后一个整数1 < matrix.length, matrix[0].length < 100 解答思路 先二分查找找到target所处的行&#xff0c;找到行后再二分查找找到target所处的列…