Find and copy regex in bash (saving folder structure)?

I have a folder with a bunch of log files. Each set of log files is located in a folder with a detailed indication of the time and date of program execution. Inside these log folders, I have video files that I want to extract. All I want is video files, nothing more. I tried to use this command only to copy video files, but this did not work because the directory did not exist.

.rmv is the file extension of the files I want.

$ find . -regex ".*\.rmv" -type f -exec cp '{}' /copy/to/here/'{}'

If I have a folder structure, for example:

|--root  
   |  
   |--folder1  
   |  |  
   |  |--file.rmv  
   |  
   |--folder2  
      |  
      |--file2.rmv  

How can I make it copy / copy / here by copying the structure of folder1 and folder2 in the destination directory?

+3
source share
4 answers
+2

cp -parents , :

find root -name '*.rmv' -type f -exec cp --parents "{}" /copy/to/here \;
+12

{} , cp :

cp /root/folder1/file.rmv /copy/to/here/root/folder1/file.rmv

{},

cp /root/folder1/file.rmv /copy/to/here

copy-file-to-cp, .

, -regex, yor -name:

find root -name '*.rmv' -type f -exec cp {} /copy/to/here \;
+1

Assuming what srcis yours rootand dstis yours/copy/to/here

#!/bin/sh

find . -name *.rmv | while read f
do
       path=$(dirname "$f" | sed -re 's/src(\/)?/dst\1/')
       echo "$f -> $path"
       mkdir -p "$path"
       cp "$f" "$path"
done

putting this in cp.shand running ./cp.shfrom the directory as root

Conclusion:

./src/folder1/file.rmv -> ./dst/folder1
./src/My File.rmv -> ./dst
./src/folder2/file2.rmv -> ./dst/folder2

EDIT: improved version of the script (thanks for the comment)

0
source

Source: https://habr.com/ru/post/1745544/


All Articles