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

news2025/1/12 11:55:58

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【GIT】最好用的git可视化教程网站推荐

最好用可视化学习git 网站:https://learngitbranching.js.org/?demo&localezh_CN 玩遍所有关卡&#xff0c;花半天时间便能掌握git &#x1f603; 本地仓库 基础命令介绍 git commit 提交 git branch <分支名> 创建分支 git checkout <分支名> 切换分支 git…

定制红酒:品质与口感,双重保障

在葡萄酒的世界里&#xff0c;云仓酒庄的洒派定制红酒以其卓着的品质和迷人的口感&#xff0c;成为了无数品鉴者的心头好。这款红酒&#xff0c;不仅是对品质的追求&#xff0c;更是对生活的热爱和品味的体现。 云仓酒庄深知品质是红酒的灵魂&#xff0c;因此对洒派定制红酒的品…

栈和队列的学习

存储方式分两类&#xff1a;顺序存储和链式存储 栈&#xff1a;只允许从一端进行数据插入和删除的线性表&#xff1a;先进后出 FILO 队列&#xff1a;只允许从一端进行数据插入&#xff0c;另一端进行数据删除的线性表&#xff1a;先进先出 FIFO 栈 创建空栈&#xff0c;创建…

wmv转换成mp4能无损吗?这样设置~

WMV和MP4是两种不同的视频格式&#xff0c;它们使用不同的编解码算法和容器格式。在将WMV转换为MP4时&#xff0c;通常会发生一定程度的重新编码&#xff0c;因此不能完全保证无损转换。无损转换意味着输出的MP4文件与输入的WMV文件在视听上没有任何质量损失&#xff0c;这在实…

基于springboot的反诈宣传平台

技术&#xff1a;springbootmysqlvue 一、系统背景 反欺诈平台可以对公交信息进行集中管理&#xff0c;可以真正避免传统管理的缺陷。反欺诈平台是一款运用软件开发技术设计实现的应用系统&#xff0c;在信息处理上可以达到快速的目的&#xff0c;不管是针对数据添加&#xff…

spring cloud项目微服务间互相调用使用自定义标注进行鉴权方案

来吧&#xff0c;贴代码。 一、背景 我们有一个项目使用了spring cloud&#xff0c;有的微服务需要调用别的微服务&#xff0c;但这些调用没有鉴权&#xff1b;当初项目时间非常紧&#xff0c;同时这部分微服务有的对外也没有鉴权&#xff0c;在代码中设置了无须鉴权&#xf…

真机笔记(2)项目分析

目录 1. 项目&#xff1a; 2. 网络工程师工作流程 3. 实验 设备命名 登录密码 使用SSH协议 1. 项目&#xff1a; 竞标方&#xff1a;集成商、厂商、代理商、服务商、监理检测公司 在一个网络项目中&#xff0c;不同的角色承担着不同的职责和任务。以下是集成商、厂商、代…

程序人生——Java异常使用建议

目录 引出异常建议110&#xff1a;提倡异常封装&#xff1b;建议111&#xff1a;采用异常链传递异常 建议112&#xff1a;受检异常尽可能转化为非受检异常建议113&#xff1a;不要在finally块中处理返回值 建议114&#xff1a;不要在构造函数中抛异常建议115&#xff1a;使用Th…

VMD + CEEMDAN 二次分解,CNN-Transformer预测模型

往期精彩内容&#xff1a; 时序预测&#xff1a;LSTM、ARIMA、Holt-Winters、SARIMA模型的分析与比较-CSDN博客 风速预测&#xff08;一&#xff09;数据集介绍和预处理-CSDN博客 风速预测&#xff08;二&#xff09;基于Pytorch的EMD-LSTM模型-CSDN博客 风速预测&#xff…

Data.olllo:一键数据“分组统计”!

引言&#xff1a; 数据统计是数据分析中的重要环节&#xff0c;而如何快速、准确地进行数据分组统计是许多数据工作者关注的焦点。现在&#xff0c;借助Data.olllo的神奇功能&#xff0c;您可以轻松进行一键式的数据分组统计&#xff0c;为您的数据分析提供更强大的支持&…

