Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am using xargs to remove files from remote server.

xargs -a /var/log/del.log -i -exec  ssh root@abc.com 'rm  -rf "{}"'

del.log contains path of the files which are deleted on local server and I want to delete them on remote server. Every thing is working fine but problem starts when their is a temoprary office file like ~$excel.xlsx when file naming like this occur xargs makes the command like.

ssh root@abc.com 'rm -rf "~.xlxs"'  - which is wrong,

It should be like - ssh root@abc.com 'rm -rf "~$excel.xlxs"'

Why xargs is doing like this? May be xargs is excepting it as variable. I need some solutions please.

Or is there a better way to delete files from remote server? Provided the local server has the list of path which need to be deleted on remote.

share|improve this question

2 Answers 2

up vote 0 down vote accepted
xargs -a /var/log/del.log -i -exec  ssh root@abc.com 'rm  -rf "{}"'

Swap the single quotes and the double quotes:

xargs -a /var/log/del.log -i -exec  ssh root@abc.com "rm  -rf '{}'"

then the command will be expand to:

ssh root@abc.com "rm -rf '~$excel.xlxs'"

and it works fine because:

man bash:

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the excep‐ tion of $, , \, and, when history expansion is enabled, !. The characters $ and retain their special meaning within double quotes.

share|improve this answer
    
Great ... this worked thanks for the help. –  user1993197 Feb 26 '13 at 16:40

The shell is interpreting the word following the $ sign and preceding the . as a variable. Since the $excelis not set as a variable and not an environment variable either, the shell is replacing it with an empty string. So

  ~$excel.xlxs  ==>  ~.xlxs     when you replace the variable with an empty string.

To remediate this, you need to precede the dollar sign with an escape character \ -

   rm -rf ~\$excel.xlsx
share|improve this answer
    
Thanks for Good advice this is nice solution but Quanta's suggested solution works fine. –  user1993197 Feb 26 '13 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.