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) |
$ 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 |
$ 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 |
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 |
$ 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 |
$ 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 |
The locate command searches for files based on the index database located at "/var/lib/mlocate/mlocate.db". However, it does not create this database file itself. The database is generated by another command called updatedb (update database).
However, using locate and updatedb together is not without drawbacks. By default, the system may execute the updatedb command only once a day, which means that locate may not find files that have been modified during that time. Fortunately, you can manually update the index database by running the updatedb command (typically only the root user can execute updatedb).
Therefore, the characteristics of locate are as follows:
Additionally, the index database created by the updatedb command follows the specifications in the configuration file "/etc/updatedb.conf". Here is a brief explanation:
# cat /etc/updatedb.conf PRUNE_BIND_MOUNTS = ←"yes" to enable these rules, "no" to disable PRUNEFS = ← Filesystems to exclude from the index database (by default, rare and CD-ROM filesystems are excluded) PRUNENAMES = ← Files whose names contain these keywords will not be searched PRUNEPATHS = ← Directories excluded from indexing and updating the database |
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 |
$ 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 |
$ 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 |
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:
|
|
-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 | |||
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(!) |
|
$ cd / $ find -path './et*et' 2>&- ← Search for files or directories matching the path pattern './etet' within the current working directory (root directory in this case) ./etc/issue.net ./etc/alternatives/servlet ./etc/auto.net $ find / -path '/et*et' 2>&- ←Same as above, but specifies the starting path for the search ./etc/issue.net ./etc/alternatives/servlet ./etc/auto.net |
# find /usr -path '/usr/lib' -a -prune -o -name 'applet.*' ↑ Search for files matching the name pattern 'applet.*' within the '/usr' directory, excluding the '/usr/lib' directory /usr/share/pygtk/2.0/defs/applet.defs /usr/share/nm-applet/applet.glade /usr/share/system-config-printer/applet.pyc /usr/share/system-config-printer/applet.png /usr/share/system-config-printer/applet.py /usr/share/system-config-printer/applet.glade /usr/share/system-config-printer/applet.pyo |
At first glance, this example may seem complicated and unclear. Let's break it down to understand it better. The "-a" symbol represents the logical AND operator (similar to "&&"), and the "-o" symbol represents the logical OR operator (similar to "||"). Therefore, if the execution result of the command find /usr -path '/usr/lib' is correct (meaning there is a '/usr' directory), the option "-prune" will be executed. Otherwise, the option "-name" will be executed.
In the find command, if two expressions are used together, "-a" is the default value and can be omitted. Hence, in the example above, "-a" can be omitted.
When using regular expressions with find -regex, the matching rules are similar to those used with find -path. You can include the path with "/" or "./".
Example:# find -regex './.*\.txt' ←Search for files with the extension ".txt" in the current working directory # find / -regex '.*/bin' ←Search for "/bin" starting from the root directory # find / -regex '.*apple.*' ← Search for files with the string "apple" starting from the root directory # find / -regextype posix-extended -regex '.*ap+le.*' ←Specify extended regular expression typ |
$ find /etc \( -name '*.conf' -o -name '*.config' \) ↑ Search for files with the extensions ".conf" or ".config" in the directory "/etc". To use parentheses within the command, escape them with the backslash character "\" $ find / -mindepth 7 -name "*ee" ←Search for files matching the pattern "*ee" with a directory depth of at least 7 $ find / -name "myfile" -fstype vfat ←Search for files named "myfile" only in the DOS FAT filesystem $ find / -name "myfile" ! -fstype fuseblk ←Exclude files in the Windows NTFS (fuseblk) filesystem from the search for files named "myfile" $ find ./ ! -name "*.thml" ←Exclude files in the current working directory that do not have the extension ".html" $ find -not -name "*.thml" ←Same as the above example "!" can also be written as "-not" $ find -name *.jpg -delete ←Delete the files that match the pattern "*.jpg" in the current working directory |
$ find /etc -size 2b -exec ls -l {} \; Lists files in the "/etc" directory with a size of two blocks (512 bytes to 1024 bytes) and executes the "ls -l" command on each file $ find ~ -size 1024c ←Searches for files in the home directory with a size of 1024 bytes $ find ./ -size -2k ←Searches for files in the current working directory with a size less than 2 kibibytes $ find my_dir/ -size +1G ←Searches for files in the "my_dir" directory with a size greater than 1 gibibyte $ find -empty ←Searches for empty directories or files with a size of 0 in the current working directory |
$ find / -empty ! -type f ←Searches for empty directories in the root directory ("/"), excluding files. $ find / -links 6 -not -type d ←Searches for files with a hard link count of 6 in the root directory ("/"), excluding directories $ find / -type d -name "man1" ←Searches for directories with the name "man1" in the root directory ("/") |
$ find /tmp -user aaa ← Searches for files in the "/tmp" directory owned by the user "aaa" $ find /tmp -group root ← Searches for files in the "/tmp" directory belonging to the group "root" $ find /home /tmp -nouser ← Searches for files that do not belong to any user in the "/home" and "/tmp" directories. This is commonly used to find files left on the disk from deleted user accounts $ find /home -uid 501 ←Searches for files in the "/home" directory with the UID (user ID) of 501 |
$ find / -perm 0444 -exec ls -l {} \; ←Searches for files with permissions "0444" (readable by all) in the root directory ("/") and executes the "ls -l" command on each file $ find / -perm -0444 ← Searches for files with permissions "0444" in the root directory ("/") using the logical AND operation (all conditions must be met) $ find / -perm +0444 ←Searches for files with permissions "0444" in the root directory ("/") using the logical OR operation (at least one condition must be met) |
〝rw- r-- rw-〞 〝r-x r-x r--〞 〝rwx r-x rwx〞 〝r-x r-- r--〞In other words, within the given scope, all three parts highlighted in red must match (while the non-red parts are disregarded) in order for a file to be considered a match. On the contrary, in the case of find / -perm +0444, the "+" symbol represents the logical OR operation. This means that if any of the specified conditions are met, the file will be considered a match. Therefore, the resulting files may have permissions as follows:
〝rwx rwx rwr〞 〝r-x --x r--〞 〝rwx --x -wx〞 〝--x r-- ---〞In other words, for the three parts highlighted in red, only one of them needs to match for a file to be considered a match.
$ find ~ -mtime -5 ←Search for files in the home directory that have been modified within the last 5 days (unit is days) $ find ~ -mtime +5 ←Search for files in the home directory that have been modified more than 5 days ago. $ find ~ -mtime 5 ← Search for files in the home directory that were modified exactly 5 days ago |
$ find /tmp -mmin -30 -type f ←earch for files in the "/tmp" directory that have been modified within the last 30 minutes (note that the unit is minutes) $ find ~/ -newer ref_file ←Search for files in the home directory that have a modification time newer than the reference file "ref_file" $ find ~/ -anewer ref_file ← Search for files in the home directory that have an access time newer than the reference file "ref_file" |