File LookupFind
------directory------
1. Search Conditions
2, after the file search processing
----------------
1. Search Conditions
1.1) Matching of file names && Multi-condition name matching
You can use regular expressions to match, and-a or-o in multiple conditions for stitching
[email protected]:~# find \ (-name "list*"-o-name "config*" \) -print
./list
./tools/django-1.7.9/django/views/generic/list.py
./tools/django-1.7.9/django/contrib/comments/templates/comments/list.html
./tools/django-1.7.9/build/lib.linux-x86_64-2.7/django/views/generic/list.py
./tools/django-1.7.9/build/lib.linux-x86_64-2.7/django/contrib/comments/templates/comments/list.html
./tools/django-1.7.9/tests/generic_views/templates/generic_views/list.html
./test_shell/module/config.sh
./list2
Regular way to find the file name
. match any one
* Repeat before an arbitrary time
+ Repeat one or more times before
? Matches one or 0 of the previous character
[Email protected]:~# Find/tmp–regex ". *\ (\.py\|\.sh\) $"
./tmp/.pythontab.py
./tmp/install.sh
./tmp/testpty.py
./tmp/test.py
./tmp/test.sh
Using regular and multi-conditional name matching
[Email protected]:~# FInd./tmp \ (!-regex ". *py.*"-a-name "*.c" \)
./tmp/tcpportping.c
./tmp/pid.c
./tmp/test.c
[Email protected]:~# find/tmp-name "*.c"
./tmp/tcpportping.c
./tmp/pid.c
./tmp/test.c
./tmp/test.py.c
1.2) search based on find path
Limit the depth of the search path
[Email protected]:~# find ./-maxdepth 2 -type f-name "*.py"
./tmp/.pythontab.py
./tmp/testpty.py
./tmp/test.py
1.3) Follow the search type to find
-F File
-D Directory
-L Soft Connect
-C Character device
-B block device
1.4) Search by search time
-atime: last time a user accessed a file
-mtime: Last time the user was modified
-ctime: file metadata (such as permissions or ownership) time of last modification
The above day is the unit
-cmin-mmin-amin three units in minutes
[Email protected]:~# Find/-type f-atime-7-regex ". *\ (\.py\|\.sh\) $"
./test.sh
./test_shell/module/config.sh
./test_shell/module/test1.sh
./test_shell/demo1.sh
./test_shell/module2/mail.sh
./test_shell/main.sh
./tmp/test.sh
1.5) search based on size of search
[Email protected]:~# Find/-size +2k-type f-atime-7-regex ". *\ (\.py\|\.sh\) $"
[Email protected]:~# Find/-size-2k-type f-atime-7-regex ". *\ (\.py\|\.sh\) $"
./test.sh
./test_shell/module/config.sh
./test_shell/module/test1.sh
./test_shell/demo1.sh
./test_shell/module2/mail.sh
./test_shell/main.sh
./tmp/test.sh
1.6) Search based on search permissions && owner
[Email protected]:~# Find/-perm 644-size-2k-type f-atime-7-regex ". *\ (\.py\|\.sh\) $"
./test_shell/module/config.sh
./test_shell/module2/mail.sh
[Email protected]:~# find./ -user root -perm644-size-2k-type f-atime-7-regex ". *\ (\.py\|\.sh\) $"
./test_shell/module/config.sh
./test_shell/module2/mail.sh
2, the operation after the search
Two ways to do the stitching
2.1) through -exec {} \; to be combined with other commands.
Such as
[Email protected]:~# Find/-maxdepth 2-type f-name "*.py"-exec ls-lh {}\;
-rw-r--r--1 root root./tmp/.pythontab.py
-rw-r--r--1 root root 124 Nov 16:37./tmp/testpty.py
-rw-r--r--1 root root 1.3K May 3 2016./tmp/test.py
We can understand this command format in this way. We will -exec \: As a fixed format with command stitching, and {} represents a file found through find .
[Email protected]:~# Find/-maxdepth 2-type f-name "*.py" -exec cat {} \; >>./findcont
[Email protected]:~# LS-LH
Total 40K
lrwxrwxrwx 1 root root 4 Jan backup-/data/backup
-rw-r--r--1 root root 2.9K Apr 1 15:17findcont
But one drawback here is that we can accept a limited number of commands if we want to take this document to the next step of complex processing. We may need to do this in the form of a function or a script argument.
2.2) Use xargs to achieve command binding
Xargs accepts the expression input through a pipeline and converts this input into a command-line argument of a particular name.
Bash hackers like single-line commands, one-line command is a sequence of commands, not using semicolons between the commands, can be more efficient processing, for text processing is also. Xargs is an essential component of building a single-line command.
We understand that the content obtained from the standard input is output to the corresponding command again as a parameter. Standard input for multiple rows
Xargs 's preliminary knowledge
-D number of points to perform standard input
-N How many elements are output after the standard input is separated by a delimiter
-i specifies an identifier to replace the parameter. Subsequent occurrences of an identifier will be replaced with a specific parameter.
but one of the pitfalls here is that we when entering the standard input (i.e. | The end of the pipeline) The output delimiter is uncertain or does not clearly know what to split the time, for follow-up to add the operation should be careful.
For example, we have a file name that contains a space, we find it through find, after |xargs is divided into a number of parameters, if we later combined with the command parameter is RM–RF , Then we will delete the contents of the original 123123 directory. And we meant to delete the 123123 123123.asdf file.
[Email protected]:~# find./-maxdepth1-name "123*" | Xargs-n 1
./123123
123123.asdf
./123123
[Email protected]:~# LS-LH
Total 44K
Drwxr-xr-x 2 root root 4.0K Apr 1 16:19,123,123
-rw-r--r--1 root root 0 Apr 1 16:18 123123 123123.asdf
How to avoid this problem, we need to Xargs add -0 ( number ) to define the
[Email protected]:~# Find/-maxdepth 1-name "123*" |xargs-0
./123123 123123.ASDF
./123123
[Email protected]:~# Find/-maxdepth 1-name "123*" |xargs
./123123 123123.ASDF./123123
the combination of Xargs and find
the combination of Xargs and find finds the appropriate file through find, through | The pipe is output to Xargs, and then xargs splits the input file and then assigns a parameter substitution identifier through-I. As with-exec 's {} , then our command format will become something like this.
[Email protected]:~# Find/-maxdepth 1-type f-name "*.sh" | Xargs-i {} LS-LH {}
-rwxr-xr-x 1 root root 1 15:22./test.sh
-rw-r--r--1 root root 0 Apr 1 16:38./123.sh
# Here the identity is set to 1, followed by 1 to represent. There are some symbols that cannot be used as identifiers.
[Email protected]:~# Find/-maxdepth 1-type f-name "*.sh" | Xargs-i 1 LS-LH 1
-rwxr-xr-x 1 root root 1 15:22./test.sh
-rw-r--r--1 root root 0 Apr 1 16:38./123.sh
This article is from the "Start from scratch" blog, be sure to keep this source http://atong.blog.51cto.com/2393905/1913344
Shell File Lookup command find