Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use find and xargs to find and process files in linux

Shulou Source: shulou.com Published: 2022-06-01 06:34:36 10月04日 Update

This article is about how to use find and xargs to find and process files in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Find is one of the powerful and flexible command-line programs in the daily toolbox. It is as its name implies: find files and directories that meet your specified criteria. With parameters such as-exec or-delete, you can make it manipulate the files you find.

In this installment of the command line prompt series, you will see an introduction to the find command and learn how to work with files using built-in commands or using xargs commands.

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 operates using xargs

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.

Thank you for reading! On "how to use find and xargs in linux to find and deal with files" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: File command parameter directory content processing important name character what to do extension time more mode program article scope path help output Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple OPPO Reno Linux Shulou Tech Info