Search code examples
bashsshsftpbatch-rename

How to rename all files over SSH


I am trying to rename all files in a remote directory over SSH or SFTP. The rename should convert the file into a date extension, for example .txt into .txt.2016-05-25.

I have the following command to loop each .txt file and try to rename, but am getting an error:

ssh $user@$server "for FILENAME in $srcFolder/*.txt; do mv $FILENAME $FILENAME.$DATE; done"

The error I am getting is:

mv: missing destination file operand after `.20160525_1336'

I have also tried this over SFTP with no such luck. Any help would be appreciated!


Solution

  • You need to escape (or single-quote) the $ of variables in the remote shell. It's also recommended to quote variables that represent file paths:

    ssh $user@$server "for FILENAME in '$srcFolder'/*.txt; do mv \"\$FILENAME\" \"\$FILENAME.$DATE\"; done"