Verwenden von Platzhaltern im Pfad des UNIX-Suchbefehls

1763
LLJ

Dieser Befehl mit einem * im Suchpfad funktioniert auf dem lokalen Server einwandfrei

Svr1$ find /path/*/foo/ -name "*20160208" 

Wenn ich dies von einem anderen Server aus der Ferne versuche, funktioniert es nicht

Svr2$ echo $Pswd|ssh Svr1 /usr/local/bin/sudo -S find /path/*/foo/ -name "*20160208*" 

Die Fehlermeldung lautet:

find: stat() error /path/*/foo/: No such file or directory 

Wenn ich jedoch den Suchpfad ändere, um die Verwendung von * zu vermeiden, funktioniert das einwandfrei. So was:

Svr2$ echo $Pswd|ssh Svr1 /usr/local/bin/sudo -S find /path/ -name "*20160208*" 

Was mache ich falsch? Ich denke, ich muss einen Teil der Saite zitieren oder entziehen, aber ich kann es einfach nicht herausfinden.

Danke vielmals.

1

2 Antworten auf die Frage

2
Matteo

Die *wird von Ihrer lokalen Shell erweitert. Sie sollten Ihr Argument zitieren

ssh Svr1 /usr/local/bin/sudo -S find '/path/*/foo/' -name "20160208" 
Genau. Das * kommt nie am entfernten Computer an. Es wurde erweitert, bevor Sie diese Stufe erreichen. Hennes vor 8 Jahren 0
1
eggo

As @Matteo said, you need to escape the * from your local shell. He's done that in his first example:

ssh Svr1 /usr/local/bin/sudo -S find '/path/*/foo/' -name "20160208" 

As alternative, either of these should work as well, as the single quotes ('...') and backslash (\) "hide" the * wildcard from your local shell:

ssh -t Svr1 /usr/local/bin/sudo 'find /path/*/foo/ -name *20160208' 

or

ssh -t Svr1 /usr/local/bin/sudo find /path/\*/foo/ -name \*20160208 

As a side note, see that I've included a -t argument to ssh so that sudo has a tty through which to ask for my password, which is MUCH better than putting the password on the command line, IMO. If you must pipe the sudo password in, I suggest doing it via cat secret.txt | find ..., which keeps the password out of the command line which is potentially visible to ps and to anyone with access to your shell history (including ~/.history and variants)