Reference URL: http://www.runoob.com/linux/linux-command-manual.html
which command:
The which command is used to find and display the absolute path of a given command, and the environment variable path holds the directory that needs to be traversed when the find command is saved. The which directive looks for eligible files in the directory where the environment variable $path is set. That is, with the which command, you can see whether a system command exists, and the command that executes exactly which location.
Grammar
which (option) (parameter)
Options
-n< filename Length: The length of the file name, the length specified must be greater than or equal to the longest file name in all files;-p< filename length;: The same as the-n parameter, but here the < filename length > contains the path of the file; W: Specifies the width of the field at the output;-V: Displays version information.
Parameters
Instruction name: List of command names.
Instance
Find file, show command path:
Pwd/bin/pwd[[email protected] ~]# which adduser/usr/sbin/adduser
Description: The which is based on the user's configured directory within the PATH variable to search for a running file! Therefore, the different PATH configuration content found the command of course not the same!
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------
Linux Whereis Commands
Linux Command Encyclopedia
The Linux whereis command is used to locate files.
The directive looks for eligible files in a specific directory. These files should belong to the original code, binary files, or Help files.
This directive can only be used to find binaries, source code files, and man manual pages, and general file positioning requires the use of the locate command.
Grammar
[-bfmsu][-< catalogue ...] [-< catalogue ...] [-< catalogue ...] [File ...]
Parameters :
- -B finds only binary files.
- -b< directory > Find binaries only in Settings directory.
- -F does not display the path name before the file name.
- -M finds only the description file.
- -m< directory > Find the description file only in the directory you set.
- -S finds only the original code file.
- -s< directory > Find the original code file only in the directory you set.
- -U finds files that do not contain the specified type.
Instance
Using the instruction "Whereis" to view the location of the command "bash", enter the following command:
After the above instruction executes, the output information is as follows:
Bash:/bin/bash/etc/bash. BASHRC/usr/share/man/man1/bash. 1.gz
Note: The above output information is the query's program name, bash Path, and Bash's Man manual page path, respectively, from left to right.
If the user needs to query the binaries or Help files separately, use the following command:
--
The output information is as follows:
$ whereis-B BashBinary Program for #显示bash commandsBash: /bin/bash /etc/bash./usr/share/< Span class= "PLN" >bash # the address of the binary program of the bash command $ whereis -m bash #显示bash command bash: /usr/share/man/ man1/bash. 1.gz #bash命令的帮助文件地址
--------------------------------------------------------------------------------------------------
1. Introduction to Commands
Locate (locate) command to locate a file or directory. The locate command is much faster than find-name because it does not search for a specific directory, but instead searches for a database/var/lib/mlocate/mlocate.db. This database contains all the local file information. The Linux system automatically creates this database and updates it automatically once a day, so when we use Whereis and locate to find files, we sometimes find the data that has been deleted, or just build the file, but we can't find it because the database file was not updated. To avoid this situation, you can manually update the database by using the UpdateDB command before using locate. The whole locate work is actually made up of four parts:
/usr/bin/updatedb is mainly used to update the database, through the crontab auto-completed
/usr/bin/locate Querying file Locations
/etc/updatedb.conf configuration files for UpdateDB
/var/lib/mlocate/mlocate.db files that store file information
2. Usage
Locate [OPTION] ... [PATTERN] ...
3. Options
-B,--basename match only the base name of the path names-c, and--count only outputs the number of found-D,--database DBPATH makes The database specified with DBPath, not the default database/var/lib/mlocate/mlocate.db-e,--existing only print entries for currently existing files -L,--follow follow trailing symbolic links when checking file existence (default)-H,--help display Help Help-I,--ignore-case ignore case-L,--limit,-N Limit limit output (or counting) to limit entries-m,--mmap Ignored, for backward compatibility-p,--nofollow,-H don ' t follow trailing symbolic links when checking file Exis tence-0,--null separate entries with NUL on output-s,--statistics don ' t search for entries, print Statistics about eachused database-q,--quiet quiet mode, will not display any error messages-R,--regexp regexp use basic regular Expressions--regex Use extended Regular expression-S,--stdio ignored, for backward compatibility-v,--version display version information-W,--who Lename Match WholE path name (default)
4. Example 1: Search all files in the ETC directory that start with my
[Email protected] lib]# LOCATE/ETC/MY/ETC/MY.CNF
Example 2: New files cannot be locate, using updatedb
[[email protected] ~]# Touch New.txt[[email protected] ~]# locate New.txt[[email protected] ~]# Updatedb[[email protected] ~]# Locate New.txt/root/new.txt
Example 3:updatedb configuration file/etc/updatedb.conf
[[email protected] ~]# cat/etc/updatedb.conf prune_bind_mounts = "Yes" prunefs = "9p AFS anon_inodefs auto AutoFS Bdev bin Fmt_misc cgroup cifs coda Configfs cpuset debugfs devpts ecryptfs exofs fuse fusectl GFs gfs2 hugetlbfs inotifyfs iso9660 JFFS2 Lustre Mqueue Ncpfs NFS nfs4 nfsd pipefs proc Ramfs rootfs rpc_pipefs securityfs selinuxfs SFS sockfs SYSFS TMPFS UB IFS udf usbfs "prunenames =". Git. Hg. svn "prunepaths ="/afs/media/net/sfs/tmp/udev/var/cache/ccache/var/spool/cups /var/spool/squid/var/tmp "
The first line prune_bind_mounts= "yes" means: whether to restrict the search.
The second line excludes the file system types that are retrieved, that is, the file system types listed are not retrieved.
The second line indicates which suffixes are excluded from the retrieval of the file, that is, the file that is listed in the suffix of this page is skipped. The different suffixes are separated by a space.
The four rows are excluded from the retrieved path, that is, the files and subfolders under the listed path are skipped and not retrieved. updatedb After using locate still can't find the desired file
You can check if the mounted directory is ignored.
-------------------------------------------------------------------------------------------
Find is a command that uses a higher frequency. It is often used in a system-specific directory to find files that have some kind of feature.
Format of the Find command: Find [-path ...]-options [-print-exec-ok]
Path: The directory path to find.
~ Represents the $home directory
. Represents the current directory
/Represents the root directory
Print: Indicates that the result is output to standard output.
EXEC: Executes the shell command given by the parameter to the matching file.
The form is command {} \;, note {} and \; There is a space between
OK: As with the EXEC function,
The difference is that a prompt is given before the command is executed to let the user confirm whether the
Options are commonly used with the following option:
-name: Search by name
-perm: Install permissions Lookup
-prune: No longer found under the currently specified directory
-user: File owner to find
-group: File belongs to group to find
-nogroup: Finding files with no valid owning group
-nouser: Finding files with no valid owner
-type: Search by file type
Here are some simple examples to illustrate the general use of the next find:
1. Search by name
In the current directory and subdirectories, find the TXT file at the beginning of the capital letter
$ find. -name ' [A-z]*.txt '-print
In/etc and its subdirectories, look for files at the beginning of host
$ Find/etc-name ' host* '-print
In the $home directory and its subdirectories, find all Files
$ find ~-name ' * '-print
In the current directory and subdirectories, find the TXT file that is not the beginning of the out
$ find. -name "out*"-prune-o-name "*.txt"-print
2. Search by directory
Search for txt files in subdirectories other than AA in the current directory
$ find. -path "./aa"-prune-o-name "*.txt"-print
Find txt files in the current directory and subdirectories other than AA and BB
$ find. \ (-path "./aa"-o-path "./bb" \)-prune-o-name "*.txt"-print
In the current directory, no longer subdirectories, find txt files
$ find. ! -name "."-type d-prune-o-type f-name "*.txt"-print
3. Search by permissions
In the current directory and subdirectories, look for the master with read and write execution, and other files with reading Execute permission
$ find. -perm 755-print
4. Search by Type
In the current directory and subdirectories, locate the symbolic link file
$ find. -type L-print
5. According to the owner and genus Group
Find files that belong to www
$ find/-user Www-type F-print
Find files that belong to the main deleted
$ find/-nouser-type F-print
Find files belonging to the group MySQL
$ find/-group Mysql-type F-print
Find files deleted by user group
$ find/-nogroup-type F-print
6. Search by Time
Find files that have been changed in 2 days
$ find. -mtime-2-type F-print
Find files that have been changed 2 days ago
$ find. -mtime +2-type F-print
Find files that are accessed in a day
$ find. -atime-1-type F-print
Find files that were accessed a day ago
$ find. -atime +1-type F-print
Find files that are changed in the day
$ find. -ctime-1-type F-print
Find a day ago state changed file
$ find. -ctime +1-type F-print
Find files with status changed 10 minutes ago
$ find. -cmin +10-type F-print
7, according to the old and new documents
Find a new file than Aa.txt
$ find. -newer "Aa.txt"-type f-print
Look for older files than Aa.txt
$ find. ! -newer "Aa.txt"-type f-print
Find newer than aa.txt, older than bb.txt files
$ find. -newer ' Aa.txt '! -newer ' Bb.txt '-type f-print
8. Search by size
Find files that are over 1M
$ find/-size +1m-type F-print
Find files that are equal to 6 bytes
$ find. -size 6c-print
Find files that are less than 32k
$ find. -size-32k-print
9. Execution of Orders
Find Del.txt and delete, prompt confirmation before deleting
$ find. -name ' Del.txt '-ok rm {} \;
Find Aa.txt and back up as Aa.txt.bak
$ find. -name ' Aa.txt '-exec cp {} {}.bak \;
Creatively use Find
You can use find
commands to perform a variety of tasks. This section provides find
examples of several ways to use commands to manage file systems.
To keep it simple, these examples avoid the use of a command that involves passing the output of one command through a pipeline to another command -exec
. However, you can -exec
use such a command in the clause of the find command exactly as you want.
Clear Temporary files
You can find
save disk space by using commands to clear temporary files that are generated during normal use in a directory or subdirectory. To do this, use the following command:
$ find. \ (-name a.out-o-name ' *.o '-o-name ' core ' \)-exec rm {} \; |
The file masks in parentheses identify the types of files that need to be deleted, and each file mask is preceded by a -name.
list that expands to include any temporary file types in the system that you need to purge. During the compilation and connection of the Code, the programmer and its tools generate the file types in the example: a.out, *.o
and core.
other users typically generate similar temporary files, which can be edited using such *.tmp, *.junk
file masks as the corresponding ones. You may also find it useful to put commands into a clean
script called scripts that can be executed when you need to clear the contents of a directory.
Copy content from a directory
find
Commands allow you to copy everything in a directory while maintaining permissions, time, and ownership for each file and subdirectory. To do this, you need to combine the use find
and the cpio
commands as follows:
Listing 2. Use the Find and Cpio commands together
$ cd/path/to/source/dir$ Find. | Cpio-pdumv/path/to/destination/dir |
cpio命令
is a copy command designed to copy or copy files to or from a cpio or tar archive file and automatically maintain permissions, time, and ownership of files and subdirectories.
List the first line of text files
Some people use the first line of each text file as a caption or as a description of the file's contents. You can make it easier to filter a large number of text files by using a report that lists the file name and the first row of all text files. The following command lists the first line of all the text files in your home directory in the report, making it easier to use less
commands for inspection as needed:
Listing 3. Less command
$ find $HOME/. -name *.txt-exec head-n 1-v {} \; > report.txt$ Less < Report.txt |
Maintain storage space for LOG and TMP files
To maintain the LOG and TMP file storage space for applications that generate a large number of files, you can put the following commands into a task that runs every day cron
:
Listing 4. Maintain LOG and TMP file storage space
$ find $LOGDIR-type d-mtime +0-exec compress-r {} \;$ find $LOGDIR-type d-mtime +5-exec rm-f {} \; |
The first command finds all directories in the $LOGDIR directory that contain data modified within 24 hours (-mtime +0)
(-type D), compressing them (compress -r {})
to conserve disk space. If these directories exceed one work week (-mtime +5)
, the second command deletes them (rm -f {})
to increase the free space on the disk. In this way, the Cron task automatically maintains the directory based on the time window you specify.
Duplicate a complex directory tree
If you want to copy a complex tree from one computer to another, while maintaining the copy permissions and the user ID and Group ID (the UID and the value of ownership of the tag file used by the gid--operating system), and do not copy the users files, you will need to reuse find
andcpio;
Listing 5. Duplicate a complex directory tree
$ cd/source/directory$ Find. -depth-print | Cpio-o-o/target/directory |
Find links that don't point anywhere
To find links that do not point anywhere, you can use find
the band perl
interpreter as follows:
$ find/-type L-print | Perl-nle '-e | | print '; |
This command starts at the top-level directory (/) and lists perl
all links that are determined by the interpreter not to point anywhere, and for (-nle ‘-e || print‘)
(-type l -print)
More detailed information about this technique in the Unix Guru Universe site, see the Resources section. If you want to delete these files, you can further use the pipeline to pass the output to the rm -f {}
command. Of course, Perl can be found in most of the UNIX toolkit, which is one of many powerful explanatory language tools.
Locate and rename a directory that cannot be printed
In UNIX, you may have an error or malicious program that creates a directory that contains characters that cannot be printed. Locating and renaming these directories makes it easier to check and delete them. To do this, you first need to ls
include a -i
switch in to get the numeric value of the index node for that directory. Then, use the value of the find
index node to convert to a file name that can be renamed with a mv
command:
Listing 6. Locate and rename a directory that cannot be printed
$ ls-ail$ Find. -inum 211028-exec mv {} newname.dir \; |
List files with a length of zero
To list all files of zero length, use the following command:
$ find. -empty-exec ls {} \; |
After you find an empty file, you can choose to use rm
commands instead ls
of commands to delete the files.
Search for directories and files under Linux