个人工具

“UbuntuHelp:Find”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第4行: 第4行:
 
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:[[UbuntuHelp:BasicCommands|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:

2007年12月5日 (三) 12:06的版本


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*'