gpt4 book ai didi

python - 如何在 bash 中将前缀组合到 ls 的输出?

转载 作者:行者123 更新时间:2023-11-28 20:54:54 27 4
gpt4 key购买 nike

假设我在父目录中有一些图像:../imagea/a.png 和 ../images/b.png。

当我这样做时:

ls ../images

# I get
../images/a.png
../images/b.png

如何为所有这些输出添加前缀 ![]( 和后缀 )

我试过:

!ls ../images/*.png | cat

# am not sure what to do next

要求的输出

![](../images/a.png)
![](../images/b.png)

感谢您的帮助。

最佳答案

最简单的解决方案是使用标准的 posix find 命令和 printf 操作,如下所示:

 find ../images -name "*.png" -printf '![](%p)\n'

说明:

../images - 目标目录

"*.png" - 全局模式

-printf - 输出格式化 Action

'![](%p)\n' - 格式化参数以输出完整路径名。

示例:

$ ls -l
total 8
drwxrwxr-x. 3 cfrm cfrm 15 Dec 13 2018 app
drwxrwxr-x. 2 cfrm cfrm 23 Mar 24 2019 app_5811_install
drwxrwxr-x. 13 cfrm cfrm 4096 Dec 12 2018 app_58_install
drwxrwxr-x. 2 cfrm cfrm 4096 Oct 3 2018 grants
-rwx------. 1 cfrm cfrm 526 Feb 17 2019 ic_start_all.sh
-rwx------. 1 cfrm cfrm 920 Oct 4 2018 ic_status_all.sh
-rwx------. 1 cfrm cfrm 984 Sep 27 2018 ic_stop_all.sh
-rwxrwxr-x. 1 cfrm cfrm 1693 Dec 13 2018 loadTrigger.sh



$ find . -name "*.sh" -printf '![](%p)\n'
![](./ic_stop_all.sh)
![](./ic_status_all.sh)
![](./loadTrigger.sh)
![](./ic_start_all.sh)

关于python - 如何在 bash 中将前缀组合到 ls 的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com