Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use find and xargs in the Linux command

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to use find and xargs in Linux commands. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

1. Using find, you can find files or directories that match certain characteristics (such as permissions, ownership, length, type, name, etc.), and more can be done by combining-exec,-ok, and-xargs.

Name options for 2.find:

Search according to the file name, such as finding files that match * .txt in your root directory:

Extension: find /!-name "myshell"-print

(

As you can see from the output above, for root users:

~

$HOME

/ root

They all represent the same directory, which is / root.

In particular, for the user xxx, $HOME and ~ represent the / xxx directory, and the location of this directory (take the zhy user as an example) is as follows:

As you can see, / zhy is under / home. For non-administrator users, the / xxx directory is under / home, while the / root directory location for root users is as follows:

Is located directly in the root directory (the administrator is the boss, of course, preferential treatment).

)

If you look in the current directory, use ".":

# find. -name "* .txt"-print

This command looks for files that meet the criteria in the current directory and subdirectories.

Look for files starting with uppercase letters in the current directory:

# find. -name "[Amurz] *"-print

Notice that there is a *

Look for files that start with "host" in / etc:

# find / etc-name "host*"-print

Find all files under $HOME:

# find ~-name "*"-print

Or

# find ~-print

If you want the system to run at a high load, look it up from the root directory:

# find /-name "*"-print (# find /-print)

Modify this command slightly:

# find /-name "*"-print &

You can make it run in the background, and at this time, through ctrl + c, you can't interrupt its execution.

The lookup file begins with two lowercase letters, followed by a two-digit .txt file, which you can use:

# find /-name "[a murz] [a murz] [0-9] [0-9] .txt"-print

3. Perm options for find:

Find files according to file permissions.

Find files that the file owner can read, write, and execute, and which other users can read and execute:

# find. -perm 755-print

Such as:

Extension: find. !-perm 755-print

4. Prune options for find:

When looking for files, ignore a directory, such as:

If you want to look for files in the / apps directory, but not in the / apps/bin directory, you can use:

# find / apps-name "/ apps/bin"-prune-o-print

5. User and nouser options:

User: find the file according to its owner:

# find. -user zhy-print

Under linux, some owners may be deleted by the administrator, and at this point, you can use the-nouser option to find files that do not have a valid account in the owner's / etc/passwd file. When using-nouser, you do not need to give a user name.

# find /-nouser-print

6. Use group and nogroup:

Just like using user and nouser, the group and nogroup options can find files that belong to a user group and files that no longer exist.

# find /-group zhy-print

# find /-nogroup-print

7. Use the-mtime option:

This option allows you to find files that have been modified within or outside the specified time. If the system suddenly runs out of free space, it is likely that the length of a file will grow rapidly during this period, and you can use the mtime option to find such files:

-1 represents a file that has been changed within 1 day (24 hours).

+ 2 represents files that have been changed before 2 days (48 hours).

8. Newer option:

The newer command allows us to find files with a change time between two files, such as the following two files, with a change time difference of about two days:

With the following command, we can find files whose change time is between these two:

# find /-newer age.awk!-newer belts.awk-exec ls-l {}\

As you can see from the above, there must be a reference file for this comparison. For example, to find a file with a change time of less than two hours, there must be a file with a change time exactly two hours ago. Here we can use touch to create a file that specifies the change time:

If it is 10:41 on December 26th, you need to create a file with a change time of 08:41 on December 26th:

# touch-t 12260841 oldfile

Then pass:

# find /-newer oldfile-print

To find files that were changed within two hours.

9. Use type to find files of the specified type:

Under the root directory, find all the directories:

# find /-type d-print

Find all types of files except directories:

# find /!-type d-print

Find all symbolic link files:

# find /-type l-print

Find all ordinary files:

# find /-type f-print

10. Use the size option:

Use to find files of a specified length, either in bytes or in blocks, 1 block = 512 bytes (0.5m).

In bytes, you need to add c after the number, such as:

Look for files with a length greater than 1m in the root directory:

# find /-size + 1000000c-print

Expressed in blocks is:

# find /-size + 2-print

Find a file that happens to be 100 bytes:

# find /-size 100c-print

Find files that are less than 100 bytes:

# find /-size-100c-print

11. Use the depth option:

Using the depth command when searching, you can first find all files in the root directory of the specified files, and then go to the subdirectories to find them, which may be required for backup sometimes.

For example, starting from the root directory, look for a file named "CON.FILE". It will first match all the files and then go to the subdirectory to look for it:

# find /-name "CON.FILE"-depth-print

twelve。 Use the mount option:

When looking for files, you can only look under the specified directory, not under a subdirectory, which can be specified by mount.

