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 find a file and copy it to a specified directory through the find command

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

Share

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

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!

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