06_Linux Directory File Operation Command 3 Find Command_My Linux Road

The last few sections have roughly told you some commands for file directory operations on the Linux side

In this essay, we will continue to learn the operation commands for file directories

 

command to find files or directories

 

find Find files in the specified directory

find(options)(parameters)

 The find command can be used to find files in a specific directory. By default, the search path needs to be added. If no path is added, the find command will search for subdirectories and files in the current directory.

Then display the searched file or directory

His options are as follows

-amin<minutes>: Find files or directories that have been accessed at a specified time, in minutes;
-anewer <reference file or directory>: Find the file or directory whose access time is closer to the current one than the access time of the specified file or directory;
-atime<24 hours>: Find the files or directories that have been accessed at the specified time, the unit is calculated in 24 hours;
-cmin <minutes>: Find files or directories that have been changed at the specified time;
-cnewer <reference file or directory> finds a file or directory whose change time is closer to the current file or directory than the change time of the specified file or directory;
-ctime<24 hours>: Find files or directories that have been changed at the specified time, in units of 24 hours;
-daystart: Calculate the time from today;
-depth: Search from the deepest subdirectory in the specified directory;
-expty: Find files with a file size of 0 Byte, or an empty directory without any subdirectories or files in the directory;
-exec<execute command>: Assuming the return value of the find command is True, execute the command;
-false: Set the return value of the find command to False;
-fls<list file>: The effect of this parameter is similar to that of specifying the "-ls" parameter, but the result will be saved as the specified list file;
-follow: exclude symbolic links;
-fprint<list file>: The effect of this parameter is similar to that of specifying the "-print" parameter, but the result will be saved as the specified list file;
-fprint0<list file>: The effect of this parameter is similar to that of specifying the "-print0" parameter, but the result will be saved as the specified list file;
-fprintf<list file><output format>: The effect of this parameter is similar to that of specifying the "-printf" parameter, but the result will be saved as the specified list file;
-fstype <file system type>: only look for files or directories under this file system type;
-gid<group ID>: Find files or directories that match the specified group ID;
-group <group name>: Find files or directories that match the specified group name;
-help or --help: online help;
-ilname <template style>: The effect of this parameter is similar to that of specifying the "-lname" parameter, but the difference between upper and lower case characters is ignored;
-iname<template style>: The effect of this parameter is similar to that of specifying the "-name" parameter, but the difference between upper and lower case characters is ignored;
-inum<inode number>: Find files or directories that match the specified inode number;
-ipath<template style>: The effect of this parameter is similar to that of specifying the "-path" parameter, but the difference between upper and lower case characters is ignored;
-iregex<template style>: The effect of this parameter is similar to that of specifying the "-regexe" parameter, but the difference between upper and lower case characters is ignored;
-links<number of connections>: Find files or directories that match the specified number of hard links;
-iname<template style>: Specify a string as a template style for finding symbolic links;
-ls: Assuming the return value of the find command is True, list the file or directory name to standard output;
-maxdepth<directory level>: Set the maximum directory level;
-mindepth<directory level>: Set the minimum directory level;
-mmin<minutes>: Find files or directories that have been changed at the specified time, 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 the specified time, the unit is calculated in 24 hours;
-name<template style>: Specify a string as the template style for finding files or directories;
-newer <reference file or directory>: Find the file or directory whose change time is closer to the current file or directory than the change time of the specified file or directory;
-nogroup: find files or directories that do not belong to the local host group identifier;
-noleaf: Do not consider that the directory must have at least two hard links;
-nouser: find files or directories that do not belong to the localhost user ID;
-ok<execution command>: The effect of this parameter is similar to specifying "-exec", but the user will be asked before executing the command. If the answer is "y" or "Y", the execution of the command will be abandoned;
-path<template style>: Specify a string as the template style for finding directories;
-perm<permission value>: Find files or directories that match the specified permission value;
-print: Assuming the return value of the find command is True, list the file or directory name to standard output. The format is one name per column, and each name is preceded by a "./" string;
-print0: Assuming the return value of the find command is True, list the file or directory name to standard output. The format is that all names are on the same line;
-printf <output format>: Assuming the return value of the find command is True, list the file or directory name to standard output. The format can be specified by yourself;
-prune: do not look for strings as a template style for finding files or directories;
-regex<template style>: Specify a string as the template style for finding files or directories;
-size<file size>: Find files that meet the specified file size;
-true: Set the return value of the find command to True;
-typ<file type>: only find files that match the specified file type;
-uid<user ID>: Find files or directories that match the specified user ID;
-used<days>: Find the files or directories that have been accessed at the specified time after the file or directory has been changed, the unit is calculated in days;
-user<owner name>: the file or directory of the finder and the specified owner name;
-version or --version: Display version information;
-xdev: limit the scope to the first file system;
-xtype<filetype>: The effect of this parameter is similar to specifying the "-type" parameter, the difference is that it checks for symbolic links.

 For reference or search only

 