Such as:

# find. -name "* .XC"-mount-print

13. Use the cpio option:

The cpio option can be used to back up or restore files from a tape device. With find, you can look for files throughout the file system and then back them up to tape with the cpio command.

The following command:

# cd /

# find etc home apps-depth-print | cpio-ivcdC65535-o\

/ dev/rmt0

It is used to tell the system that the shell command is not finished. Please ignore the carriage enter after\. This command is used to back up files in the / etc, / home, and / apps directories to the device / dev/rmt0.

In the above command, there is no / before etc, home or apps. This is a relative path (absolute path is used). The relative path is used because:

When you restore these files from tape, you can choose the path to restore the files. For example, you can restore these files to another directory, do something with them, and then restore them to the original directory. If you use an absolute path when backing up, such as / etc, you can only restore to the / etc directory when restoring, and there is no other choice.

The above command tells find to first go to the / etc directory, then the / home and / apps directories, match the files in these directories, and then match the files in their subdirectories, all of which will be piped to the cpio command for backup.

By the way, in the above example, the cpio command uses the C65536 option, but I could have used the B option, but the size of each block is only 512 bytes, but with the C65536 option, the block size becomes 64K bytes (65536 / 1024).

14. Use the exec or ok option:

After you match some files with find, you can use the exec and ok options to do something about them.

The format for exec and ok is: the exec (ok) option is followed by the command to be executed, followed by a pair of {}, a space and a\, and finally a semicolon.

Such as:

# find. -type f-exec ls-l {}\

This command finds and lists matching files.

# find logs-type f-mtime + 5-exec rm {}\

This command looks for normal files that were changed 5 days ago and deletes them.

Security Mode of exec: ok

# find. -name "* .log"-mtime + 5-ok rm {}\

The only difference between this command and exec is that it prompts you when it is deleted.

# find / etc-name "passwd*"-exec grep "zhy" {}\

This command first matches all files named "passwd*", such as passwd, pssswd.old, passwd.bak, and then executes the grep command to see if there is a zhy user in these commands.

Other examples of find:

# find ~-print (find $HOME-print)

Find all files

# find. -type f-perm 4755-print

Find the suid setting for files that the file owner has read, write, and execute permissions, and other users have read and execute permissions.

# find /-group zhy-print

Look for files whose owner is zhy.

# $find / logs-name 'admin.log [0-9] [0-9] [0-9]'-mtime + 7-exec rm {}\

Find the file with the specified name and the modification date is 7 days ago, and delete it.

# find / dev/rmt-print

Find all rmt tape devices in the system.

15. Use the xargs option:

Why use xargs:

When processing matched files using the-e x e c option of the f i n d command, the f i n d command passes all matched files together to e x e c for execution. Unfortunately, some systems have limits on the length of commands that can be passed to e x e c, so that an overflow error occurs a few minutes after the f i n d command runs. The error message is usually "Parameter column too long" or "Parameter column overflow". This is what the x a rg s command is for, especially when used with the f i n d command. The F i n d command passes the matching files to the x a rg s command, while the x a rg s command fetches only some files at a time, not all of them, unlike the-e x e c option. In this way, it can first process the first part of the files obtained, then the next batch, and so on. In some systems, using the-e x e c option initiates a process to process each matched file, instead of executing all the matched files as parameters at once; in some cases, there will be too many processes and poor system performance, resulting in inefficiency; while using the xargs command, there is only one process. In addition, when using the x a rg s command, whether to get all the parameters at once or in batches, and the number of parameters each time will be determined according to the options of the command and the corresponding adjustable parameters in the system kernel.

Example:

# find /-type f-print | xargs file | tee / root/shell/xargs.log

Find each normal file in the system, then use the xargs command to test which type of file they belong to, and import the output into the xargs.log file. Try it with-exec, that is:

# find /-type f-print-exec file {}\; | tee / root/shell/exec.log

By comparing xargs.log and exec.log, it is found that the output of both is indeed the same, but the efficiency of executing the xargs command is much faster than that of-exec (in terms of execution speed).

# find / apps/audit-perm 777-print | xargs chmod Omurw

Look in the / a p p s / a u d i t directory for files for which all users have read, write, and execute permissions, and reclaim write permissions for other users.

# find /-type f-print | xargs grep "device"

Use the grep command to search for the word device in all ordinary files:

# find. -name\ *-type f-print | xargs grep "192.168.5.29"

This command searches all ordinary files in the current directory for the word 192.168.5.29 and\ cancels the special meaning of * in the find command in shell.

The above is how to use find and xargs in the Linux command. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report