bash 在 Mac 终端中循环文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31797856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:03:17  来源:igfitidea点击:

Loop through files in Mac terminal

macosbashunix

提问by NikosDim

I am trying to write a simple loop that will loop through the files the current directory and just print the file names.

我正在尝试编写一个简单的循环,它将遍历当前目录中的文件并只打印文件名。

I tried that:

我试过了:

#!/bin/bash    
FILES=/Users/nick/Desktop/*.jpg
    for f in $FILES
    do
        echo $f
    done

but it didn't work. Running ./script it just printed "/Users/nick/Desktop/*.jpg". No errors

但它没有用。运行 ./script 它只是打印“/Users/nick/Desktop/*.jpg”。没有错误

I am running this on a MacBook Pro 10.10.4

我在 MacBook Pro 10.10.4 上运行它

Thanks

谢谢

回答by Leonid Usov

for f in /Users/nick/Desktop/*.jpg; do echo $f; done

EditActually I think that this commentof @KyleBurton is very clever and should be taken into an account, since it explains why a result like that could be observed.

编辑实际上我认为@KyleBurton 的这个评论非常聪明,应该考虑在内,因为它解释了为什么可以观察到这样的结果。

回答by Abdul

Try this, please:

试试这个,请:

for f in $(ls /Users/nick/Desktop/*.jpg); 
do echo $f; 
done

回答by Manikandan

You can use simple find command to get all things, with type file ..

您可以使用简单的 find 命令来获取所有内容,类型为 file ..

find . type f