0

How can I copy all files of specific type to a specific folder using Mac's Terminal.

find . -name '*.STY' -type f -print0 | xargs -0 -I '{}' /usr/bin/rsync -avR "{}" "./styleFiles_chekad/"

Above shell script does the job and keeps the folders intact. What if I don't want the subfolders and want to have all the files right in the "styleFiles_chekad"? However, it is very complicated. Is there an easier way to do this? The following command does not work!

cp . -name '*.STY' styleFiles_chekad/

Any help would be greatly appreciated.

PS. Anyone can recommend a good resource for bash scripting?

2
  • I found "From Bash to Z Shell” to be very good, but I think "Unix Power Tools (3rd Edition)” is the definitive reference (as much as any book can be considered as such in this day and age. I think it’s like 1200 pages. Zsh as the default shell in MacOS Catalina makes it a good tool to focus on, and the Zsh mailing list is a great and helpful place to seek help. I’ve been on the list since the 90s, I think.
    – TJ Luoma
    Commented Dec 6, 2019 at 15:24
  • O'Reilly Classic Shell Scripting is ... well, a classic.
    – benwiggy
    Commented Dec 6, 2019 at 16:06

1 Answer 1

2

You should be able to just run

t=${TMPDIR:-/tmp}/target
mkdir "$t"

find . -type f -name '*.STY' -exec cp {} "$t"/ \;

mv "$t"/* ./styleFiles_chekad/
rmdir "$t"

The idea behind using a temporary directory is to prevent find from also copying the files it already moved into ./styleFiles_chekad/.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .