linux Find File Techniques

Posted by twinzen on Mon, 10 Jan 2022 18:17:38 +0100

Tools find, locate, grep, whereis/which, type.

find File Finder

find supports a variety of parameters. Here are some of the commonly used parameters. For more information, see: find command

Common Conditions
-type Type parameters 
	f Normal File
	l Symbolic Connection
	d Catalog
	c Character Device
	b Block Device
	s socket
	p Fifo
-iname ignore case
-path  Match file path or file
-regex Match file paths based on regular expressions (-iregex Ignore case)
! Negative Parameters, Negative Conditions
-maxdept Maximum search depth
-mindepth Minimum Lookup Depth

Search based on file timestamp
 Access time (-atime/Days,-amin/Minutes: The last time the user visited.
Modification time (-mtime/Days,-mmin/Minutes: The last time the file was modified.
Change time (-ctime/Days,-cmin/Minutes: The last time a file data element (such as permissions, etc.) was modified.
	- Search for the nearest (<7, ==7, >7)All files visited in days 
	find . -type f -atime -7	# Within seven days
	find . -type f -atime 7	# Seven days
	find . -type f -atime +7	# More than 7 days
-newer Compare with the specified file.
	find . -type f -newer file.lo # Find out the file.log all files that take longer to modify
-size Match based on file size
	b - Block (512 bytes)
	c - byte
	w - Word (2 bytes)
	k - Kilobytes
	M - Megabytes
	G - Gigabytes
	- search(Greater than, equal to, less than)10KB Files
	find . -type f -size +10k
	find . -type f -size 10k
	find . -type f -size -10k

File Operation
-delete Delete matching files
	find . -type f -name "*.txt" -delete #Delete all under the current directory. txt file

Refer to for more tips:https://wangchujiang.com/linux-command/c/find.html
Example: Find the set function header file set in C++. H
find /usr/include/ "set.h" | grep "set.h$"
#    Start path keyword filtering ends with "set.h"

Example: Recursively delete files with the specified suffix

directory structure

Remove all suffixes recursively as. c file
Alternatively, we can choose to recursively delete all suffixes as. c or. cpp file

Loce File Finder

The locate command maintains a database and we should update it before using it or we won't be able to query the latest files.

parameter

-b, --basename  # Match only the base name of the path name
-c, --count     # Output only the quantity found
-d, --database DBPATH # Use the database specified by DBPATH instead of the default database/var/lib/mlocate/mlocate.db
-e, --existing  # Print only the entries of the current existing file
-1 # If it is 1.Start safe mode. In secure mode, users will not see files that permissions cannot see. This will start slowing down because locate must go to the actual file system to get permission data for the file.
-0, --null            # Single entry with NUL on output
-S, --statistics      # Print statistics about each database without searching for entries
-q                    # Quiet mode, no error message will be displayed.
-P, --nofollow, -H    # Do not follow the trailing symbolic link when checking for file existence
-l, --limit, -n LIMIT # Limit output (or count) to LIMIT entries
-n                    # Display up to n outputs.
-m, --mmap            # Ignored for backward compatibility
-r, --regexp REGEXP   # Use basic regular expressions
    --regex           # Use extended regular expressions
-q, --quiet           # Quiet mode, no error message will be displayed
-s, --stdio           # Ignored for backward compatibility
-o                    # Specify the name of the inventory.
-h, --help            # Show help
-i, --ignore-case     # ignore case
-V, --version         # display version information
Example: Find the c library function header file stdio.h file

Example: Specify directory query

Although the locate command does not directly specify the parameters of the directory, it supports fuzzy queries, such as omitting several paths using *.

Query anaconda's log file:

Current directory query: Note that when specifying a current directory query, try to give an absolute path, such as. /* Such relative paths do not work (but relative paths to home directories can be used).

Query the file at the beginning of TCP under the current path. You need to use pwd to print out the path of the working directory first, then stitch out the keywords. Note that the search criteria here are *\tcp and *tcp*.

Also, if we use the -i parameter to ignore case, we can get matches with multiple.

grep text search tool

grep is commonly used for filtering commands to connect pipelines, but pipelines can also act as tools for querying files.

Basic parameters

-i	Ignore case when searching
-c	Output only the number of matching rows
-l	List only matching filenames, not specific matching rows
-n	List all matching rows, display line numbers
-h	Do not display file name when querying multiple files
-s	Do not display error messages that do not exist and do not match text
-v	Show all lines that do not contain matching text
-w	Match whole word
-x	Match entire line
-r	Recursively searching
-q	Prohibit output of any results, exit status indicates success of search
-b	Print offset in bytes from the header of a matching line spacing file
-o	and-b In combination, print the offset of the header of the matching data file in bytes

The grep tool is different from other file-finding tools in that it is used to find the contents of text files, or it is called the text-search tool.

For example, if we want to see the contents of a file, the general solution is to find the location of the file, open the file to view it, and grep simplifies this step.

Example: Find a file based on its contents

Existing web pages with the text style "Merry Christmas to all".

There are many files in the root directory of the website. Now we want to find all the pages that contain "Merry Christmas to everyone".

This can be done using the grep command. Grep-r "Merry Christmas to everyone". /

This method is very useful for finding some configurations and log files.

Example: View a configuration file

For example, look at the cgi section in the apache configuration file.

For example, see which profile the current PS1 is in

PS1 (Prompt Sign) is a first-level terminal command prompt. If you want to modify the current command prompt style, you only need to assign values to PS according to its syntax. Now we need to see which profile PS1 is currently in effect.

Note, at ~/. Bashrc, ~/. Bash_ Configuration of PS1 in profile, /etc/bashrc, profile files takes effect. The specific differences are:

  • /etc/profile
    This file sets the environment information for each user of the system and is executed when the user logs on for the first time. [All users, effective at first login]
  • /etc/bashrc (ubuntu is/etc/bash.bashrc)
    Execute this file for each user running the bash shell. When the bash shell is opened, the file is read. [All users, effective when Bash is turned on]
  • ~/. bash_profile (ubuntu is ~/.profile)
    Each user can use this file to enter shell information for their own use. By default, it sets some environment variables to execute the user's ~/. bashrc file. [Current user, effective at logon]
  • ~/.bashrc
    This file contains bash information specific to your bash shell, which is read when you log in and each time you open a new shell. [Current user, effective when Bash is turned on]


Example: View the configuration of the plug-in in vim

The Plug management plug-in used here, so search for Plug by keyword.

whereis/which Executable Finder

The which and whereis commands can be used to find executables. The difference is that which is queried by the PATH environment variable and whereis by the index database.

Parameter Display

Example: Find the location of the ls program


The results of the whereis lookup are obviously more detailed, with the first being the path to the ls command. Article 2 and 3 are the paths where the manual page is located.

Example: Find a python program

This example can clearly compare the differences between the two.
Whch looks through PATH, so only one program, /usr/bin/python, is found.

whereis is used to find all python programs

Path Finding for Typee Executable Programs

Typically, the type command is used to determine if a command is a built-in command. But its parameters provide a choice of paths to search for

Simple use

The type-A option lists all the information about the NAME parameter name. The -p option lists the path of the command as if it were using the which command.

Special Scene Query

Example: Find files generated/modified within a period of time

Scenario: The general rule for log file generation is to generate a file when it is larger than a certain predefined value. For example, 20220101.log, 20220101.log.1. 20220101.log.2....

Now you'll get the log files that were generated during a certain time period of the day. find's time-based query can be used here.

1. Use create_ File. The sh script simulates the generation of log files. Generate files from file1 to file15.

2. Now let's query file_6 ~ file_10 files. A time-based comparison using find.

This command queries later than file_6, and files generated/modified earlier than file10. If we modify file_now 10 files so that the time is up to date, then executing this command will show you the effect below.

Topics: Linux bash