Linux File Lookup

Source: Internet
Author: User
Tags pear print print

Find related commands on Linux systems:

    • Which viewing the location of an executable file

    • Whereis viewing the location of a file

    • Locate to view file locations with a database

    • Find actual search hard disk query file name

Whereis

The Whereis command locates the location of the executable file, the source code file, and the Help file in the file system. The properties of these files should belong to the original code, binary files, or Help files. The Whereis program also has the ability to search for source code, specify alternate search paths, and search for unusual items.

Grammar
whereis [-bmsu] 文件或者目录名称
Parameters
    • -B locates the executable file.

    • -M locate the help file.

    • -S Locate the source code file.

    • -U searches the default path for files other than executables, source code files, and Help files.

    • -b Specifies the path to the search executable file.

    • -m specifies the path to the search Help file.

    • -s Specifies the path of the search source code file.

Example

Find the files associated with the passwd file:

[[email protected] ~]# whereis passwdpasswd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz

Only binary files are found:

[[email protected] ~]# whereis -b passwdpasswd: /usr/bin/passwd /etc/passwd

Whereis looks very fast compared to find, because the Linux system records all the files in the system in a single database file, and when you use Whereis and the locate described below, the data is looked up from the database, not like the Find command, By traversing the hard drive to find, the efficiency will naturally be very high. However, the database file is not updated in real time and is updated once a week by default, so when we use Whereis and locate to find files, we sometimes find data that has been deleted, or just created the file, but cannot find it because the database file is not updated.

which

The purpose of the which command is to search for the location of a system command in the path specified by the path variable, and return the first search result. That is, with the which command, you can see whether a system command exists, and the command that executes exactly which location.

Parameters
    • -n Specifies the length of the file name, which must be greater than or equal to the longest file name in all files.

    • -P is the same as the-n parameter, but the path to the file is included here.

    • -W Specifies the width of the field at the output.

    • -V Display version information

Locate

The locate command can quickly find the file when searching the database, the database is updated by the UpdateDB program, UpdateDB is created periodically by Cron Daemon, and the locate command searches the database faster than the entire data from the hard disk. But the worse is locate found the file if recently established or newly renamed, may not be found, in the default value, UpdateDB will run once a day, can be modified crontab to update the set value. Because the Linux system does not have the locate command installed by default, manual installation is required: Yum-y install mlocate.
Locate designated to search for eligible files, it will be stored in the file and directory name of the database, to find matching template style conditions of the file or directory, you can use special characters (such as "*" or "?" And so on) to specify the template style, as specified by Kcpa*ner, locate will find all files or directories that have a starting string of KCPA and end with NER, such as the name Kcpartner if the directory name is Kcpa_ner, all files including subdirectories are listed under that directory.
Locate command and find find files similar function, but locate is through the UPDATEDB program will all the files and directory data on the hard disk first set up an index database, in the execution of LOACTE directly to find the index, query speed will be faster, the index database is generally managed by the operating system, But you can also directly release the UpdateDB force system to immediately modify the index database.

Parameters
    • -E will be excluded from the scope of the search.

    • -1 if it is 1. The security mode is started. In safe mode, the user does not see files that the permissions cannot see. This slows down because the locate must get access to the file in the actual file system.

    • -F excludes specific file systems, for example, we have no reason to put the files in the proc file system in the database.

    • -Q Quiet mode, no error message is displayed.

    • -N displays at most x outputs.

    • -R uses the normal arithmetic to do the searching condition.

    • -o Specifies the name of the data inventory.

    • -d Specifies the path of the repository

    • -H Display Auxiliary message

    • -V Displays the version message of the program

Find

Each operating system is made up of thousands of different kinds of files. There is the system itself with the file, the user's own files, as well as shared files and so on. Sometimes we often forget where a file is placed on the hard drive. In Microsoft's Windows operating system to find a file is quite simple thing, as long as the desktop click on the "Start"-"search" can be in various ways on the local hard disk, LAN, and even on the internet to find a variety of files, documents.
But users who use Linux are less fortunate, and finding a file on Linux is really a tricky thing to do. After all, in Linux we need to use the dedicated "find" command to find files on the hard disk. The file expression format under Linux is very complex, not as easy to find as the Unified aaaaaaa.bbb format under Windows,dos, and in Windows it is easy to find the file name or suffix of the files that you are looking for. The commands for finding files in Linux are usually "find" commands, and the "find" command helps us to find the files we need conveniently in our daily tasks of managing Linux.

