Find command, find

Source: Internet
Author: User
Tags egrep

Find command, find
Syntax

Find [Option] [path...]

Option
Option Description
-Help or -- help Online Help
-name <pattern> Shell Mode<pattern>Match the file name.
<pattern>It can be enclosed in quotation marks or not, but it is best to enclose it in quotation marks.
-iname <pattern> Similar to-name, but case-insensitive
-Ls List file or directory names to standard output
-exec command ; Run the command. For detailed usage, see the following-exec command ;Description
-type <c> Only find files that match the specified file type. c is a character.
-Prune If the file is a directory, it will not enter this directory
-regex <pattern> Use a regular expression to match the file name. This only matches the full path of the file, not the file content.
-regextype <type> Change the regular expression syntax that is understood by the-regex and-iregex tests that will appear later in the command line.
Currently implemented<type>Emacs (which is the default one), posix-awk, posix-basic, posix-UNK p, and posix-extended.
-Print List file or directory names to standard output. The format is one name for each column, and each name is preceded by a "./" String
Example: list all files and folders in the current directory and subdirectory

find .

Find the. java file in the current directory

find . -name "*.java"

Search for all. java and. cpp files in the current directory and subdirectory

find . -name "*.java" -o -name "*.cpp" 
find . \( -name "*.java" -o -name "*.cpp" \)

Or use a regular expression.

find . | egrep ".*(\.java|\.cpp)$" 
find . -regextype "posix-egrep" -regex ".*(\.java|\.cpp)$"

Find the files in the current directory that do not end with. cpp

find . ! -name "*.cpp"

Search by file type

Find.-type <type parameter>

Type parameter Description
F Common File
L Symbolic Link
D Directory
C Character Device
B Block Device
S Socket
P Pipe FIFO
Delete matching files

find . -type f -name "*.class" -delete

Find all. java files in the current directory, but skip the subdirectory chapter02

find . -path "./src/com/fireway/util" -prune -o -name "*.java" -print

-exec command ;Usage instructions

-execThe option is followed by the command or script to be executed, followed by a pair of braces{}, A space, and a backslash\Add a semicolon. Because the semicolon;It has special significance in the bash environment, so it uses the backslash to escape.

{}Curly braces indicate the names of the files found by the previous find. 
Example:

find . -exec ls -l {} \;

Example: Delete

find . -exec rm -rf {} \;

For example, rename all the current Android. mk files to Android. mk_bak.

find . -name "Android.mk" -exec mv {} {}_bak \;

But one bad thing is that you can only add a suffix to the backend.
For example, if you want to change the suffix. class to. clazz, you can write a shell script.

for file in $(find . -name "*.class" -type f); do mv ${file} ${file/.class/.clazz}; done

For more information about how to replace shell variables, see "shell variable details.

Use find in combination with xargs

Note that xargs is a separate command, not an option in find.

References
  • About the '-- exec' parameter (find command) and introduction to the differences between the 'xargs 'command (new version)
  • Find command
  • Find batch RENAME [change]
  • Shell variable details
  • How to use regular expressions in the linux find command
  • Xargs command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.