Linux Find Command Mastery Guide

  
 

The Linux find command is the most useful and the most confusing of all Linux commands. It's hard because its syntax is different from the standard syntax of other Linux commands. However, it is powerful because it allows you to find files by file name, file type, user, or even timestamp. With the find command, you can find not only files with any combination of these properties, but also the files it finds.

The purpose of this article is to simplify the learning and use of the command by outlining the purpose and potential of the find command. At the same time, it will provide a basic guide and reference for some of the most powerful but confusing aspects of the find command.

[Note: The version of find used in this article is the GNU version, so some details may differ from other versions of find. ]

Basic Format

Before you start, let's take a look at the basic structure of the find command: find start_directory test options criteria_to_matchaction_to_perform_on_results

In the following command, find will start at the current Look for any file with the extension "java" in the directory (indicated by “.”): find . -name "*.java"

The following is a thumbnail of the command found by the command. Checklist: find . -name "*.java"./REGEXPvalidate/src/oracle/otnsamples/plsql/ConnectionManager.java./REGEXPvalidate/src/oracle/otnsamples/plsql/DBManager.java..

[Note: If you cut and paste from this article to run the find command, you may need to replace the double quotes (“”) with your own keyboard to get the correct results. ]

The following command will perform the same operation. In both cases, you need to escape the wildcard to make sure it is passed to the find command and is not interpreted by the shell. So put your search string in quotation marks or precede it with a backslash: find . -name /*.java

Although all parameters of find are optional, You did not specify where to start the search, and the search will start in the current directory by default. If you do not specify a test connection, option, or value to match, your results will be incomplete or indistinguishable. Running the following three find commands will result in the same result — a complete list of all files (including hidden files) in the current directory and all subdirectories: find find .find . -print

This is similar to running An ls command with the -la option. If you want the output of the above command to include the full pathname (perhaps for backup), you will need to specify the full path to the home directory: find /home/bluher -name /*.java/home/bluher/plsql/REGEXPvalidate/Src/oracle/otnsamples/plsql/ConnectionManager.java/home/bluher/plsql/REGEXPvalidate/src/oracle/otnsamples/plsql/DBManager.java/...

You can also specify in the search string Multiple starting directories. If running as a user with appropriate privileges, the following command will find all jar files in the /usr, /home /tmp directory: find /usr /home /tmp -name "*.jar"

But If you do not have the appropriate permissions, you will generate an error message when you start browsing through many system directories. Here's an example: find: /tmp/orbit-root: Permission denied

You can avoid confusing output by appending your search string as follows: find /usr /home /tmp -name "*.jar" 2>/dev/null

This will send all error messages to the empty file, thus providing cleaner output.

By default, find is case sensitive. For a case-insensitive find, replace the -iname test with the -name test. Find downloads -iname "*.gif"downloads/.xvpics/Calendar05_enlarged.gifdownloads/lcmgcfexsmall.GIF

In addition to the file name, you can also search for files by type. For example, you can use the following command to find all subdirectories in a directory: find . -type d

You can find all the symbolic links in your /usr directory with the following command: find /usr -type l

This may list more than 3,000 links. Any of the following commands running with root privileges will list the links in the /usr directory and the files it points to: # find /usr/bin -type l -name "z*" -exec ls -l {} /;lrwxrwxrwx 1 root root 8 Dec 12 23:17 /usr/bin/zsh -> /bin/zshlrwxrwxrwx 1 root root 5 Dec 12 23:17 /usr/bin/zless -> zmorelrwxrwxrwx 1 root root 9 Dec 12 23:17 /usr/bin/zcat -> /bin/zcatfind /usr/bin -type l -name "z*" -ls

But the second shorter command will be listed More files, as well as directory and inode information: In the remainder of this article, we will discuss the use of the -exec and -ls operations.

Other find types that can be found include:

• b — block (cache) special • c — character (uncached) special • p — named pipe ( FIFO)• s — Socket

Using the root as the starting point for the find command can greatly slow down the system. If you have to run such a command, you can run it during off-peak hours or at night. You can redirect the output to a file using the following syntax: find /-print > masterfilelist.out

If you enter a find command by mistake and generate a lot of unnecessary output, just press CTRL-C Interrupt the command, which will stop the most recently executed command.

On a corporate network with multiple file systems, limiting the files found by find is also a particularly useful method. Use as many options and tests as possible to reduce the load on your system. The two most useful options for this purpose are -xdev and -mount. They shorten the search by preventing finds from going down to directories on other file systems such as MS-DOS, CD-ROM, or AFS. This limits the search to the same type of file system as the home directory.

If you run the mount command, users on dual-boot systems can use these options. Assuming the Windows
partition is involved, you can install it using a command similar to the following: mount -t vfat /dev/sda1 /mnt/msdos

The actual command you use depends on your system settings . You can verify that the partition is installed by running df or by executing the following command: find /mnt/msdos -name "*.txt" 2> /dev/null

You should see the MS Windows partition listed A lot of files. Now run the following command with the -mount or -xdev option: find /-name "*.txt" -mount 2> /dev/null

Copyright © Windows knowledge All Rights Reserved