Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to find a file and copy it to a specified directory through the find command

Shulou Source: shulou.com Published: 2022-06-01 06:59:40 09月16日 Update

Editor to share with you how to find files and copy them to a specified directory through the find command. I hope you will get something after reading this article. Let's discuss it together.

Copy the files found by the find command to a new directory

There is a need to copy some qualified files from one directory to another. I use the find command to find qualified files from the source directory and then use the cp command to copy to the destination directory:

Method one

The command is as follows:

Find src_dir-name "access.log.2011102 [2-6] *"-exec cp {} dst_dir\

Command to copy files to the destination directory on the remote host:

Find src_dir-name "access.log.2011102 [2-6] *"-exec scp {} username @ host ip:dst_dir\

Method two

Find src_dir-name "access.log.2011102 [2-6] *" | xargs-I cp {} dst_dir

Or

Find src_dir-name "access.log.2011102 [2-6] *" | xargs-I {} cp {} dst_dir

Command to copy files to the destination directory on the remote host:

Find src_dir-name "access.log.2011102 [2-6] *" | xargs-I scp {} username @ host ip:dst_dir

Or

Find src_dir-name "access.log.2011102 [2-6] *" | xargs-I {} scp {} username @ host ip:dst_dir

Src_dir source directory

Dst_dir destination directory

Access.log.2011102 [2-6] * regular expression of the file name to get the conditions of the file

Method three

The find command, combined with the cp command, copies all files from one directory to another.

The entire directory is required to be copied completely to another directory, and individual directories are ignored, as shown in the script:

Find. /-path'/ tmp/mnt/disk1/ignore'-prune-o\ (- name'*'!-name "* .tmp"\) | xargs cp "destination directory"{}"\

In the above script, when "| xargs cp" is executed, assume that the input string looks like this:

/ tmp/mnt/disk1/tt.txt

/ tmp/mnt/disk1/test/dd.txt

After the execution of "xargs cp", the corresponding copy is made into the following directory structure

(that is, all the files under the original disk1 directory are copied to the src directory, and the directory structure remains unchanged):

/ tmp/mnt/src/tt.txt

/ tmp/mnt/src/test/dd.txt

After reading this article, I believe you have a certain understanding of "how to find files and copy them to a specified directory through the find command". If you want to know more about it, please follow the industry information channel and thank you for your reading!

Tags: Directory copy file command host target method condition user user name source directory article structure script character string done file name more regular Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology MariaDB Apple Shulou Information