-1

I have list of files a.xxx a.yyy. a.zzz

I need copy all files by select by extension for example

ls *.xxx | xargs cp a.* dir

I write such code

ls mysql/db/*.MYD | xargs -n1 basename | sed 's/\.MYD//g' | xargs -i cp mysql/db/{}.* new_folder

but get error

cp: cannot stat 'mysql/db/ps_opc_social_customer.*'
kusanagi
  • 14,296
  • 20
  • 86
  • 111
  • 1
    if you're copying all the files of similar name, like of extension `*.xxx`, you can use `find` command, like : `find /path_name/ -type f -name "*.xxx" -exec cp {} /destination_path/ \;` – User123 Jul 09 '18 at 07:52
  • I need copy all files *.xxx *.yyy *.zzz by select by one extension *.xxx – kusanagi Jul 09 '18 at 08:01

1 Answers1

1

The problem here is that the * in the last command gets expanded by the shell at the very instant when you press return, so the copy command does not get an expanded string, but the literal <file>.* string. You need to get all the files you need in one go or to use a new shell to do the glob expansion for you:

ls mysql/db/*.MYD | xargs -n1 basename | sed 's/\.MYD//g' | xargs -i bash -c "cp mysql/db/{}.* new_folder"
Poshi
  • 5,332
  • 3
  • 15
  • 32