The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
 

 File Search in Linux

File Search
       type : Display command type
       which : Find executable files
       whereis : Find files in default paths
       llocate : Search through on the hard drive
           updatedb : Update the database of indexed files
       find : The ultimate file search tool

ENG⇒中ENG⇒中
   File Search
One of the design philosophies of Linux is "everything is a file," so files within Linux's filesystem are scattered like stars. Therefore, file search becomes particularly important. Fortunately, Linux provides five powerful file search commands (type, which, whereis, locate, find), and their file search capabilities are comparable to the "great Google".

type : Display command type
Strictly speaking, type is not used for file search. It is a built-in command in the shell used to display the type of a command (such as built-in or executable). However, an unexpected side effect is that it is very useful and simple for finding executable files (commands). The usage is as follows:

Syntax:type [-otpiton] COMMAND
Command name/Function/Command use Options Functions
type/
display instruction type/Any
-a Show all possible types of commands
-t Display the type of command (alias/file or builtin)
-P Find instructions according to PATH (similar towhich)

The advantage of the type command is its simplicity, but the disadvantage is that it can only search for files built-in to the shell or based on the environment variable $PATH [note] (although it does not necessarily find all types of files, as non-executable files can be found if they are placed in a directory listed in the $PATH variable). Files that are not included in the $PATH variable will not be found, and it also cannot be used with wildcards for searching.

Example:
$ type -a pwd ←Displays the location of the command "pwd"
pwd is a shell builtin ←The first result shows that "pwd" is a shell builtin, meaning it is built into the shell
pwd is /bin/pwd ←The second result shows that "pwd" is located at "/bin/pwd"
$ type type ←Displays the location of the command "type"
type is a shell builtin ←shell built-in
$ type -a ls ←Searches for the location of the command "ls"
ls is aliased to `ls --color=tty' ←The first result shows that "ls" is an alias for "ls --color=tty," meaning it is a shorthand or alternative form of the command
ls is /bin/ls ←The second result shows that "ls" is located at "/bin/ls"
$ type shadow ←Searches for the file named "shadow"
bash: type: shadow: not found ←ttype is generally used to find executable files, or files placed in the environment variable PATH, otherwise it will not be found

^ back on top ^

which : Find executable files
The which command can only search for executable files or aliases within the $PATH environment variable. It does not have the ability to search for shell builtins, making it the weakest among the file search commands. It can be considered as a sickly cat with limited functionality, similar to type -fP.

.Example: ( use the example given by type to do it again)
$ which pwd ←Displays the location of the command "pwd"
/bin/pwd
$ which type ←Find the command "type"
/usr/bin/which: no cd in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/
usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin) ← The "which" command cannot find "type" within the $PATH environment variable. It does not have the ability to search for shell builtins
$ which ls ←Searches for the command "ls"
alias ls='ls --color=tty' ← alias ls='ls --color=tty' (Executing "ls" actually runs "ls --color=tty")
        /bin/ls ←Location of "ls"
$ which shadow ←Searches for the file named "shadow"
/usr/bin/which: no cd in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/
usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin) ←is not found within the $PATH environment variable

^ back on top ^

whereis : Find files in default paths
The whereis command is primarily used to locate executable files, source code, and manual page (manpage) documents. The search paths can vary depending on the options used. For example, using whereis -b will search for files in directories such as /bin, /sbin, /usr/bin, which are common locations for storing executable files.

The usage of"whereis is as follows:
Syntax:whereis [-otpiton] COMMAND
Command name/Function/Command use Options Function
whereis/
(where is file)
default path to find files/
Any
-b Searches for the location of the binary/executable file (/bin, /sbin, /usr/bin, etc.)
-B Specify search executable path
-l (lowercase L) list search paths
-m Searches for the location of the manpage document(/usr/share/man etc.)
-M Specifies the path to search for help files
-s Searches for the location of the source code file.(/usr/src etc.)
-S Specifies the source code search path

Example:
$ whereis -l ←query the paths where "whereis" searches for executable files, manual pages, and source code
bin: /usr/bin ←The beginning of "bin:" is the path to find executable files
bin: /usr/sbin
bin: /usr/lib
bin: /etc
bin: /usr/etc
(Other paths may be listed in between)
man: /usr/share/man/pl ←"man:" at the beginning is the path to find the man page
man: /usr/share/man/man7
(Other paths may be listed in between)
src: /usr/src/kernels ← "src:" at the beginning is the path to find the source code
src: /usr/src/debug


Example:
$ whereis -m zip ←Search "zip" man page file
zip: /usr/share/man/man1/zip.1.gz
$ whereis -b zip ←Search "zip" executable file
zip: /usr/bin/zip
$ whereis -l | grep 'src:' ←List the path to search source code
src: /usr/src/kernels
src: /usr/src/debug

^ back on top ^

locate : Search through on the hard driv
Because there are too many files in the Linux filesystem, the commands type, which, and whereis only search their respective default paths in order to speed up the search process. If a file is located in the home directory ("/home/xxx") or a custom directory that is not included in the default search paths of these commands, it may not be found. On the other hand, the locate command uses the index database located at "/var/lib/mlocate/mlocate.db" to search for files. It has a wider search scope than the default search paths of the aforementioned commands.


Now that we have covered the basic knowledge of locate, let's move on to explaining its syntax.
Syntax:locate [-otpiton] file/directroy
Command name/Function/Command user Options Function
locate/
hard disk index search/
Any
-b Only list files that match the "base name" and can be searched with wildcards
-n # List up to # results ("#" is a number)
-r Search using regular notations
-i Ignore case
-d Specify the index database

In general, when searching for the string "FILE_NAME" with locate, it will search for "*FILE_NAME*" by adding wildcard characters "*" before and after the file name string to expand the search scope. The part where "FILE_NAME" is listed can be a file name, path, or directory.

Example:
$ locate 586 ←← Search for the file string "586"
locate: can not open `/var/lib/mlocate/mlocate.db': No such file or directory
↑ If "mlocate.db" is not found, it may be because the "updatedb" command has not been executed to generate the index database.
$ su - root ←Switch to "root" to manually update "updatedb"
Password:
]# updatedb ←(It will take some time to generate the index database)
# locate 586 ←Search for "586" again
/lib/modules/2.6.23.1-42.fc8/kernel/arch/i386/crypto/aes-i586.ko
/usr/lib/perl5/5.8.8/pod/perl586delta.pod
/usr/lib/rpm/i586-linux/macros ← The path contains "586" as well
/usr/share/man/man1/perl5866delta.1.gz

