Detailed usage of Linux text search command find

Detailed usage of Linux text search command find

The find command is mainly used to find directories and files, and you can specify multiple parameters for matching.

Usage: find +查找路徑 +命令參數 [輸出形式]

Search path: Tell find where to search

Command parameters: specify the file attributes to be searched, including various attributes, such as type, name, size, modification time, etc.

Commonly used parameters:

  • -name Search by file name
  • -user Search by file owner
  • -mtime searches by file modification time, -n means the file modification time is within n days from today, +n means the file modification time is more than n days from today
  • -type Search by file type, d means directory, f means file, l means symbolic link file
  • -size Search by file size (need to add units, K, M, G), +n M means to search for files larger than n M
  • -maxdepth specifies the maximum depth when searching for files (the default is to search in all subdirectories under the specified path. After specifying the depth, you can now search only in the first-level directory or the second-level directory)

Output format: The output format is optional. The output formats include -print to print the found content (the default is to print the found content, so it is generally not used), and -exec to further process the found content (more commonly used). The format is:

-exec Linux command {} \; {} represents the found content

Examples:

find . -maxdepth 1 -name "1.txt" # Directly specify the file name to search, -maxdepth 1 means the maximum search level is the first-level directory 

find . -maxdepth 1 -name "*.txt" # Search for files ending with .txt in the current directory 

find . -maxdepth 1 -name "[0-9].txt" # Find txt files named with numbers 

find . -maxdepth 1 -name "[0-9]*.txt" # Find files starting with a number 

find . -maxdepth 1 -mtime -5 # Find files in the current directory that have been modified within 5 daysfind . -maxdepth 1 -mtime +5 # Find files in the current directory that have been modified more than 5 daysfind . -maxdepth 1 -type d # Find files of type directory in the current directory, with a maximum search level of one directory (hidden directories will be found) 

find . -maxdepth 1 -size +10M # Find files larger than 10M in the current directory find . -maxdepth 1 -size -1M # Find files smaller than 1M in the current directory 

find . -maxdepth 1 -name "[az].txt" -exec rm -rf {} \; # Find txt files named with one letter and delete them find . -maxdepth 1 -name "*.txt" | xargs grep "error" # Print the content of the line with error in the file ending with txt find . -maxdepth 1 -name "*.txt" | xargs rm # Delete files ending with txt in the current directory

xargs passes the execution results of the command before the pipeline as parameters one by one to the command after the pipeline

ls [az].txt | xargs -n1 -i{} mv {} {}.bak # Add a .bak suffix to the txt files named with a single letter in the current directory. -n1 means to process objects one by one. -i{} replaces the previous object with {}. mv {} {}.bak is equivalent to mv a.txt a.txt.bak 

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Introduction to the use and disabling of transparent huge pages in Linux
  • Detailed explanation of upgrading Python and installing pip under Linux
  • How to use for loop combined with if to find files in Linux Shell directory
  • How to get the real path of the current script in Linux
  • Linux uses lsof command to check file opening status
  • Linux virtual memory settings tutorial and practice
  • Detailed explanation of using grep command in Linux
  • Detailed explanation of Linux text processing command sort
  • One question to understand multiple parameters of sort command in Linux
  • How to delete garbled or special character files in Linux

<<:  How to draw a cool radar chart in CocosCreator

>>:  Detailed steps to install Mysql5.7.19 using yum on Centos7

Recommend

Detailed explanation of Promises in JavaScript

Table of contents Basic usage of Promise: 1. Crea...

How to view the database installation path in MySQL

We can view the installation path of mysql throug...

A brief discussion on the principle of React two-way data binding

Table of contents What is two-way data binding Im...

CSS3 realizes the effect of triangle continuous enlargement

1. CSS3 triangle continues to zoom in special eff...

Podman boots up the container automatically and compares it with Docker

Table of contents 1. Introduction to podman 2. Ad...

Elements of user experience or elements of web design

System and user environment design <br />Th...

Simply understand the writing and execution order of MySQL statements

There is a big difference between the writing ord...

How to view files in Docker image

How to view files in a docker image 1. If it is a...

Nginx configuration PC site mobile site separation to achieve redirection

Use nginx to configure the separation of PC site ...

Steps for docker container exit error code

Sometimes some docker containers exit after a per...