LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rm to exclude file (https://www.linuxquestions.org/questions/linux-newbie-8/rm-to-exclude-file-4175576796/)

hilou 04-06-2016 02:53 AM

rm to exclude file
 
I am using !(filename) sytax to exclude file from rm command.

It's working when I run it directly in bash.

But not working when used in a ssh command like below:

ssh 10.23.10.138 'rm -rf /tmp/td/d1/d2/!(*.py)'
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `rm -rf /tmp/td/d1/d2/!(*.py)'

Can someone tell me why ?

Thank you in advance!

JJJCR 04-06-2016 03:39 AM

run: shopt -s extglob

then try running the command again it should be okay.

to turn off: shopt -u extglob

malekmustaq 04-06-2016 03:42 AM

Login first and run bash as local.

hilou 04-06-2016 05:51 AM

Quote:

Originally Posted by JJJCR (Post 5526918)
run: shopt -s extglob

then try running the command again it should be okay.

to turn off: shopt -u extglob

Nope, tested not working.

hilou 04-06-2016 05:51 AM

Quote:

Originally Posted by malekmustaq (Post 5526920)
Login first and run bash as local.

I will need to run this in a script.

malekmustaq 04-06-2016 06:11 AM

Quote:

I will need to run this in a script.
Put and run the script in the host remote machine.

jpollard 04-06-2016 12:54 PM

I think the sshd is getting confused between a command and a shell...

You might try
Code:

ssh 10.23.10.138 bash -c 'rm -rf /tmp/td/d1/d2/!(*.py)'

hilou 04-06-2016 10:18 PM

Quote:

Originally Posted by jpollard (Post 5527177)
I think the sshd is getting confused between a command and a shell...

You might try
Code:

ssh 10.23.10.138 bash -c 'rm -rf /tmp/td/d1/d2/!(*.py)'

Same error! :)

JJJCR 04-07-2016 12:50 AM

as Jpollard suggested try double quotes:

Quote:

ssh 10.23.10.138 bash -c "rm -rf /tmp/td/d1/d2/!(*.py)"

rknichols 04-07-2016 09:15 AM

You might also include "-v" in the bash options so that you can see what command line actually got passed.

sundialsvcs 04-07-2016 11:14 AM

You probably need to use single-quotes to enclose the command, which then itself includes quotes to tell the shell on the other side not to do its own expansion.

But I admit that I'm getting a little froggy about this. I'd use ssh to execute some sort of script on the remote side which does your bidding.


All times are GMT -5. The time now is 09:27 PM.