Usage
find [OPTIONS]  [查找起始路径]  [查找条件]  [处理动作]

Find the starting path: Specify a specific search target starting path, default to the current directory;
Find criteria: The specified lookup criteria can be based on file name, size, type, affiliation, permissions, and so on, the default is to find all the files under the specified path;
Handling actions: Actions that are made on files that match the search criteria, such as deletions, which are output to standard output by default;

Command options
  • -amin< minutes;: Finds files or directories that have been accessed at a specified time, measured in minutes;

  • -atime<24 hours;: Finds files or directories that have been accessed at a specified time, in 24-hour terms;

  • -cmin< minutes;: Finds files or directories that have been changed at the specified time;

  • -ctime<24 hours;: Finds files or directories that have been changed at a specified time, measured in 24 hours;

  • -depth: Start searching from the deepest subdirectory under the specified directory;

  • -exec< execution instruction;: If the return value of the Find command is true, execute the instruction;

  • -false: Sets the callback value of the Find command to false;

  • -fls< list file: The effect of this parameter is similar to specifying the "-ls" parameter, but saves the result as a specified list file;

  • -follow: Exclude symbolic connections;

  • -fprint< list file: The effect of this parameter is similar to specifying the "-print" parameter, but the result is saved to the specified list file;

  • -fprint0< list file: The effect of this parameter is similar to specifying the "-print0" parameter, but the result is saved to the specified list file;

  • -fprintf< list file >< output format: The effect of this parameter is similar to specifying the "-printf" parameter, but the result is saved to the specified list file;

  • -fstype< file system type;: Only look for files or directories under the file system type;

  • -gid< group identification Code;: Find files or directories that match the specified group ID;

  • -group< Group name: Find a file or directory that matches the specified group name;

  • -ilname< template style;: This parameter has the same effect as specifying the "-lname" parameter, but ignores the difference in the case of the character;

  • -iname< template style;: This parameter has the same effect as specifying the "-name" parameter, but ignores the difference in the case of the character;

  • -inum: Finds a file or directory that matches the specified inode number;

  • -ipath< template style;: This parameter has the same effect as specifying the "-path" parameter, but ignores the difference in the case of the character;

  • -iregex< template style;: This parameter has the same effect as specifying the "-regexe" parameter, but ignores the difference in the case of the character;

  • -links< number of connections;: Find files or directories that match the specified number of hard connections;

  • -lname< template style;: Specifies a string as the template style for searching for symbolic connections;

  • -ls: Assuming that the return value of the Find command is ture, the file or directory name is listed to the standard output;

  • -maxdepth< directory hierarchy;: Set the maximum directory level;

  • -mindepth< directory hierarchy;: Set the minimum directory level;

  • -mmin< minutes;: Finds files or directories that have been changed at a specified time, measured in minutes;

  • -mount: The effect of this parameter is the same as specifying "-xdev";

  • -mtime<24 hours;: Find files or directories that have been changed at a specified time, measured in 24 hours;

  • -name< template style;: Specifies a string as a template style for searching for a file or directory;

  • -newer< reference file or directory;: Find changes time more closely to the current file or directory than the specified file or directory;

  • -nogroup: Find files or directories that do not belong to the local landlord group identification code;

  • -noleaf: Do not consider the directory must have at least two hard connections exist;

  • -nouser: Find files or directories that do not belong to the local host user identification code;

  • -ok< execution instructions; the effect of this parameter is similar to specifying "-exec", but the user is asked before executing the instruction, and if "Y" or "Y" is answered, the execution of the command is discarded;

  • -path< template style;: Specifies the string as the template style for the search directory;

  • -perm< permission value;: Find the file or directory that matches the specified permission value;

  • -print: Assuming that the return value of the Find directive is ture, the file or directory name is listed to standard output. The format is a name for each column, with a "./" string before each name;

  • -print0: Assuming that the return value of the Find directive is ture, the file or directory name is listed to standard output. All names are in the same line;

  • -printf< output format: Assuming that the return value of the Find directive is ture, the file or directory name is listed to standard output. The format can be specified by itself;

  • -prune: Do not look for a string as a template to find a file or directory style;

  • -regex< template style;: Specifies a string as a template style for searching for a file or directory;

  • -size< file size;: Find files that match the specified file size;

    • b--block (512 bytes)

    • c--bytes

    • w--Word (2 bytes)

    • k--kilobytes (1024 bytes)

    • m--MBytes (1048576 bytes)

    • g--gigabytes (1073741824 bytes)

  • -true: Sets the callback value of the Find command to true;

  • -typ< file type;: Only files that match the specified file type are found;

    • B-block device files.

    • D-Directory.

    • C-character device file.

    • P-Pipeline file.

    • L-Symbolic link file.

    • F-Normal file.

  • -uid< user identification Code;: Find a file or directory that matches the specified user ID;

  • -used< days;: Find files or directories that have been accessed at a specified time after a file or directory has been changed, and the units are calculated on a daily basis;

  • -user< name of the owner;: a file or directory with the specified owner name;-version or--version: Displays version information;

  • -xdev: Confine the scope to the first file system;

  • -xtype< file type;: The effect of this parameter is similar to specifying the "-type" parameter, except that it checks for symbolic connections.

