Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use find and xargs to find and process files

Shulou Source: shulou.com Published: 2022-06-01 05:05:01 09月17日 Update

Editor to share with you how to use find and xargs to find and deal with files, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Find a file

Find should at least add the path of lookup. For example, this command finds (and prints) every file on the system:

Find /

Since everything is a file, you will see a lot of output. This may not help you find what you need. You can change the path parameters to narrow it down, but this is actually no better than using the ls command. Therefore, you need to consider what you are looking for.

Maybe you want to find all the JPEG files in the home directory. The-name parameter allows you to restrict the results to files that match a given pattern.

Find ~-name'* jpg'

But wait! What if some of these extensions are uppercase? -iname is similar to-name, but is not case-sensitive:

Find ~-iname'* jpg'

Fine! But the 8.3 naming scheme was made in 1985. Some pictures may have a .jpeg extension. Fortunately, we can combine patterns using "OR" (- o). Parentheses need to be escaped so that find commands rather than shell programs try to interpret them.

Find ~\ (- iname 'jpeg'-o-iname' jpg'\)

further more. What if you have some directories that end in jpg? I don't understand why you named the directory bucketofjpg instead of pictures? We can add the-type parameter to find only the files:

Find ~\ (- iname'* jpeg'-o-iname'* jpg'\)-type f

Or maybe you want to find directories with strange names so that you can rename them later:

Find ~\ (- iname'* jpeg'-o-iname'* jpg'\)-type d

You've taken a lot of photos recently, so use-mtime (modification time) to narrow it down to files that have been modified in the last week. -7 represents all files modified within 7 days or less.

Find ~\ (- iname'* jpeg'-o-iname'* jpg'\)-type f-mtime-7

Use xargs to operate

The xargs command takes parameters from the standard input stream and executes the command based on them. Continuing with the example in the previous section, suppose you want to copy all the JPEG files from the home directory you modified last week to a U drive so that you can insert them into the electronic photo album. Suppose you have mounted the flash drive to / media/photo_display.

Find ~\ (- iname'* jpeg'-o-iname'* jpg'\)-type f-mtime-7-print0 | xargs-0 cp-t / media/photo_display

The find command here is slightly different from the previous version. The-print0 command makes some changes to the output: instead of using newline characters, it adds a null character. The-0 (zero) option of xargs adjusts parsing to achieve the desired results. This is important, otherwise operations on file names that contain spaces, quotation marks, or other special characters may not work as expected. These options should be used when taking any action on the file.

The-t parameter of the cp command is important because cp usually requires the destination address to be last. You can do this using find's-exec instead of xargs, but xargs is faster, especially for a large number of files, because it calls cp in a single call.

The above is all the contents of the article "how to use find and xargs to find and process files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: File command parameter directory content article processing important character what to do extension time mode range path help output different special lucky Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Shulou Tech Info Microsoft Xiaomi