-2

I have these files in my source folder

source_path/date=20191230/somefile.txt
source_path/date=20191231/somefile.txt
source_path/date=20200101/somefile.txt
source_path/date=20200102/somefile.txt

If I do the bellow command all files will be copied to my dest_path folder

cp --recursive source_path/ dest_path/

I just want to copy all folders where dates are in 2020 or something

I just need these to files of 2020

source_path/date=20200101/somefile.txt
source_path/date=20200102/somefile.txt

How can I add filters with cp command

Ajay Chinni
  • 780
  • 1
  • 6
  • 24

2 Answers2

2

This question is not suitable for Stack Overflow, but this is the answer:

cp --recursive source_path/date=20200* dest_path/

Or does dest_path not exist? Then you would write

mkdir -p dest_path && cp --recursive source_path/date=20200* dest_path/
ypnos
  • 50,202
  • 14
  • 95
  • 141
1

You can use find with the -name, -type and -exec flags and so if the source directory was /home/foo and the destination directory /tmp:

find /home/foo -type d -name "*date-2020*" -exec cp '{}' /tmp \;

-type signifies that we only searching directories, -name is the name we are searching for and exec for command we are executing with the results

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18