如何在包含正则表达式的命令中使用ssh?

时间:2014-04-07 11:01:44

标签: regex bash ssh

例如,我有一个目录:/ full / path / to / your / dir,其中包含以下文件:

$ls /full/path/to/your/dir 
/full/path/to/your/dir/test.1.log.gz
/full/path/to/your/dir/test.2.log.gz
/full/path/to/your/dir/test.3.log.gz

如果我直接在目标主机上运行cmd,它将打印结果而不会出现错误:

$ls /full/path/to/your/dir/test.!(3).log*
/full/path/to/your/dir/test.1.log.gz
/full/path/to/your/dir/test.2.log.gz

但如果我通过ssh在另一台主机上运行到目标主机执行,例如:

$ssh myself@hostname "ls /full/path/to/your/dir/test.!(3).log*"
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `ls /full/path/to/your/dir/test.!(3).log*'

另外,我试过以下:

$ssh myself@hostname "ls /full/path/to/your/dir/test.'!(3)'.log*"
ls: /full/path/to/your/dir/test.!(3).log*:  No such file or directory

任何人都可以帮忙告诉如何更正cmd吗?

2 个答案:

答案 0 :(得分:3)

您正在尝试使用必须首先启用的扩展模式匹配功能herehere可以shopt -s extglob

启用

答案 1 :(得分:-1)

你能做的是:

ssh myself@hostname 'ls | grep -v test\.\(3\)\.log$'