Combination criteria: Precede the options you want to find

    • -A: With, while satisfying, can be omitted

    • -O: Or, a meeting

    • -not,! Non -. Take counter

Tip: Search by permissions:-perm [+|-]mode exact match +mode: any one of the user's rights matches, often used to find out whether a certain class of user's specific permissions exist,-mode: Each class of users of the specified permission bits to check match, for example:
File permissions: 644
-perm 600: Mismatch
-perm + 222: match, find user has Write permission
-perm + 002: Mismatched, 0 means not viewing
-perm-444: Match

Find criteria search by file name
    • -name "pattern"

    • -iname "pattern": Supports Glob-style wildcard characters;

    • -regex pattern: Finds a file based on a regular expression pattern, which matches the entire path, not its name;

Find based on file dependencies
    • -user USERNAME: Find all files belonging to the owner of the specified user;

    • -group GRPNAME: Finds all files belonging to the specified group of groups;

    • -uid uid: Finds all files that belong to the UID specified by the master;

    • -gid GID: Finds all files belonging to the specified GID of the group;

    • -nouser: Find files that are not owned by the master;

    • -nogroup: Find files without a group;

Find based on the type of file
    • -type Type:
      F: Normal file
      D: Catalog file
      L: Symbolic Link file
      B: Block device files
      C: Character device file
      P: Pipeline File
      S: Socket file

Combination Test

With:-A, default combination logic;
Or:-O
Non:-not,!

Find based on the size of the file
    • -size [+|-] #UNIT: Common units: K, M, G
      #UNIT: (#-1, #)
      -#UNIT: [0,#-1]
      + #UNIT: (#, OO)

Find by Time stamp

In "Days" as the unit

    • -atime [+|-]#
      #:[#, #-1)
      -#: (#, 0]
      +#: (Oo, #-1)

    • -mtime

    • -ctime

In "Minutes" units:

    • -amin

    • -mmin

    • -cmin

Find based on permissions
    • -perm [/|-]mode
      Mode: precise permission matching;
      /mode: Any one (r,w,x) of the permissions of any class of users (U,g,o) satisfies the condition, and there is a "or" relationship between 9-bit permissions;
      -mode: Each class of users (U,g,o) in the permissions of each bit (r,w,x) at the same time meet the conditions are satisfied; 9-bit permissions exist between the "and" relationship;

Handling actions
    • -print: Output to standard output, default action;

    • -ls: Similar to the "ls-l" command for the found file, the output file details;

    • -delete: Delete the found file;

    • -fls/path/to/somefile: Saves the long format information of all files found to the specified file;

    • -ok COMMAND {}; : Executes commands for each file that is found, and each operation is confirmed by the user;

    • -exec COMMAND {}; : Executes commands for each file that is found;