To exclude results that are directory names, you can use locate -b to only list files or directories that match the "base name" . What is the "base name"? Taking the file "/var/lib/mlocate/mlocate.db" as an example, after excluding the path, the rightmost part "mlocate.db" is the base name.

So, in the previous example, when running locate -b 586 instead of locate 586, the line "/usr/lib/rpm/i586-linux/macros" will not be listed. Using locate -b to only list files or directories that match the "base name" is also useful when combined with wildcard characters for file or directory searching.

Example:
$ locate -b apple?? ←Using the "base name" with wildcard characters to search for the string "apple"
/home/aaa/.gconf/apps/panel/applets
/usr/share/terminfo/a/apple2e
/usr/share/terminfo/a/appleII

Here are some other examples of options:
$ locate -b t[!o-p]e ← Using the "base name" with wildcard characters for searching
$ locate -i x11 ← Ignoring case sensitivity
$ locate -n 5 poweroff ←Displaying a maximum of 5 results
$ locate -d /var/lib/mlocate/office_hd.db file ←Specifying the index database
$ locate -r 'x\{3\}' ←Using regular expressions to search for three consecutive "x" in file names or paths

^ back on top ^



  find : The ultimate file search tool
The ultimate file search tool is undoubtedly the find command. Besides the file name, it can search for any characteristics of a file, such as size, time, permissions, ownership, file type, and so on. Its philosophy is: "There's no file that find can't locate, unless you don't know how to use it."

For example, if your hard drive is almost full and you want to find the top 5 largest files occupying space, you can use the following command to uncover the culprits hogging your disk:
find / -type f -exec du {} ; 2>&- | sort -n | tail -n 5
.

Because of the powerful functionality of find, it can be a bit complex to use. Additionally, it doesn't have a default search path or use an indexed database. Instead, it actually scans through the files on the hard drive one by one, which can be time-consuming.

The basic usage of find is find PATH -name 'PATTERN' If you omit the PATH, it searches the current working directory (including subdirectories) by default.

For example:
$ find / -name 'apple??'← Starting from the root directory, search for files with the pattern "apple??"
$ find / -name 'apple??' 2> /dev/null ← Same as above, but filters out error output (commonly used))
/usr/src/kernels/3.10.0-693.el7.x86_64/include/config/hid/apple.h
/usr/src/kernels/3.10.0-693.el7.x86_64/include/config/backlight/apple.h
$ find /etc /usr -name "read*" ←You can search multiple paths

If you're a regular user, you might not have sufficient permissions to enter every directory layer for searching, resulting in error outputs cluttering the screen. In such cases, you can combine it with redirection to STDERR, like 2> /dev/null to filter out the error output. Alternatively, you can disable the stderr file descriptor by writing find / -name 'apple??' 2>&- to make the output cleaner.

