Linux file search common commands – detailed notes

Time:2021-8-30

Which whereis locate grep find command

The following commands are used to find files:

which View the location of the executable
whereis View the location of executable files and related files
locate With the database cache, you can quickly view the file location
grep Filter matching, which is a file search tool
find Find related files

give an example:

[[email protected] ~]# which cd
/usr/bin/cd
[[email protected] ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
[[email protected] ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.g
locate

The locate command has the same function as find – name. It is another way to write it, but it is much faster than find search because
The find command looks for a specific directory file, while locate searches for a database
/var/lib/mlocate/mlocate.db, this database contains all local file information; This database is created automatically by Linux
Build and automatically update maintenance every day. The relevant configuration information is in the/etc/updatedb.confTo view scheduled task information
/etc/cron.daily/mlocate

[[email protected] ~]# yum -y install mlocate
[[email protected] mnt]# touch /opt/xuegod.txt
[ [email protected] Mnt]# locate xuegod.txt # not found
[ [email protected] Mnt]# updatedb # if you want to find the files of the day, you need to manually update the database updatedb
[[email protected] mnt]# locate xuegod

Grep find using

Function: filtering. It can use regular expressions to search text and print the results
Parameters:

-v Reverse
-i ignore case
^# Start with #
#$ End with #
^$ Blank line
-n Add a line number to the filtered content
l Or what do you mean
[[email protected] ~]#   ps -aux | grep sshd | grep -v grep
root  1089  0.0  0.2 105996   4088 ?   Ss   20:19  0:00 /usr/sbin/sshd –D
[ [email protected] ~]#Cat / etc / passwd | grep ^ a # starts with a
[ [email protected] ~]#Grep bash $/ etc / passwd # ends with Bash
[[email protected] ~]# grep "nologin\|root" /etc/passwd | wc -l
Note: \ indicates escape character
[[email protected] ~]# egrep "nologin|root" /etc/passwd | wc -l
#View the row that includes nologin or root
Note: egrep is an enhanced version of grep

Use the find command

Format: find pathname - options [- Print]
     Command word path name option output

Parameters:
Pathname: the directory path found by the find command. It does not represent the current directory. For example, use. To represent the current directory, use/
To represent the system root directory.

Find command options:

-name Find file ‘name’ by file name
-perm Find files by file permissions. 666 777 et al
-user Find files by file owner
-group Find files by the group to which they belong
-mtime -N / + n find the file according to the change time of the file
– n Indicates that the file change time is within n days from now
+ n Indicates that the file was changed n days ago
-type Find a type of file
b – Block device file
d- catalogue
c – Character device file
p – Pipeline file
l- Symbolic link file
f – Ordinary file
-size n Finds files that match the specified file size
-exec Execute other linux commands given by this parameter on the matching files. The corresponding commands are in the form of ‘command {} \ note {} and \; The space between {} represents the content found

Example 1: you want to find a modified file in the root directory that has been changed within 1 day

[[email protected] ~]# find /root/ -mtime -1

Execute the corresponding command on the search content
-The exec option parameter can be followed by a customized shell command. The format is as follows:
Linux file search common commands - detailed notes

Example 2:

[[email protected] ~]# touch {1,2,3}.back
[[email protected] mnt]# find . -name "*.back" -exec ls -l {} \;

Example 3:

[[email protected] ~]#   find . -name "*.back" -exec mv {} /opt \;
[[email protected] ~]# ls /opt/
1.back  2.back   3.back  rh   xuegod.txt

Example 4: copy the found file to a specified directory

[[email protected] mnt]# find /root -name "*.txt" -exec cp {} /opt\;

Example 5: copy files with xargs and find commands

-I means that the result passed from find to xargs is replaced by {}
Solution)
[[email protected] ~]# rm -rf /opt/*
[[email protected] ~]# find . -name "*.txt"   | xargs    -i cp {}
[[email protected] ~]# ls /opt/

Example 6: find multiple types of files

Use of comparators:

-a And also
-o Or or
+ exceed
lower than
[[email protected] ~]# touch a.pdf back.sh
[[email protected] ~]# find . -name "*.sh" -o -name "*.pdf"
[[email protected] ~]# find /etc -size +20k -a -size -50k | wc -l
22
[[email protected] ~]# find /etc -size +20k   | wc -l
49

Example 7: search by permission: – perm

[[email protected] ~]# find /bin/ -perm 755
#File or directory with permissions equal to 0755
[ [email protected] ~]#Find / bin / - perm - 644 # - indicates a file or directory with at least 644 permissions

Example: view files or directories with at least 777 permissions in the system
Create some test files:

[[email protected] ~]# mkdir ccc
[[email protected] ~]# chmod 777 ccc
[[email protected] ~]# mkdir test
[[email protected] ~]# chmod 1777 test
[[email protected] ~]# touch b.sh
[[email protected] ~]# chmod 4777 b.sh

Find:
[[email protected] ~]# find /root/ -perm 777
[[email protected] ~]# find /root/ -perm 1777
[[email protected] ~]# find /root/ -perm
4777
Example: find out the dangerous directory with permission no less than 777 in the system

[ [email protected] ~]#Find / root / - perm-777 # has at least 777 permissions

Example: find out the dangerous files with permission no less than 777 in the system

[[email protected] ~]# find / -type f -perm-777

Example 8: find directory depth:

-Maxdepth 1 # only finds files and directories at the first level of the directory
For example, find files with permissions equal to 644 in the / bin directory

[[email protected] ~]# find /etc/ -maxdepth 1 -perm 644 | more
[ [email protected] ~]#Find / bin / - maxdepth 1 - perm 755 # / bin should be followed by/
[ [email protected] ~]#Find / bin - maxdepth 1 - perm 755 # not added / unable to meet our needs

Example 9: find all files belonging to user user1 in the system and put this file in the / root / findresults directory

Note: / root / findresults needs to be created in advance.

[email protected] ~]# mkdir /root/findresults
[[email protected] ~]# useradd user1
[[email protected] ~]# find / -user user1 -exec cp -a {} /root/findresults/

Parameter: – a when # copying, all attributes of the original file are retained

report errors:

Find: '/ proc / 43475 / task / 43475 / FD / 6': there is no such file or directory
Find: '/ proc / 43475 / task / 43475 / fdinfo / 6': there is no such file or directory
Find: '/ proc / 43475 / FD / 6': there is no such file or directory
Find: '/ proc / 43475 / fdinfo / 6': there is no such file or directory
CP: cannot overwrite non directory "/ root / findresults / user1" with directory "/ home / user1"

Can the file user1 and the folder user1 be created in the same directory? File name and directory created in the same directory
Same name? may not

[[email protected] ~]# touch abc
[[email protected] ~]# mkdir abc
MKDIR: cannot create directory 'ABC': file already exists

solve:

[ [email protected] ~]#Find / - user user1 # discovery
[ [email protected] ~]#Ll / var / spool / mail / user1 # view this file
[[email protected] ~]# ll /home/user1
The names of / var / spool / mail / user1 and / home / user1 were found to be the same. And both have to be copied to
/Under root / findresults /, / var / spool / mail / user1 is copied first, so / home / user1 cannot be copied.
[[email protected] ~]# mv /var/spool/mail/user1
[[email protected] ~]# rm -rf /root/findresults/ *
/var/spool/mail/user1.mail
[[email protected] ~]# find / -user mk -exec cp -a {} /root/findresults/ \;
[[email protected] ~]# mv /var/spool/mail/user1.mail
/var/spool/mail/user1
#Modify it again
This concludes the article

Want to know more about technology+
Linux file search common commands - detailed notes