1

I've been trying to copy some remote files to localhost using regex expression, but it reads the regex as if it would be a regular string and does not find any files matching that.

Any ideas why?

file-download

#!/bin/bash

scp "[email protected]:/home/student/download-this/[a-zA-Z0-9]+\.tar\.gz" .

I have also tried copying from localhost to remote device, but still the regex won't work with scp

file-upload

scp "/root/scripts/this[XYZ]_[0-9]{1,5}\.txt" [email protected]:/home/fs/upload

Local host files

Remote device files

Error messages

  • expansion is performed by shell, what does `ls "/root/scripts/this[XYZ]_[0-9]{1,5}\.txt"` (quotes) and `ls /root/scripts/this[XYZ]_[0-9]{1,5}\.txt` (no quotes) show ? – Archemar Jan 08 '23 at 15:07
  • `ls "/root/scripts/this[XYZ]_[0-9]{1,5}\.txt"` -> `ls: cannot access '/root/scripts/this[XYZ]_[0-9]{1,5}\.txt': No such file or directory`. `ls /root/scripts/this[XYZ]_[0-9]{1,5}\.txt` -> `ls: cannot access '/root/scripts/this[XYZ]_[0-9]5.txt': No such file or directory /root/scripts/thisY_91.txt` – Florin200217 Jan 08 '23 at 15:22
  • 1
    How do you expect `{1,5}` regex quantifier to work in `ls` ? @Florin200217 – Gilles Quénot Jan 08 '23 at 15:25
  • I see it won't work, do you have any other suggestions? @GillesQuenot – Florin200217 Jan 08 '23 at 15:28
  • Check my answer below – Gilles Quénot Jan 08 '23 at 15:37
  • in first case quote prevent expansion in second `pattern{1,5}` is expanded into `pattern1 pattern5` which is not what you want. (and work only for `this_Y91`. – Archemar Jan 08 '23 at 15:37
  • 1
    Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee Jan 08 '23 at 17:24
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. Please see [the relevant help-center article](https://stackoverflow.com/help/someone-answers) – Gilles Quénot Jan 13 '23 at 06:15

1 Answers1

0

As said in comments, scp is not regex capable. Better use a glob like this:

scp '[email protected]:/home/student/download-this/[a-zA-Z0-9]*.tar.gz' .
#   ^                                                                     ^
# need this single quotes

You can't use regex, instead, scp can handle glob, check

man bash | less +/'Pattern Matching'

To 'upload' a file by wildcard *:

scp [a-zA-Z0-9]*.tar.gz [email protected]:/home/student/upload-this/
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82