I have a /dir1
, the structure is:
./aaa.txt
./bbb.jpg
./subdir1/cccc.doc
./subdir1/eeee.txt
./subdir2/dddd.xls
./subdir2/ffff.jpg
I want to copy all .txt
and .jpg
in /dir1
and its subdirectories to /dir2
.
By using cp -r /dir1/* /dir2
, it copies all files and its structure to /dir2
.
By using cp -r /dir1/*.jpg /dir2
and cp -r /dir1/*.txt /dir2
, it doesn't copy the .jpg
and .txt
files in the subdirectories.
By using
for FILE in `find`
do
cp -f $FILE /dir1/
done
it can copy all files in dir and subdirectories to the destination, but I cannot filter the file type.
Is there another way that can achieve what I want?
You can use find
to get the desired source files, and then use cp
within the -exec
predicate of find
to copy the files to the desired destination. With GNU cp
:
find /dir1/ -type f \( -name '*.txt' -o -name '*.jpg' \) -exec cp -t /dir2/ {} +
POSIX-ly:
find /dir1/ -type f \( -name '*.txt' -o -name '*.jpg' \) -exec cp {} /dir2/ \;