Note: When find passes the file path found to the following command, it is the first time to find out all eligible file paths and pass them to the following commands at once, but some commands cannot accept too long arguments, and the command execution fails; another way to circumvent this problem:

find | xargs COMMAND
Example

Find all files or directories in the/var directory that are subordinate to root and belong to the group mail;

# find /var/ -user root -a -group mail  -ls

Find all files or directories in/usr that do not belong to root, bin, or Hadoop, in two ways;

~]# find /usr -not -user root -a -not -user bin -a -not -user hadoop~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls

Look for the last week in the/etc directory whose contents have been modified and are not the root user or the file or directory of the Hadoop user;

~]# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls~]# find /etc -mtime -7 -a -not -user root -a -not -user hadoop -ls

Find files or directories on the current system that are not part of a group and have been visited in the last week;

~]# find  /  \( -nouser -o -nogroup \)  -atime  -7  -ls

Find all files that are larger than 1M in the/etc directory and are of the normal file type;

~]# find /etc -size +1M -type f -exec ls -lh {} \;

Find files in/etc directory where all users do not have permission to write;

~]# find /etc -not -perm /222 -type f -ls                   

Find/etc directory at least one type of user does not have permission to execute files;

~]# find /etc -not -perm -111 -type f -ls

Find the/etc/init.d/directory, all users have execute permissions, and other users have write permission to all files;

~]# find /etc -perm -113 -type f -ls
Xargs

Xargs is a filter that passes parameters to the command, and is also a tool for combining multiple commands. It divides a data stream into small enough chunks to facilitate the processing of filters and commands. Typically, Xargs reads data from a pipe or stdin, but it can also read data from the output of a file. The default command for Xargs is echo, which means that the input passed to Xargs by the pipeline will contain line breaks and whitespace, but with xargs processing, line breaks and whitespace will be replaced by spaces.

Parameters
    • -0: When Sdtin contains special characters, it is treated as a general character, such as blank or quotation marks.

    • -A file: read in as Sdtin

    • -E flag: Note Sometimes it may be that the-e,flag must be a space-delimited flag that stops when Xargs is analyzed to contain flag.

    • -N Num: The number of argument to be used at the time of execution of the command, by default, is used for all.

    • -P: The operation is interactive, each execution comand interactively prompts the user to choose, when each execution of a argument to ask the user

    • -T: Indicates that the command is printed before execution.

    • -I or-I: This depends on Linux support, the name of each of the xargs, usually a line assigned to {}, you can use {} instead.

    • -R: If there is no parameter to be processed to Xargs,xargs, the default is to run with an empty parameter, if you want no parameters, stop Xargs, exit directly, use the-r option, it can prevent xargs after command with empty parameters run error.

    • -S NUM xargs the maximum number of command-line characters (with spaces) behind that command

    • -L reads NUM lines once from standard input to command commands, as-l and-l functions.

    • The-D Delim delimiter, the default Xargs delimiter is carriage return, the delimiter for argument is a space, and this is where the Xargs delimiter is modified.

    • -X exit means that if there is any command line larger than the-s Size flag specified by the number of bytes, stop running the xargs command,-l-i-n defaults to the-x parameter, mainly with-s used.

    • -P modifies the maximum number of processes, the default is 1, which is 0 times for as many as it can.

Example

-0 Options:

[[email protected] test]# find /root/test -type f -print0/root/test/text1/root/test/text4/root/test/text2/root/test/text3[[email protected] test]# find /root/test -type f -print0 |xargs xargs: 警告: 输入中有个 NUL 字符。它不能在参数列表中传送。您是想用 --null 选项吗?/root/test/text1[[email protected] test]# find /root/test -type f -print0 |xargs -0/root/test/text1 /root/test/text4 /root/test/text2 /root/test/text3

-A option:
···
[email protected] test]# Cat Text1
1 apple
2 Pear
3 Banana
[Email protected] test]# Xargs-a Text1 Echo
1 apple 2 Pear 3 banana
···
-E option:

