Linux/OS X 重命名保留时间戳

标签 linux macos bash touch rename

要将 OS X 服务器的内容传输到 NTFS 兼容服务器,我需要清理所有文件名,保留原始时间戳。

我已经得到了以下命令:

1- 重命名命令 (OS X 版本)首先:brew install rename

find "$1" -print0 | xargs -0 rename 's/[\\:*?"<>|]/-/g'

2- 保留时间戳:

touch -r "$1" reference.tmp; mv -- "$1" "$2"; touch -r reference.tmp -- "$2"; rm reference.tmp

有谁知道我如何将两者结合到一个脚本中......? 感谢所有帮助! :)

最佳答案

通过 brew install rename ( http://plasmasturm.org/code/rename/ ) 安装的重命名似乎默认保留了 mtime、atime 和 ctime,以及创建时间。

$ touch a
$ stat -f'%m %a %c %B' a
1385979835 1385979835 1385979835 1385979835
$ rename s/a/b/ *
$ stat -f'%m %a %c %B' b
1385979835 1385979835 1385979835 1385979835

你也可以使用这样的命令:

for f in **/*; do mv -- "$f" "${f//[\\:*?\"<>|]/-}"; done

** 需要 bash 4.0 或更高版本和 shopt -s globstar

关于Linux/OS X 重命名保留时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20324851/

相关文章:

linux - 如果发生错误,从 shell 脚本执行 shell 命令而不停止

linux - 如何通过将其内存存储到磁盘并稍后恢复它来在 Linux 中 "hibernate"进程?

linux - 使用 Linux 命令 head 和 tail

python - 有没有办法用 python 开发 OS X 应用程序?

macos - Mac OS X 10.4+ 上的 Bash 配置

linux - 有没有办法检查某个选项是否对某个程序有效?

linux - 设置路径变量并运行 Ruby 脚本

macos - NSTextField 在 Swift 中绑定(bind)为 double,如何允许空白?

macos - 如何在OS X Lion中编写“all user”首选项?

python - 在 init.d 后台执行 python 脚本