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

Pou*_*del 4 python bash shell markdown jupyter-notebook

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

当我做:

ls ../images

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

Run Code Online (Sandbox Code Playgroud)

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

我试过:

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

# am not sure what to do next
Run Code Online (Sandbox Code Playgroud)

所需输出

![](../images/a.png)
![](../images/b.png)
Run Code Online (Sandbox Code Playgroud)

感谢帮助。

Dud*_*Boy 5

最简单的解决方案是使用标准 posixfind命令和printf如下操作:

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

解释:

../images- 目标目录

"*.png" - 全局模式

-printf- 格式化动作以输出

'![](%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)
Run Code Online (Sandbox Code Playgroud)