What command is better for fuzzy searching files in Linux?

What command is better for fuzzy searching files in Linux?

1. Introduction

This article mainly explains how to use command line tools to fuzzy search for files in Linux system. The tools in this article include the usage of find command and the usage of grep command. There is also a combination of find and grep, which can more accurately search for files according to conditions.


2. Fuzzy search for files based on file name

For example:

In the directory /var/zcwyou, find files with the suffix .txt. The file names are case sensitive. That is, only the .txt suffix can be matched

[root@zcwyou ~]# find /var/zcwyou -name '*.txt'

In the directory /var/zcwyou, find files with the suffix .txt and ignore the size. That is, it can match files with .txt suffix, .Txt suffix, or .TXT suffix.

[root@zcwyou ~]# find /var/zcwyou -iname '*.txt'

3. Fuzzy search for files based on file name and file content

Directions:

Combining find command with xargs and grep -i 'target matches keyword'

Case 1:

Search for files in the current directory whose contents contain a certain string (case sensitive):

grammar:

find /etc/ -type f | xargs grep 'target keyword'

Case 2:

In the /etc/ directory, search for files with the keyword zcwyou

[root@zcwyou ~]# find /etc/ -type f | xargs grep 'zcwyou'

You can find the following files and their corresponding contents:

/etc/group-:zcwyou:x:1000:

/etc/gshadow-:zcwyou:!!::

/etc/passwd-:zcwyou:x:1000:1000:zcwyou:/home/zcwyou:/bin/bash

/etc/group:zcwyou:x:1000:zcwyou

/etc/shadow-:zcwyou:

Isn’t it very efficient?

Case 3:

Search for specific files in the current directory whose contents contain a certain string (ignoring case):

Use the find command to search for all files ending with .c in the current directory and all files ending with .c in subdirectories, and pass the results as parameters to grep, which will perform the search and filtering.

[root@zcwyou ~]#find . -type f -name '*.c' | xargs grep -i 'Target search keyword'

4. Use the `find` command to fuzzy search for Linux files

In the current directory, fuzzy search for files accessed in the last 10 minutes in the system

[root@zcwyou ~]# find . -amin -10

In the current directory, fuzzy search for files accessed in the last 48 hours in the system

[root@zcwyou ~]# find . -atime -2

In the current directory, search for files or folders that are empty in the system

[root@zcwyou ~]# find . -empty

In the current directory, search for files belonging to group cat in the system

[root@zcwyou ~]# find . -group cat

Fuzzy search for files modified in the last 5 minutes in the system

[root@zcwyou ~]# find . -mmin -5

Find files modified in the last 24 hours on the system

[root@zcwyou ~]# find . -mtime -1

5. Fuzzy search for Linux files based on user attributes

Find files belonging to obsolete users in the system

[root@zcwyou ~]# find . -nouser

Find the files belonging to the user zcwyou in the system

[root@zcwyou ~]# find . -user zcwyou

6. Fuzzy search for files by file size

Find files larger than 1M in the root directory and print them

[root@zcwyou ~]# find / -size +1M -type f -print

Find files equal to 500 bytes in the current directory, including subdirectories, and print the results

[root@zcwyou ~]# find -size 500c -print

Find files smaller than 600k in the home directory

[root@zcwyou ~]# find /home -size -600k -print

7. Search for files by age

Find files newer than old.txt

[root@zcwyou ~]# find -newer "old.txt" -type f -print

Find files older than newer.txt

[root@zcwyou ~]# find ! -newer "newer.log" -type f -print

Find files newer than old.txt and older than newer.txt

[root@zcwyou ~]# find -newer 'old.txt' ! -newer 'newer.txt' -type f -print

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.

You may also be interested in:
  • How to fuzzily find a file in Linux

<<:  Vue implements two-way data binding

>>:  MySQL FAQ series: When to use temporary tables

Recommend

harborRestart operation after modifying the configuration file

I won't say much nonsense, let's just loo...

Detailed explanation of common methods of JavaScript String

Table of contents 1. charAt grammar parameter ind...

Vue simulates the shopping cart settlement function

This article example shares the specific code of ...

Complete example of Vue encapsulating the global toast component

Table of contents Preface 1. With vue-cli 1. Defi...

Detailed explanation of the use of Linux seq command

01. Command Overview The seq command is used to g...

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...

Vue implements top left and right sliding navigation

Navigation and other things are often used in dai...

JavaScript implements class lottery applet

This article shares the specific code of JavaScri...

How to use IDEA to configure tomcat and create JSP files

Before using idea to write JSP files, you need to...

LinkedIn revamps to simplify website browsing

Business social networking site LinkedIn recently...

JavaScript quickly implements calendar effects

This article example shares the specific code of ...