根据排序的创建日期重命名文件?

发布于 2024-08-15 03:09:29 字数 174 浏览 2 评论 0原文

我有一个目录,里面充满了随机名称的文件。我希望能够根据时间顺序(即文件创建日期)将它们重命名为“文件 1”“文件 2”等。我可以编写一个简短的 Python 脚本,但那样我就学不到任何东西。我想知道是否有一个聪明的 1 行命令可以解决这个问题。如果有人能指出我正确的方向。

我正在使用 zsh。

谢谢!

I have a directory filled with files with random names. I'd like to be able to rename them 'file 1' 'file 2' etc based on chronological order, ie file creation date. I could be writing a short Python script but then I wouldn't learn anything. I was wondering if there's a clever 1 line command that can solve this. If anyone could point me in the right direction.

I'm using zsh.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-08-22 03:09:29

对于 zsh

saveIFS="$IFS"; IFS=

对于 Bash(请注意读取选项和从零开始的索引而不是从一开始的索引的差异):

saveIFS="$IFS"; IFS=

这些通过添加修改来重命名文件date 到原始文件名的开头,保留该文件名是为了防止名称冲突。

由此产生的文件名可能如下所示:

2009-12-15+11:08:52.original.txt

由于使用 null 作为内部字段分隔符 (IFS),因此应保留带有空格的文件名。

\0'; while read -A line; do mv "${line[2]}" "${line[1]%.*}.${line[2]}"; done < <(find -maxdepth 1 -type f -printf "%T+ %f\n"); IFS="$saveIFS"

对于 Bash(请注意读取选项和从零开始的索引而不是从一开始的索引的差异):


这些通过添加修改来重命名文件date 到原始文件名的开头,保留该文件名是为了防止名称冲突。

由此产生的文件名可能如下所示:


由于使用 null 作为内部字段分隔符 (IFS),因此应保留带有空格的文件名。

\0'; while read -a line; do mv "${line[1]}" "${line[0]%.*}.${line[1]}"; done < <(find -maxdepth 1 -type f -printf "%T+\0%f\n"); IFS="$saveIFS"

这些通过添加修改来重命名文件date 到原始文件名的开头,保留该文件名是为了防止名称冲突。

由此产生的文件名可能如下所示:

由于使用 null 作为内部字段分隔符 (IFS),因此应保留带有空格的文件名。

\0'; while read -A line; do mv "${line[2]}" "${line[1]%.*}.${line[2]}"; done < <(find -maxdepth 1 -type f -printf "%T+ %f\n"); IFS="$saveIFS"

对于 Bash(请注意读取选项和从零开始的索引而不是从一开始的索引的差异):

这些通过添加修改来重命名文件date 到原始文件名的开头,保留该文件名是为了防止名称冲突。

由此产生的文件名可能如下所示:

由于使用 null 作为内部字段分隔符 (IFS),因此应保留带有空格的文件名。

For zsh:

saveIFS="$IFS"; IFS=

For Bash (note the differences in the option to read and zero-based indexing instead of one-based):

saveIFS="$IFS"; IFS=

These rename files by adding the modification date to the beginning of the original filename, which is retained to prevent name collisions.

A filename resulting from this might look like:

2009-12-15+11:08:52.original.txt

Because a null is used as the internal field separator (IFS), filenames with spaces should be preserved.

\0'; while read -A line; do mv "${line[2]}" "${line[1]%.*}.${line[2]}"; done < <(find -maxdepth 1 -type f -printf "%T+ %f\n"); IFS="$saveIFS"

For Bash (note the differences in the option to read and zero-based indexing instead of one-based):


These rename files by adding the modification date to the beginning of the original filename, which is retained to prevent name collisions.

A filename resulting from this might look like:


Because a null is used as the internal field separator (IFS), filenames with spaces should be preserved.

\0'; while read -a line; do mv "${line[1]}" "${line[0]%.*}.${line[1]}"; done < <(find -maxdepth 1 -type f -printf "%T+\0%f\n"); IFS="$saveIFS"

These rename files by adding the modification date to the beginning of the original filename, which is retained to prevent name collisions.

A filename resulting from this might look like:

Because a null is used as the internal field separator (IFS), filenames with spaces should be preserved.

\0'; while read -A line; do mv "${line[2]}" "${line[1]%.*}.${line[2]}"; done < <(find -maxdepth 1 -type f -printf "%T+ %f\n"); IFS="$saveIFS"

For Bash (note the differences in the option to read and zero-based indexing instead of one-based):

These rename files by adding the modification date to the beginning of the original filename, which is retained to prevent name collisions.

A filename resulting from this might look like:

Because a null is used as the internal field separator (IFS), filenames with spaces should be preserved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文