Jon
Jon

Reputation: 739

Copy multiple files of one type into another directory in shell

I'm trying to copy all files of ".sh" saved in one directory to another.

I can copy the entire directory using

cp -rp ~/Documents/ToCopy ~/Documents/CopyToHere

but I cannot figure out how to select only the .sh files

Is there also anyway to change the file names of the copied files?

Upvotes: 1

Views: 4489

Answers (3)

Stephen Dedalus
Stephen Dedalus

Reputation: 11

Did you try doing *.sh so that you have cp ~/Documents/*.sh ~/Documents/CopyToHere ? That should work...

Upvotes: 0

jhartz
jhartz

Reputation: 1596

cp -rp ~/Documents/ToCopy/*.sh ~/Documents/CopyToHere

The * is a wildcard character that represents any number of characters in the filenames, so *.sh matches any file in that directory that ends in sh.

I'm pretty sure the wildcard character (*) is parsed by the shell itself, not by the cp command (so watch out if you are calling it from somewhere that isn't a shell).

Upvotes: 3

Jeffrey
Jeffrey

Reputation: 1689

You can try typing

cp -rp ~/Documents/*.sh ~/Documents/CopyToHere

The *.sh selects all files in the directory whose names end in ".sh" using regular expressions. As another example, you could copy all files whose names contain the word "pizza" by typing *pizza* on the command line.

Upvotes: 0

Related Questions