Here are some advanced usages of find:
Syntax:find [path] [-otpiton][--option] expression
Command name/Function/Command user Options Function Note
find/
Ultimate file search/
Any
By file name or directory or filesystem
-name "PATTERN" Pattern filename search PATTEN can be used with wildcard templates, but only the base-name can be found, that is, the path of the file to be removed
-iname "PATTERN" Same as -name but case insensitive  
-regex "PATTERN" Regular Expressions Search PATTEN supports regular expressions
-regextype is used to change the supported type of regular expressions Options are:
"emacs" (default), "posix-basic", "posix-egrep", "posix-extended", "awk", "grep, "egrep" , etc.
-iregex Same as -regex but case insensitive  
-path "PATTERN" Path Template Search It is similar to the option "-name", but the option "-name" cannot match the "/" or "./" of the path symbol correctly, which can be overcome by using this option
-ipath "PATTERN" Same as -path but case insensitive  
-prune Exclude files in directories specified by -path or -ipath To be used with the option "-path"
-maxdepth # Maximum search # levels of directories (# is a number) If #=1, subdirectories will not be searched
-mindepth # Search at least # levels of directories (# is a number) If #=1, any level of directory will be searched
-fstype FILESYSTEM Specify the filesystem Common filesystem types include:
  • Linux filesystems: ext2, ext3, ext4, reiserFS
  • DOS filesystem: vfat
  • Windows filesystem: ntfs (fuseblk)
  • CD/DVD-ROM filesystem: iso9660
  • DVD-ROM filesystem: udf
  • APPLE filesystem: hfs
  • Network filesystem: nfs, and so on.
-xdev Search only the current filesystem For example, if the root directory is mounted with different filesystems, using this option will restrict the search to only the filesystem that the root directory belongs to.
-mount Same as "-xdev"  
By file size
-size [+][-]#
[bckMG]
Search by file size, the available items are
〝b〞:Block occupied by the file, Bolock=512B
〝c〞: byte
〝k〞:1024 byte
〝M〞: 10242 byte
〝G〞: 10243 byte

-empty Search for files of size 0 or empty directories  
By file type
-type [bcdpfls] The file types has the following items:
"b": block device
"c": character device
"d": directory
"p": named pipe
"f ":regular file
"l": symbolic link
"s":socket
 
-links [+][-]# Find hard link file # is the number of links
-follow Exclude symbolic link files  
By permissions or ownership
-perm[+][-]# Search by Numeric mode  
-user OWNER_NAME Search for files with a specified owner  
-group GROUP_NAME Search files for a specific group  
-uid # Search the UID of the file  
-gid # Group GID to search for files  
-nouser Search for files with invalid owner  
-nogroup Search files for invalid groups  
y time
-atime[+][-]# Search for files based on their atime # unit is day
-ctime[+][-]# Search for files based on their ctime # unit is day
-mtime[+][-]# Search for files based on their mtime # unit is day
-amin Search for files based on their atime # unit is minutes
-cmin Search for files based on their ctime # unit is minutes
-mmim Search for files based on their mtime # unit is minutes
-anewer ref_file Search for files that have a newer atime than the reference file.  
-cnewer ref_file Search for files that have a newer ctime than the reference file.  
-newer ref_file Search for files that have a newer mtime than the reference file.  
-daystart is used to search for files based on the number of days starting from the beginning of today.

It ensures that the search considers the start of the current day as the reference point for counting days
The unit is day, and it needs to cooperate with options such as -amin, -atime, -cmin, -ctime, -mmin, -mtime
Control/Execution/Output/Others
-print Output to stdout (default))  
-exec COMMAND {} \; Passes the search results to the subsequent command  
-ok COMMAND {} \; Similar to -exec, but prompts for confirmation before executing the command  
-delete Deletes the files directly that are found during the search  
-a(and) ,-o(or) -not(!)


To perform logical filtering while searching for files, you can use the following options:

  • "-a" (and) is equivalent to "&&" (returns true if the exit code is 0).
  • "-o" (or) is equivalent to "||" (returns true if either condition is true).
  • "-not" (!) is used for logical negation (reverses the condition).
   

The find command has more complex usage scenarios. Here are some examples and explanations. It is important to note the usage of the "+" and "-" symbols:

When used with file size comparisons:

When used with the "-perm" option:

^ back on top ^




    [註]在文字界面下輸入 echo $PATH 可查詢環境變數 PATH 在那些路徑下搜索執行檔。