如何通过 SSH 重命名所有文件 [英] How to rename all files over SSH

查看:62
本文介绍了如何通过 SSH 重命名所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 SSH 或 SFTP 重命名远程目录中的所有文件.重命名应将文件转换为日期扩展名,例如将 .txt 转换为 .txt.2016-05-25.

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.

我有以下命令来循环每个 .txt 文件并尝试重命名,但出现错误:

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"

我得到的错误是:

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

我也通过 SFTP 尝试过这个,但没有这样的运气.任何帮助将不胜感激!

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

推荐答案

您需要对远程 shell 中的变量 $ 进行转义(或单引号).还建议引用代表文件路径的变量:

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"

这篇关于如何通过 SSH 重命名所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