猛击;将目录更改为以 2* 或 21* 开头的文件夹

发布于 2025-01-16 04:21:16 字数 280 浏览 3 评论 0原文

我有一系列文件夹,它们是根据创建日期和其他一些信息命名的;即

220310_AS
220307_DF
220228_1A
..
211228_QR
..
201224_HH

如何在 bash 脚本中循环遍历以 22 或 21 开头的文件夹? 这是我的代码的一部分,不起作用

..
for dd in */2*; do
    cd "$dd"
    #do something
    eval "ls"
    cd ..
..

I have a series of folders which are named based on the date of creation and some other information; i.e.

220310_AS
220307_DF
220228_1A
..
211228_QR
..
201224_HH

How can I in a bash script loop over folders which start with 22 or 21?
Here is part of my code which does not work

..
for dd in */2*; do
    cd "$dd"
    #do something
    eval "ls"
    cd ..
..

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

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

发布评论

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

评论(1

执笏见 2025-01-23 04:21:16

您的通配符 */2* 匹配原始目录下 2 级的目录。因此,要返回原始目录,您需要 cd ../.. 而不是 cd ..

您还可以在子 shell 中执行 cd,然后只需退出 shell 即可返回到原始目录。

for dd in */2*; do (
    cd "$dd"
    # do stuff
); done

Your wildcard */2* matches directories 2 levels below the original directory. So to get back to the original directory, you need cd ../.. instead of cd ...

You could also do the cd in a subshell, then simply exiting the shell will return you to the original directory.

for dd in */2*; do (
    cd "$dd"
    # do stuff
); done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文