根据MACOS上的ZSH的某些模式移动文件

发布于 2025-01-17 20:52:26 字数 467 浏览 2 评论 0原文

我正在尝试根据某种模式移动文件,在下面的情况下,将包含 2018 的所有文件移动到 2018/ 文件夹:

mv *2018* 2018/

此命令仅适用于 2017,现在我得到了

mv:将 2018 重命名为 2018/2018:参数无效

所以我尝试了 zmv

autoload -Uz zmv
mv *2018* 2018/

它有效,但对于 2019 或具有相同错误消息的任何其他值不再有效。

我应该如何修复它?或者什么是根据某些正则表达式模式移动文件的更好且简单的方法?

我还安装了oh-my-zsh

I'm trying to move files according to some pattern, in the case below, moving all files containing 2018 to 2018/ folder:

mv *2018* 2018/

This command worked once with 2017, now then I'm getting

mv: rename 2018 to 2018/2018: Invalid argument

So I tried zmv:

autoload -Uz zmv
mv *2018* 2018/

It worked, but doesn't work anymore for 2019 or any other value with the same error message.

How should I fix it ? Or what is a better and yet simple way to move files according to some regex pattern ?

I also have oh-my-zsh installed

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

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

发布评论

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

评论(1

有木有妳兜一样 2025-01-24 20:52:26

试试这个:

mv *2018*(.) 2018/

使用 . glob 限定符(在括号中),通配符模式将仅匹配常规文件。如果没有限定符,源模式将在要移动的项目列表中包含目标目录 2018,并且 mv 无法将目录移动到其自身中。

当我在我的系统上尝试原始模式 *2018* 时,mv 仍然按照请求移动匹配的文件,然后报告有关目录的错误消息。我怀疑当这似乎起作用时,错误消息只是在其他文本中丢失了。

演示 . glob 限定符:

>>>> ls -p1
2017/
2017a.txt
2017b.txt
2018/
2018a.txt
2018b.txt
2019/
2019a.txt
2019b.txt
>>>> print -l *2018*   
2018
2018a.txt
2018b.txt
>>>> print -l *2018*(.)
2018a.txt
2018b.txt
>>>> mv *2018*(.) 2018/
>>>> ls -pR1           
2017/
2017a.txt
2017b.txt
2018/
2019/
2019a.txt
2019b.txt

./2017:

./2018:
2018a.txt
2018b.txt

./2019:

顺便说一句,您问题中的 zmv 示例正在调用 mv,而不是 zmv。另外,zmv 的参数通常需要加引号。您不需要在这里使用 zmvmv 会完成所需的一切。

Try this:

mv *2018*(.) 2018/

With the . glob qualifier (in the parentheses), the wildcard pattern will only match regular files. Without the qualifier, the source pattern will include the destination directory 2018 in the list of items to move, and mv cannot move a directory into itself.

When I tried the original pattern *2018* on my system, mv still moved the matching files as requested, and then reported the error message about the directory. I suspect that when this appeared to work, the error message was simply lost in some other text.

Demonstrating the . glob qualifier:

>>>> ls -p1
2017/
2017a.txt
2017b.txt
2018/
2018a.txt
2018b.txt
2019/
2019a.txt
2019b.txt
>>>> print -l *2018*   
2018
2018a.txt
2018b.txt
>>>> print -l *2018*(.)
2018a.txt
2018b.txt
>>>> mv *2018*(.) 2018/
>>>> ls -pR1           
2017/
2017a.txt
2017b.txt
2018/
2019/
2019a.txt
2019b.txt

./2017:

./2018:
2018a.txt
2018b.txt

./2019:

BTW, the zmv example in your question is invoking mv, not zmv. Also, arguments to zmv usually need to be quoted. You don't want zmv here, mv will do everything needed.

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