7

I tried with the following command:

cp src_folder/[!String]* dest_folder

However, this command will copy all the files that don't start with any of the characters 'S','t','r','i','n','g' instead of copying files that don't start with "String".

mstaniloiu
  • 2,325
  • 3
  • 22
  • 18

4 Answers4

15

A variation on Konrad answer, using cp option -t to specify target directory simplifies the last command. It creates a single cp process to copy all the files.

ls src_folder | grep -v '^String' | xargs cp -t dest_folder
  • list all files in src_folder
  • filter out all those that start with String
  • copy all remaining files to dest_dir
Didier Trosset
  • 34,718
  • 13
  • 85
  • 119
5

In bash:

shopt -s extglob
cp src_folder/!(String*) dest_folder
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
3
ls src_folder | grep -v '^String' | xargs -J % -n1 cp % dest_folder

This will

  • list all files in src_folder
  • filter out all those that start with String (so that the rest remains)
  • Invoke the cp command
    • once for each of those files (-n1 says to call cp for each of them separately)
    • using, as its arguments, % dest_folder, where % is replaced by the actual file name.
Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183
  • My `xargs` (Debian Testing) doesn't have a `-J` option. Did you mean `-I`? – maxelost Jan 12 '11 at 16:14
  • 1
    @maxelost: I’m using the BSD version from OS X. Option `-I` does something else. It sucks that all distributions have different switches on their POSIX tools. :-( So much for standardization. – Konrad Rudolph Jan 12 '11 at 16:20
1
cp src_folder/!(String*) dest_folder

Try that ~ Chris

TyrantWave
  • 4,485
  • 2
  • 21
  • 24