Examples of file search commands on Linux



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
# 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

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

locate -c /etc/pro

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

locate -i topoftencent.class.php

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


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

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

[[email protected] ~]# find / -user apache

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

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

[[email protected] ~]# find /var/www -mtime -2

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

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

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

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

[[email protected] ~]# find /var/lib/mysql -gid 27


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} < – […]