Learn a Linux command every day (20): find

Brother of migrant workers 2021-01-21 19:56:40
learn linux command day


find Command searches the file system for files and directories .

find The command is used to find files in the specified directory , Any string before the parameter is treated as a directory name . Use find On command , How to set no parameters , be find Command will find subdirectories and files in the current directory , And all the subdirectories and files found will be displayed .

find It's also a very powerful tool for processing Linux The tool of file on the system , It can look up files alone , It can also work with other programs ( command ) Combined with the need to deal with the file operation .

Additional explanation

Linux file type :

f # Ordinary documents
l # Symbolic connection
d # Catalog
c # Character device
b # Block device
s # Socket
p #Fifo

UNIX/Linux Every file in the file system has three timestamps :

  • Access time (-atime/ God ,-amin/ minute ): Last time user visited .
  • Modification time (-mtime/ God ,-mmin/ minute ): The last time the file was modified .
  • Time of change (-ctime/ God ,-cmin/ minute ): File metadata ( Such as authority )

Grammar format

find [ Directory path ] [ Options ] [ file name ]
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

Option description

-amin< minute >:# Find files or directories that have been accessed in a specified time , Units of minutes
-mmin< minute >:# Find a file or directory that has been changed in a specified time , Units of minutes
-mtime<24 Hours >:# Find a file or directory that has been changed in a specified time , Company 24 Hours
-atime<24 Hours >:# Find a file or directory that has been written at a specified time , Company 24 Hours
-cmin< minute >:# Find a file or directory that has been changed within a specified time
-ctime<24 Hours >:# Find the file or directory that was changed at the specified time , Unit to 24 Hour count
-daystart:# Counting time from today
-depth:# Start from the deepest subdirectory under the specified directory
-exec< Execution instruction >:# If find The return value of the command is True, Just execute the command
-false:# take find The return value of the command is set to False
-fstype< File system type >:# Only find the file or directory under the specified file system type
-gid< Group ID >:# Find a file or directory that matches this group ID
-group< The name of the group >:# Find a file or directory that matches the name of this group
-help or ——help:# Help information
-links< Number of connections >:# Find files or directories that match the specified number of hard connections
-maxdepth< Directory level >:# Set the maximum directory level for lookup
-mindepth< Directory level >:# Set the minimum directory level for lookup
-name< Template Styles >:# Specify a string as a template for finding a file or directory
-path< Template Styles >:# Specifies a string as the template style for finding directories
-perm< Authority value >:# Find a file or directory that matches the specified permission value
-size< file size >:# Find files that match the specified file size
-true: # take find The return value of the command is set to True
-type< file type >:# Only find files that match the specified file type
-uid< User identification number >:# Only find files or directories that match the specified user ID
-user< Owner name >:# Only find files or directories that match the specified owner name
-version:# Display version information 

Application, for example,

Search all files in the current directory , The file contains “192.168.1.111” The content of

find . -type f -name "*" | xargs grep "192.168.1.111"

Match based on a file or regular expression , Find the file or directory you need

# List all files and folders in the current directory and subdirectories
[root@centos7 ~]# find .
# stay /mingongge The directory contains mingongge The first filename
[root@centos7 ~]# find /mingongge -name "mingongge*.log"
/mingongge/mingongge_errors.log
/mingongge/mingongge.log
/mingongge/mingongge_test.log
# Search all the current directories and subdirectories to .txt and .log Final document
[root@centos7 ~]# find . \( -name "*.txt" -o -name "*.log" \)
or
[root@centos7 ~]# find . -name "*.txt" -o -name "*.log"
# Match file path or file
[root@centos7 ~]# find /usr/ -path "*txt*"
# Match file path based on regular expression
[root@centos7 ~]# find . -regex ".*\(\.txt\|\.log\)$"
# Ignore case
[root@centos7 ~]# find . -iregex ".*\(\.txt\|\.log\)$"

find Examples of negative parameter usage

