0

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.

quanta
  • 51,413
  • 19
  • 159
  • 217

2 Answers2

2

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
Daniel t.
  • 9,291
  • 1
  • 33
  • 36
0
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.

quanta
  • 51,413
  • 19
  • 159
  • 217