什么是浏览器指纹识别?指纹浏览器有用吗?

浏览器指纹识别是好是坏&#xff1f;这现在确实是一个有争议的话题。83%的消费者经常或偶尔会根据浏览历史记录看到广告。其实这就是利用了浏览器指纹技术。 如果您想了解浏览器指纹识别是什么&#xff0c;那就看下去&#xff01; 一、什么是浏览器指纹识别 浏览器指纹是指无…

Quartz完全开发手册(一篇学会Quartz所有知识点)

目录 一、Quartz概念 1.1、Quartz介绍 1.2、使用场景 1.3、特点 二、Quartz运行环境 三、Quartz设计模式 四、Quartz学习的核心概念 4.1、任务Job 4.2、触发器Trigger 4.3、调度器Scheduler 五、Quartz的体系结构与工作流程 5.1、体系结构 5.2、工作流程 六、Quar…

【Mock|JS】Mock的get传参+获取参数信息

mockjs的get传参 前端请求 const { data } await axios("/video/childcomments", {params: {sort: 1,start: 2,count: 5,childCount: 6,commenIndex: 0,},});后端获取参数 使用正则匹配url /*** # 根据url获取query参数* param {Url} urlStr get请求获取参数 eg:…

鸿蒙Harmony应用开发—ArkTS-全局UI方法(时间滑动选择器弹窗)

以24小时的时间区间创建时间滑动选择器&#xff0c;展示在弹窗上。 说明&#xff1a; 该组件从API Version 8开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 本模块功能依赖UI的执行上下文&#xff0c;不可在UI上下文不明确的地方使用&…

vuecli创建vue3项目

第一步&#xff1a; 在文件夹中输入 vue create xxx 第二步&#xff1a; 勾选下面带有*号的&#xff0c;经验最好把Linter/Formatter勾掉&#xff0c;不然会出现eslint报错 第三步&#xff1a; 选择3.x 第四步&#xff1a; 意思为是否用history模式来创建路由&#xff0…

把 Taro 项目作为一个完整分包,Taro项目里分包的样式丢失

现象&#xff1a; 当我们把 Taro 项目作为原生微信小程序一个完整分包时&#xff0c;Taro项目里分包的样式丢失&#xff0c;示意图如下&#xff1a; 原因&#xff1a; 在node_modules/tarojs/plugin-indie/dist/index.js文件里&#xff0c;限制了只有pages目录下会被引入app.w…

The 2023 Guangdong Provincial Collegiate Programming Contest

I. Path Planning 嗯&#xff0c;怎么说呢&#xff0c;一般二维图&#xff0c;数据不是很大的比如n*m*log级别允许的&#xff0c;如果一眼不是bfs&#xff0c;可以考虑结合一下二分 本题可知&#xff0c;只能向下或者向右&#xff0c;那么我们就像如果答案为x&#xff0c;那么…

【重温设计模式】访问者模式及其Java示例

访问者模式的基本概念 访问者模式&#xff0c;一种行为型设计模式&#xff0c;其基本定义是&#xff1a;允许一个或者多个操作应用到一组对象上&#xff0c;解耦操作和对象的具体类&#xff0c;使得操作的添加可以独立于对象的类结构变化。在面向对象编程中&#xff0c;访问者…

sqllab第35-45关通关笔记

35关知识点&#xff1a; 宽字节注入数值型注入错误注入 payload:id1andextractvalue(1,concat(0x7e,database(),0x7e))0--联合注入 payload:id0unionselect1,database(),version()-- 36关知识点&#xff1a; 字符型注入宽字节注入错误注入 payload:id1%df%27andextractvalue(…

什么是浏览器指纹识别?Maskfog指纹浏览器有用吗?

浏览器指纹识别是好是坏&#xff1f;这现在确实是一个有争议的话题。83%的消费者经常或偶尔会根据浏览历史记录看到广告。其实这就是利用了浏览器指纹技术。 如果您想了解浏览器指纹识别是什么&#xff0c;那就看下去&#xff01; 一、什么是浏览器指纹识别 浏览器指纹是指无…