Let's fight this command later

 

First we match files based on filename name or regular expression

List all files and folders in all subdirectories of the current directory

find .

 

/homeLook for filenames ending in .txt in a directory

find /home -name "*.txt"

 

Same as above, ignoring case

find /home -iname "*.txt"

 

Find all files ending in .txt and .pdf in the current directory and subdirectories

find . \( -name "*.txt" -o -name "*.pdf" \)

or

find . -name "*.txt" -o -name "*.pdf"

 

match file path or file

find /usr/ -path "*local*"

 

Match file paths based on regular expressions

find . -regex ".*\(\.txt\|\.pdf\)$"

 

Same as above, ignoring case

find . -iregex ".*\(\.txt\|\.pdf\)$"

 

Of course we can also use the deny parameter

Find files under /home that do not end with .txt

find /home ! -name "*.txt"

 

 

We can also search deeply for files by file type

The types are as follows

  • fOrdinary  file
  • lSymbolic  link
  • d  directory
  • character device
  • block device
  • socket
  • Fifo

The command is as follows

find . -type type parameter

 

 

We can also search deeply based on the directory

Downward maximum depth is limited to 3

find . -maxdepth 3 -type f

 

Search for all files that are at least 2 subdirectories away from the current directory

find . -mindepth 2 -type f

 

Of course there are various other types of options, which I won't explain here

If you are interested, you can try it

 

 

locate finds a file or directory

locate/slocate (options) (parameters)

 The ocate command is actually  -nameanother way of writing find, but it is much faster than the latter because it does not search a specific directory, but a database /var/lib/locatedbthat contains all local file information. The Linux system automatically creates this database and automatically updates it once a day, so the latest changed files cannot be found using the locate command.

This can be fixed by manually updating the database

updatedb

 

The options of the locate command are as follows

-d <directory> or --database=<directory>: specify the directory where the database is located;
-u: update the slocate database;
--help: show help;
--version: Display version information.

 

Familiarize yourself with commands with a few examples

Search for all files starting with sh in the etc directory:

locate /etc/sh

 

Search for all files starting with m in the user's home directory, ignoring case:

locate -i ~/m

 

 

whereis location path

whereis(options)(parameters)

 The whereis command is used to locate the path of the binary program, source code file, man page and other related files of the instruction

The options are as follows

-b: look for binary files only;
-B<directory>: Look for binary files only in the set directory;
-f: Do not display the path name before the file name;
-m: find only the description file;
-M<directory>: only find the description file in the set directory;
-s: find only original code files;
-S <directory> only looks for original code files in the set directory;
-u: Find files that do not contain the specified type.

Compared with the find command, the whereis search speed is very fast, because the Linux system records all the files in the system in a database file. When using whereis and locate, which will be introduced below, the data will be searched from the database. , instead of traversing the hard disk to find like the find command, the efficiency will naturally be very high

 

Let's take a look at this command

[root@localhost ~]# whereis tomcat
tomcat:

[root@localhost ~]# whereis svn
svn: /usr/bin/svn /usr/local/svn /usr/share/man/man1/svn.1.gz

 

At the beginning we look for tomcat, because I don't have it installed, so it doesn't show up here

And svn I installed, so I found a lot of related files

 

Of course we can just display the binary file

[root@localhost ~]# whereis -b svn
svn: /usr/bin/svn /usr/local/svn

[root@localhost ~]# whereis -m svn
svn: /usr/share/man/man1/svn.1.gz

[root@localhost ~]# whereis -s svn
svn:

 

The other options are still not demonstrated one by one. Linux is to be learned in practice.

 

 

which finds the absolute path of the command

which (options) (parameters)

The which command is used to find and display the absolute path of a given command. The environment variable PATH stores the directory that needs to be traversed when searching for a command. The which command will look for eligible files in the directory set by the environment variable $PATH. That is to say, using the which command, you can see whether a system command exists, and where the command is executed.

The options are as follows

-n<file name length>: Specify the length of the file name, the specified length must be greater than or equal to the longest file name in all files;
-p <file name length>: same as the -n parameter, but here <file name length> contains the path of the file;
-w: Specify the width of the column when outputting;
-V: Display version information.

 

Familiarize yourself with this command with a few examples below

[root@localhost ~]# which pwd
/bin/pwd

[root@localhost ~]#  which adduser
/usr/sbin/adduser

 

My blog site www.susmote.com 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608883&siteId=291194637