# find /mingongge Next is not to .log Final document
[root@centos7 ~]# find /mingongge ! -name "*.log"

Search based on directory depth

# The maximum depth down is limited to 5
[root@centos7 ~]# find . -maxdepth 5 -type f
# Search out the depth at least... From the current directory 3 All files in subdirectories
[root@centos7 ~]# find . -mindepth 3 -type f

Search by file timestamp

# Find the latest 10 All the files that have been accessed in days
[root@centos7 ~]# find . -type f -atime -10
# Find more than 10 All the files that have been accessed in days
[root@centos7 ~]# find . -type f -atime +10
# Search access time is longer than 20 Minutes of all documents
[root@centos7 ~]# find . -type f -amin +20
# Find out the ratio mingongge All documents with longer modification time
[root@centos7 ~]# find . -type f -newer mingongge

Delete the found matching file

# Delete all... In the current directory .txt file
[root@centos7 ~]# find . -type f -name "*.txt" -delete

According to the document authority / Ownership matches :

# In the current directory, find out that the permission is 777 The file of
[root@centos7 ~]# find . -type f -perm 777# Find out the owner of the current directory mingongge All files for
[root@centos7 ~]# find . -type f -user mingongge
# Find out the user group in the current directory as mingongge All files for
[root@centos7 ~]# find . -type f -group mingongge # Find out that the permissions under the current directory are not 644 Of .log file
[root@centos7 ~]# find . -type f -name "*.log" ! -perm 644

find and -exec Combination of options

# Find out that the owner of the current directory is root The file of , And change the owner to mingongge This user
[root@centos7 ~]# find .-type f -user root -exec chown mingongge {} \;
# Find out all the .log File and execute delete action
[root@centos7 ~]# find $HOME/. -name "*.log" -exec rm {} \;
# Find all... In the current directory .log File and put them together and write them to mingongge.txt In this file
[root@centos7 ~]# find . -type f -name "*.log" -exec cat {} \;> /mingongge.txt
# Find out 10 The day before .log file , And then all move to mingongge Under the table of contents
[root@centos7 ~]# find . -type f -mtime +10 -name "*.log" -exec cp {} mingongge \;
# Find all... In the current directory .log file , And then to “File: file name ” Print out to the screen
[root@centos7 ~]# find . -type f -name "*.log" -exec printf "File: %s\n" {} \;

Find the target file according to the file size

# Find a file in the current directory that is larger than 500M The file of
[root@centos7 ~]# find . -type f -size +500M
./mingongge/backup_file.tar.gz
./mingongge/upload.tar.gz
# Find the current directory more than 500M The file of , And print out the detailed attribute information of the file
[root@centos7 ~]# find . -type f -size +800M -print0 | xargs -0 ls -l
-rw-r--r-- 1 root root 4250099200 Apr 15 2019 ./mingongge/backup_file.tar.gz
-rw-r--r-- 1 root root 6832225765 Oct 14 12:57 ./mingongge/upload.tar.gz
# Find the current directory more than 500M The file of , And print out the specific size of the file
[root@centos7 ~]# find . -type f -size +500M -print0 | xargs -0 du -h|sort -nr
6.8G ./mingongge/upload.tar.gz
4G ./mingongge/backup_file.tar.gz

Find the front page in the system 5 The large files

# find / -type f -print0 | xargs -0 du -h | sort -rh | head -n 5
1.1G /download/ubuntu-17.04-desktop-amd64.iso
377M /download/app_backup.tar.gz
100M /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
93M /usr/lib/firefox/libxul.so
84M /var/lib/snapd/snaps/core_3604.snap

There are many ways , Need to be used with other commands , To find out .

find Commands have powerful functions in file search and other applications , Learn system commands ,find Command is something you need to focus on , Whether it's normal study , Or at work in the future , This command has a very important role .

This article is from WeChat official account. - The road of technology for migrant workers (jishuroad)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2021-01-17

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Brother of migrant workers]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121195549602g.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云