The which-whereis-locate-grep find command is used
To find a file, you can use the following commands:
which | View the location of executable files |
---|---|
whereis | View the location of executable files and related files |
locate | With the database cache, quickly view the file location |
grep | Filter Matches, which is a file search tool |
find | Find relevant files |
For example:
[root@xuegod63 ~]# which cd
/usr/bin/cd
[root@xuegod63 ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd1..gz /usr/share/man/man1p/cd1.p.gz
[root@xuegod63 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls1..gz /usr/share/man/man1p/ls1.p.g
locate
Copy the code
The locate command is a variant of the locate command, but it is much faster than the find command because it searches for a specific directory file. And locate it search is a database of/var/lib/mlocate/mlocate db, have all the files in the local information in the database; This database is created automatically by Linux and is automatically updated and maintained daily. The configuration information is stored in /etc/updatedb.conf, and the scheduled task information is stored in /etc/cron.daily/mlocate
[root@xuegod63 ~]# yum -y install mlocate
[root@xuegod63 mnt]# touch /opt/xuegod.txt
[root@xuegod63 mnt]# locate xuegod. TXT # locate xuegod
[root@xuegod63 mnt]If you are looking for files of the day, you need to manually update the updatedb database
[root@xuegod63 mnt]# locate xuegod
Copy the code
Grep Search use
Function: Filter, which can use regular expressions to search for text and print the results as parameters:
-v | The not |
---|---|
-i | Ignore case |
^ # | Begin with # |
# $ | Ends in # |
^ $ | A blank line |
-n | Add line numbers to the filtered content |
l | Or means |
[root@xuegod63 ~]# ps -aux | grep sshd | grep -v grep
root 1089 0.0 0.2 105996 4088 ? Ss 20:19 0:00The/usr/sbin/SSHD - D [root @ xuegod63 ~]# cat/etc/passwd | grep ^ # with a a
[root@xuegod63 ~]# grep bash$/etc/passwd
[root@xuegod63 ~]# grep "nologin\|root" /etc/passwd | wc -lNote: \ indicates an escape character [root@xuegod63 ~]# egrep "nologin|root" /etc/passwd | wc -lNote: egrep is an enhanced version of grepCopy the code
The find command uses
Format: find pathname -options [-print] command word pathname option outputCopy the code
Parameter: pathname: path of the directory found by the find command. If no value is entered, it indicates the current directory. To represent the current directory, and/to represent the system root directory.
Find command options:
-name | Find file ‘name’ by file name |
---|---|
-perm | Find files by file permission. 666, 777, etc. |
-user | Find files by file owner |
-group | Find files by the group to which they belong |
-mtime | -n/ +n Finds the file by the time it was changed |
– n | Indicates that the file was changed within n days |
+ n | Indicates that the file was changed n days ago |
-type | Find a file of a certain type |
b – | Block device file |
d- | directory |
c – | Character device file |
p – | Pipeline file |
l- | Symbolic link file |
f – | Common file |
-size n | Finds files that match the specified file size |
-exec | Execute any other Linux command given by this parameter on the matching file. The corresponding command is of the form ‘command {} \ note {} and \; {} indicates the content to be found |
Example 1: You want to find files in the root directory that have been changed within 1 day |
[root@xuegod63 ~]# find /root/ -mtime -1
Copy the code
The -exec option can be followed by a custom SHELL command in the following format:
Example 2:
[root@xuegod63 ~]# touch {1, 2, 3}. The back
[root@xuegod63 mnt]# find . -name "*.back" -exec ls -l {} \;
Copy the code
Example 3:
[root@xuegod63 ~]# find . -name "*.back" -exec mv {} /opt \;
[root@xuegod63 ~]# ls /opt/
1.back 2.back 3.back rh xuegod.txt
Copy the code
Example 4: copy the found file to a specified directory
[root@xuegod63 mnt]# find /root -name "*.txt" -exec cp {} /opt\;
Copy the code
Example 5: Copy files with the xargs and find commands
-i indicates that the result passed by find to xargs is replaced by {} [root@xuegod63 ~].# rm -rf /opt/* [root@xuegod63 ~]# find . -name "*.txt" | xargs -i cp {} [root@xuegod63 ~]# ls /opt/Copy the code
Example 6: Find multiple types of files
The use of comparators:
-a | And and |
---|---|
-o | The or or |
+ | More than |
– | below |
[root@xuegod63 ~]# touch a.pdf back.sh
[root@xuegod63 ~]# find . -name "*.sh" -o -name "*.pdf"
[root@xuegod63 ~]# find /etc -size +20k -a -size -50k | wc -l
22
[root@xuegod63 ~]# find /etc -size +20k | wc -l
49
Copy the code
Example 7: Search by permission: -perm
[root@xuegod63 ~]# find /bin/ -perm 755# is equal to the0755Permission file or directory [root@xuegod63 ~]# find /bin/ -perm-644 # find /bin/ -perm-644 # find /bin/ -perm-644
Copy the code
For example, view the files or directories in the system whose permission is at least 777 and create some test files:
[root@xuegod63 ~]# mkdir ccc
[root@xuegod63 ~]# chmod 777 ccc
[root@xuegod63 ~]# mkdir test
[root@xuegod63 ~]# chmod 1777 test
[root@xuegod63 ~]# touch b.sh
[root@xuegod63 ~]# chmod 4777 b.sh
Copy the code
Looking for: [root@xuegod63 ~]# find /root/ -perm 777 [root@xuegod63 ~]# find /root/ -perm 1777 [root@xuegod63 ~]# find /root/ -perm 4777 example: Find the dangerous directory whose permission is no less than 777 in the system
[root@xuegod63 ~]# find /root/ -perm-777 # find /root/ -perm-777 # find /root/ -perm-777
Copy the code
Example: the system permission is not lower than 777 dangerous files to find out
[root@xuegod63 ~]# find / -type f -perm-777
Copy the code
Example 8: Directory depth to look for:
-maxdepth 1 # Searches for files and directories at the first level of the directory. For example, searches for files in the /bin directory with permission equal to 644
[root@xuegod63 ~]# find /etc/ -maxdepth 1 -perm 644 | more
[root@xuegod63 ~]# find /bin/ -maxdepth 1 -perm 755 #/bin
[root@xuegod63 ~]# find /bin -maxdepth 1-perm 755 # find /bin -maxdepth 1-perm 755 #
Copy the code
Example 9: Find all the files belonging to user1 in the system, and put this file in the /root/findresults directory
Note: /root/findresults this needs to be created in advance.
root@xuegod63 ~]# mkdir /root/findresults
[root@xuegod63 ~]# useradd user1
[root@xuegod63 ~]# find / -user user1 -exec cp -a {} /root/findresults/
Copy the code
# error when copying files to keep all properties of the original file:
Find: '/ proc /43475/task/43475/fd/6': no file or directory find:' /proc/43475/task/43475/fdinfo/6': no file or directory find:' /proc/43475/fd/6': no file or directory find:' /proc/43475/fdinfo/6': No file or directory cp: No directory"/home/user1"To override non-directories"/root/findresults/user1"
Copy the code
Can I create file user1 and folder user1 in the same directory? Is the file name created in the same directory the same as the directory name? Can not be
[root@xuegod63 ~]# touch abc
[root@xuegod63 ~]# mkdir abcMkdir: Cannot create directory"abc": The file already existsCopy the code
Solution:
[root@xuegod63 ~]# find / -user user1 # find
[root@xuegod63 ~]'/var/spool/mail/user1
[root@xuegod63 ~]# ll /home/user1The names of /var/spool/mail/user1 and /home/user1 are the same. While both are copied to /root/findresults/, /var/spool/mail/user1 is copied first, so /home/user1 cannot be copied. [root@xuegod63 ~]# mv /var/spool/mail/user1
[root@xuegod63 ~]# rm -rf /root/findresults/ *
/var/spool/mail/user1.mail
[root@xuegod63 ~]# find / -user mk -exec cp -a {} /root/findresults/ \;
[root@xuegod63 ~]# mv /var/spool/mail/user1.mail/var/spool/mail/user1 #Copy the code
This is the end of the article for more technology +Copy the code