[[email protected] test]# xargs -a text1 -E "pear" echo1 apple 2[[email protected] test]# cat text1 |xargs -E "pear"1 apple 2

-N option:

[[email protected] test]# cat text1 |xargs -n 4 1 apple 2 pear3 banana[[email protected] test]# cat text1 |xargs -n 31 apple 2pear 3 banana

-P Option:

[[email protected] test]# cat text1 |xargs -p/bin/echo 1 apple 2 pear 3 banana ?...y1 apple 2 pear 3 banana

-T option:

[[email protected] test]# cat text1 |xargs -t -p/bin/echo 1 apple 2 pear 3 banana ?...y1 apple 2 pear 3 banana[[email protected] test]# cat text1 |xargs -t /bin/echo 1 apple 2 pear 3 banana 1 apple 2 pear 3 banana

-I option:

[[email protected] test]# ls |xargs -t -i mv {} {}.bakmv text1 text1.bak mv text2 text2.bak mv text3 text3.bak mv text4 text4.bak [[email protected] test]# lstext1.bak  text2.bak  text3.bak  text4.bak

-R option:

[[email protected] test]# echo ""|xargs -t mvmv mv: 缺少了文件操作数请尝试执行"mv --help"来获取更多信息。[[email protected] test]# echo ""|xargs -t -r mv

-S option:

[[email protected] test]# cat text1.bak |xargs  -s 9 echoaaabbbcccddda b[[email protected] test]# cat text1.bak |xargs  -s 4 echoxargs: can not fit single argument within argument list size limit      #length(echo)=4[[email protected] test]# cat text1.bak |xargs  -s 8 echoxargs: argument line too long      #length(echo)=4,length(aaa)=3,length(null)=1,total_length=8

-L Option:

[[email protected] test]# cat text1.bak |xargs -L 1 echo1 apple2 pear3 banana[[email protected] test]# cat text1.bak |xargs -L 2 echo1 apple 2 pear3 banana[[email protected] test]# cat text1.bak |xargs -L 3 echo1 apple 2 pear 3 banana
Xargs and find

When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command. The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.
In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases, there are too many processes and degraded system performance, so the efficiency is not high , while using the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.
The pipeline is to pass the output of one command to another command as input, such as: Command1 | Command2 but Command2 only takes the output as an input parameter. Find. -name "Install.log"-print print Out is Install.log this string, if only uses the pipeline, then Command2 can use only install.log this string, cannot treat it as a file.
Of course this command2 except Xargs. Xargs is written to be able to manipulate the files found by find. It can send a string from the pipeline as a file to the subsequent command execution.

Example

Displays the content from the pipeline, which is handled only as a string:

# find . -name "install.log" -print | cat./install.log     

Send the contents of the pipeline as a file and give it to cat for execution. In other words, the command executes if there is a install.log, then prints out the contents of the file.

# find . -name "install.log" -print | xargs cataaaaaa                                                     

In the current directory, look for files with read, write, and execute permissions for all users, and reclaim the appropriate write permissions:

# find . -perm -7 -print | xargs chmod o-w

Find each normal file in your system, and then use the Xargs command to test what type of file they belong to

# find . -type f -print | xargs file./liyao: empty

Try to delete too many files with RM, you may get an error message:/BIN/RM Argument list too long. Use Xargs to avoid this problem.

$find ~ -name ‘*.log’ -print0 | xargs -i -0 rm -f {}

Find all the JPG files and compress it

# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

Copy all the picture files to an external hard drive

# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

When you try to delete too many files with RM, you may get an error message:/BIN/RM Argument list too long. Use Xargs to avoid this problem.

# find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

Get a list of all the *.conf ends of the/etc/, there are several different ways to get the same result, the following example is just a demonstration of how practical Xargs, in this example practical xargs to pass the output of the Find command to Ls-l

# find /etc -name "*.conf" | xargs ls –l

If you have a file that contains many URLs that you would like to download, you can use Xargs to download all the links

# cat url-list.txt | xargs wget –c

Find all the JPG files and compress it

# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

Use VI to open files of this directory one by one:

#  

Limit the use of only two parameters per command line:

Linux File Lookup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.