特殊:Badtitle/NS100:Find:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Wikibot留言 | 贡献
新页面: {{From|https://help.ubuntu.com/community/find}} {{Languages|UbuntuHelp:find}} == What Is find? == The GNU find command search files within within a directory and its subdirectories acco...
 
Wikibot留言 | 贡献
无编辑摘要
第1行: 第1行:
{{From|https://help.ubuntu.com/community/find}}
{{From|https://help.ubuntu.com/community/find}}
{{Languages|UbuntuHelp:find}}
{{Languages|UbuntuHelp:find}}
== What Is find? ==
== What Is find? ==
The GNU find command search files within within a directory and its subdirectories according to several criteria such as name, size and time of last read/write. It performs actions on the files found, by default it prints the name of the files.
The GNU find command search files within within a directory and its subdirectories according to several criteria such as name, size and time of last read/write. It performs actions on the files found, by default it prints the name of the files.
The GNU find command is installed on every Ubuntu system. This page is complemented by the manual('man') page for find which can be read by issuing the command "man find".
The GNU find command is installed on every Ubuntu system. This page is complemented by the manual('man') page for find which can be read by issuing the command "man find".
(See Self:BasicCommands for a quick introduction to the command line)
(See Self:BasicCommands for a quick introduction to the command line)
== The Basics ==
== The Basics ==
The general syntax for using finds is:
The general syntax for using finds is:
<pre><nowiki>
<pre><nowiki>
find [-H] [-L] [-P] [path...] [expression]</nowiki></pre>
find [-H] [-L] [-P] [path...] [expression]</nowiki></pre>
We will ignore the options [-H] [-L] [-P] for now , please refer to man find to learn their meaning, and start with the [path...].
We will ignore the options [-H] [-L] [-P] for now , please refer to man find to learn their meaning, and start with the [path...].
find search a directory and its subdirectories, before any expression you should specify the directory or the directories where find will start searching
find search a directory and its subdirectories, before any expression you should specify the directory or the directories where find will start searching
<pre><nowiki>
<pre><nowiki>
find . # search within the  current directory
find . # search within the  current directory
find /home/user1 /home/user2  #search within the directories home/user1 /home/user2
find /home/user1 /home/user2  #search within the directories home/user1 /home/user2
find # same as find . , by default GNU find uses  the current directory if no path is specified</nowiki></pre>
find # same as find . , by default GNU find uses  the current directory if no path is specified</nowiki></pre>
The above commands will print the names of all the files present in the hierarchy below the starting directories passed to find as arguments. "files" can be link, directories, hidden files ...
The above commands will print the names of all the files present in the hierarchy below the starting directories passed to find as arguments. "files" can be link, directories, hidden files ...
== Searching Files With A Specific Name ==
== Searching Files With A Specific Name ==
Of course, find would just be a poor version of ls without the [expression] part, let's start by the most common and perhaps most simple example:
Of course, find would just be a poor version of ls without the [expression] part, let's start by the most common and perhaps most simple example:
<pre><nowiki>
<pre><nowiki>
find dir -name 'myfile' </nowiki></pre>
find dir -name 'myfile' </nowiki></pre>
The  test -name 'name' will only search for an ''exact'' match of the name. This means that the above command
The  test -name 'name' will only search for an ''exact'' match of the name. This means that the above command
will find, in the directory dir, a file named "myfile" but not a file named "myfile.txt" or "thisismyfile". If you are looking for a file with "myfile" in the name somewhere, you should instead use the following:  
will find, in the directory dir, a file named "myfile" but not a file named "myfile.txt" or "thisismyfile". If you are looking for a file with "myfile" in the name somewhere, you should instead use the following:  
<pre><nowiki>
<pre><nowiki>
find dir -name '*myfile*' </nowiki></pre>
find dir -name '*myfile*' </nowiki></pre>
Here, the * is a wildcard, and can stand for any number of characters (number, letter, space...). It is a basic form of a pattern. See the find manual page for more on patterns. The most important thing here is:  
Here, the * is a wildcard, and can stand for any number of characters (number, letter, space...). It is a basic form of a pattern. See the find manual page for more on patterns. The most important thing here is:  
'''Do not forget to put quotes around your pattern.'''   
'''Do not forget to put quotes around your pattern.'''   
If the pattern is not quoted, it will be replace by the shell by the list of files matching the pattern in the current directory.  For example, if your current directory contains the file mytestfile and the directory myfiles, and the myfiles directory that has the file test.txt.
If the pattern is not quoted, it will be replace by the shell by the list of files matching the pattern in the current directory.  For example, if your current directory contains the file mytestfile and the directory myfiles, and the myfiles directory that has the file test.txt.
<pre><nowiki>
<pre><nowiki>
第51行: 第32行:
$ ls myfiles/
$ ls myfiles/
test.txt</nowiki></pre>
test.txt</nowiki></pre>
and we run the above find command
and we run the above find command
<pre><nowiki>
<pre><nowiki>
$ find . -name *test* #<-- wrong
$ find . -name *test* #<-- wrong
./mytestfile</nowiki></pre>
./mytestfile</nowiki></pre>
Not exactly what we expected.  What has happened is the shell (Command Line Interface program) has interpreted ''*test*'' and matched it to ''mytestfile''.  ''mytestfile'' is what the shell passed as an argument to the find command.  We need to stop the shell interpreting ''*test*'' before passing it to ''find''.  We do this by putting single quotes (') around the pattern.  
Not exactly what we expected.  What has happened is the shell (Command Line Interface program) has interpreted ''*test*'' and matched it to ''mytestfile''.  ''mytestfile'' is what the shell passed as an argument to the find command.  We need to stop the shell interpreting ''*test*'' before passing it to ''find''.  We do this by putting single quotes (') around the pattern.  
You might have used an unquoted pattern before and it was working, this is because if the pattern doesn't match anything in the current directory the shell leaves the pattern unchanged. Of course it's better not to rely on what might or might not be in the current directory..
You might have used an unquoted pattern before and it was working, this is because if the pattern doesn't match anything in the current directory the shell leaves the pattern unchanged. Of course it's better not to rely on what might or might not be in the current directory..
== Searching Files With A Specific Size ==
== Searching Files With A Specific Size ==
This parts needs expansion.
This parts needs expansion.
*-size: the size of the file such as : 600M for a 600 megabytes file. You can also specify a minimum size by adding a +  in front of the size. Like so: +600M will find files of 600 or more megabytes.
*-size: the size of the file such as : 600M for a 600 megabytes file. You can also specify a minimum size by adding a +  in front of the size. Like so: +600M will find files of 600 or more megabytes.
== Acting On The files ==
== Acting On The files ==
This parts needs expansion.
This parts needs expansion.
*-exec: This is used to execute a command to the filenames found. Use {} to substitute the filename and \; to end the command Ex: find ./ -name test.txt -exec ls -l {} \;
*-exec: This is used to execute a command to the filenames found. Use {} to substitute the filename and \; to end the command Ex: find ./ -name test.txt -exec ls -l {} \;
== Advanced Usage ==
== Advanced Usage ==
You can combine several Expressions, for instance if you want to find the files whose names  contains both "dylan" and ogg you can do:
You can combine several Expressions, for instance if you want to find the files whose names  contains both "dylan" and ogg you can do:
<pre><nowiki>
<pre><nowiki>
第82行: 第51行:
<pre><nowiki>
<pre><nowiki>
find . -name '*dylan*' -o -name '*elvis*'</nowiki></pre>  
find . -name '*dylan*' -o -name '*elvis*'</nowiki></pre>  
----
----
[[category:CategoryNeedsExpansion]] [[category:CategoryDocumentation]]
[[category:CategoryNeedsExpansion]] [[category:CategoryDocumentation]]


[[category:UbuntuHelp]]
[[category:UbuntuHelp]]

2007年11月30日 (五) 17:14的版本

{{#ifexist: :Find/zh | | {{#ifexist: Find/zh | | {{#ifeq: {{#titleparts:Find|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:Find|1|-1|}} | zh | | }}

What Is find?

The GNU find command search files within within a directory and its subdirectories according to several criteria such as name, size and time of last read/write. It performs actions on the files found, by default it prints the name of the files. The GNU find command is installed on every Ubuntu system. This page is complemented by the manual('man') page for find which can be read by issuing the command "man find". (See Self:BasicCommands for a quick introduction to the command line)

The Basics

The general syntax for using finds is:

find [-H] [-L] [-P] [path...] [expression]

We will ignore the options [-H] [-L] [-P] for now , please refer to man find to learn their meaning, and start with the [path...]. find search a directory and its subdirectories, before any expression you should specify the directory or the directories where find will start searching

find . # search within the  current directory
find /home/user1 /home/user2   #search within the directories home/user1 /home/user2
find # same as find . , by default GNU find uses  the current directory if no path is specified

The above commands will print the names of all the files present in the hierarchy below the starting directories passed to find as arguments. "files" can be link, directories, hidden files ...

Searching Files With A Specific Name

Of course, find would just be a poor version of ls without the [expression] part, let's start by the most common and perhaps most simple example:

find dir -name 'myfile' 

The test -name 'name' will only search for an exact match of the name. This means that the above command will find, in the directory dir, a file named "myfile" but not a file named "myfile.txt" or "thisismyfile". If you are looking for a file with "myfile" in the name somewhere, you should instead use the following:

find dir -name '*myfile*' 

Here, the * is a wildcard, and can stand for any number of characters (number, letter, space...). It is a basic form of a pattern. See the find manual page for more on patterns. The most important thing here is: Do not forget to put quotes around your pattern. If the pattern is not quoted, it will be replace by the shell by the list of files matching the pattern in the current directory. For example, if your current directory contains the file mytestfile and the directory myfiles, and the myfiles directory that has the file test.txt.

$ ls
myfiles  mytestfile
$ ls myfiles/
test.txt

and we run the above find command

$ find . -name *test* #<-- wrong
./mytestfile

Not exactly what we expected. What has happened is the shell (Command Line Interface program) has interpreted *test* and matched it to mytestfile. mytestfile is what the shell passed as an argument to the find command. We need to stop the shell interpreting *test* before passing it to find. We do this by putting single quotes (') around the pattern. You might have used an unquoted pattern before and it was working, this is because if the pattern doesn't match anything in the current directory the shell leaves the pattern unchanged. Of course it's better not to rely on what might or might not be in the current directory..

Searching Files With A Specific Size

This parts needs expansion.

  • -size: the size of the file such as : 600M for a 600 megabytes file. You can also specify a minimum size by adding a + in front of the size. Like so: +600M will find files of 600 or more megabytes.

Acting On The files

This parts needs expansion.

  • -exec: This is used to execute a command to the filenames found. Use {} to substitute the filename and \; to end the command Ex: find ./ -name test.txt -exec ls -l {} \;

Advanced Usage

You can combine several Expressions, for instance if you want to find the files whose names contains both "dylan" and ogg you can do:

find . -name '*dylan*' -name '*ogg*'

There is an implicit and between the arguments. If you want to find the files whose names contains "dylan" or "elvis" you can do:

find . -name '*dylan*' -o -name '*elvis*'