Examples of file search commands on Linux

Time:2019-9-10

locate

Basic understanding

There is no location command by default on centos7, so you need to install it manually first. Installation steps: http://www.cnblogs.com/feanmy/p/7676717.html

Background database path searched by location command: / var/lib/mlocate/mlocate.db


ls -hl /var/lib/mlocate
total 1.2M
-rw-r----- 1 root slocate 1.2M Oct 16 14:36 mlocate.db

Update the database using updatedb, the configuration file is / etc / updatedb. conf

# Open Search Restrictions
PRUNE_BIND_MOUNTS = "yes"
# Unsearched File Systems
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
# Unsearched file types
PRUNENAMES = ".git .hg .svn"
# Unsearched file paths
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"

Command Options

Knowing that a command can use man, such as man locate, can display locate-related options. Here are just a few common options
– c, — count) — Output only the number found
– i, — ignore-case
– Quiet mode does not display any error messages
– r, – – regexp REGEXP. Use basic regular expressions
Using Extended Regular Expressions
– n) The results show n files

Use examples

1. Loce/etc/pro: Query out all files starting with Pro in the / etc directory


locate /etc/pro
/etc/profile
/etc/profile.d
/etc/protocols
/etc/profile.d/256term.csh
/etc/profile.d/256term.sh
/etc/profile.d/colorgrep.csh
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.csh
/etc/profile.d/colorls.sh
/etc/profile.d/lang.csh
/etc/profile.d/lang.sh
/etc/profile.d/less.csh
/etc/profile.d/less.sh
/etc/profile.d/vim.csh
/etc/profile.d/vim.sh
/etc/profile.d/which2.csh
/etc/profile.d/which2.sh

2. Loce-c/etc/pro: Display the number of matched files


locate -c /etc/pro
17

3. locate-i topoftencent. class. php: ignoring file name case


locate -i topoftencent.class.php
/var/www/html/fxyxManage/Spider/TopOfTencent.class.php

The – I option still matches the file name, although the case is not the same.

find

Basic understanding

Find is slower than locate in searching files, but its functions and parameters are stronger than locate. Avoid large-scale searches when using find

Command Options

Here are the common options, other options can be learned through man find

- Name filename # Find a file named filename
- Iname filename # Find file filename, case-insensitive
- Gid n # Find files with a genus group GID of n
- Username # Search by Document Owner
- Group groupname # Search by group
- mtime-n +n# Find files by file change time, -n refers to within n days, +n refers to before n days.
- atime-n+n# Check GIN: 0px "> by file access time
- ctime-n+n Find files by the time they were created, -n means within n days, +n means before n days.
- No group
- nouser # does not find a valid owner file, that is, the owner of the file does not exist in / etc / passwd
- Newer f1! F2 # Check change time is newer than F1 but older than F2
- Type # Check is block device, directory, character device, pipeline, symbolic link, common file
- Size n [c]

Use examples

1. find / etc – name profile: Find a file named profile in the / etc directory


[[email protected] ~]# find /etc -name profile
/etc/profile

2. find / user mysql: Find a file whose owner is MySQL in the root directory


[[email protected] ~]# find / -user apache
/proc/29936/task
/proc/29936/task/29936
/proc/29936/task/29936/attr
/proc/29936/net
/proc/29936/attr
/proc/29937/task
/proc/29937/task/29937
/proc/29937/task/29937/attr
/proc/29937/net
/proc/29937/attr
......

View the file properties of / proc / 29936 / task

[[email protected] ~]# ll /proc/29936/task
total 0
Dr-xr-xr-x 6 Apache 0 Oct 16 14:12 29936______________ belongs to Apache

3. find / var / lib – group apache: Find files under / var / lib by subgroup Apache


[[email protected] ~]# find /var/lib -group apache
/var/lib/dav
/var/lib/php/session
/var/lib/php/session/sess_7vtaesehg11f45ljrsh98k3s57
/var/lib/php/session/sess_uubh9p4tvc1mohopepndg9m3d3
/var/lib/php/session/sess_aovaqofcs4918vsl0mhquf79a7
/var/lib/php/session/sess_pm8lgnn78c9jh54umjnk0gsu14
/var/lib/php/session/sess_i1tc9i6n0chddlfp250oqun8f2
/var/lib/php/session/sess_2c1vrgb6lqgifg7lruaoq5u6s2
/var/lib/php/session/sess_3vc0gbkpl1m4hb2h5mr7nbgvk4
/var/lib/php/session/sess_rditv2odgr4vg7jmdu8sfvdm90
/var/lib/php/session/sess_a6m2fkh0cel9uf7tqo48qmphs5
/var/lib/php/session/sess_eierot422pov01ognbjo2dkqv1
/var/lib/php/session/sess_1ei3jbop7osama4m375qrsqqc5
/var/lib/php/session/sess_vovl6rrmb66a570dncpgp49p22
/var/lib/php/session/sess_6ef5d9r6fhatqie8apr28odgn0
/var/lib/php/session/sess_1o1rv6o963v3nr5sdui3hodll0

4. Files modified within 2 days under find/var/www-mtime-2:/var/www directory


[[email protected] ~]# find /var/www -mtime -2
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/b595b808c2ebeeeb0478ffc30bb4e87a.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/e82f9209c88dd6a459bf8b3d6299d38c.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/dfa8fa69d0f81d1ba09048823f5e779b.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/3fd17d8cbb99d40eed8370916fbba7bf.php
/var/www/html/fxyxManage/Application/Runcache/Cache/Admin/32ec6c906c117a7c2bf504769276d87a.php
/var/www/html/fxyxManage/Application/Runcache/Logs/Weixin

Options – atime, – CTime are used in the same way as – Mtime

5. find/etc/httpd-type d: Find directories under / etc/httpd, -type according to file type


[[email protected] ~]# find /etc/httpd -type d
/etc/httpd
/etc/httpd/conf
/etc/httpd/conf.modules.d
/etc/httpd/conf.d

6. find/var/www-iname topoftencent.class.php: Open case-insensitive file name lookup


[[email protected] ~]# find /var/www -iname topoftencent.class.php
/var/www/html/fxyxManage/Spider/TopOfTencent.class.php

7. find/var/lib/mysql-gid 27: Find files with subgroup ID 27


[[email protected] ~]# find /var/lib/mysql -gid 27
/var/lib/mysql/auto.cnf
/var/lib/mysql/ibdata1
/var/lib/mysql/ib_logfile0
/var/lib/mysql/performance_schema
/var/lib/mysql/performance_schema/accounts.frm
/var/lib/mysql/performance_schema/threads.frm
......

summary

The above is an example of file search command on Linux introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to developpaer.

Recommended Today

Write a package for parsing abnormal JSON strings – fbbk-json

F [beep] [beep] K JSON Parse a package of abnormal JSON strings. install $ npm install fbbk-json Use This package has only one function: var fJSON = require(“fbbk-json”); fJSON.parse(A_JSON_STRING); however This package supports the following JSON strings (that is, what we usually declare in javascript, without quotation marks). Raise a chestnut. ‘{foo”:”bar”,”baz”: true} < – […]