首先,我不相信你。 ls ../images 只会输出基本名称,例如:
a.png
b.png
...etc...
不同的 命令ls ../images/* 可以提供您显示的输出。
虽然在使用来自 GNU coreutils 的 ls 的系统上,如 Linux,如果输出是终端并且文件名很短,就像你展示的那样,ls 没有选项会做多列输出更像:
../images/aaaaa.png ../images/ddddd.png ../images/ggggg.png
../images/b.png ../images/eeeee.png ../images/hhhhhh.png
../images/ccccc.png ../images/f.png ../images/iiiiii.png
除非你有一个别名(或阴影函数或脚本)强制-1(一个)选项来防止这种情况。或者实际名称(显着)更长,因为它们可能应该是。 或者你正在管道到某个东西,甚至是cat,因为ls 的输出是管道而不是终端。这些细节很重要。
在某些情况下 sed 和 awk 一样好,而且通常更简洁:
ls | sed 's/^//'
# sed processes a string of command(s) for each line read from stdin,
# and writes the result to stdout (by default, -n overrides this)
# s/old/new/ is string replacement using regex (which can be complex)
# ^ in old matches the beginning of the line
# $ in old matches the end of the line
# this assumes none of your filenames contains a newline character
# which Unix permits, but is rarely done because it is quite confusing
或因为你喜欢 printf 它可以完成整个工作:
printf '\n' ../images/*
# printf takes a format string containing any mixture of literal text
# (including backslash+letter escapes mostly the same as C et succ)
# and conversion specifiers beginning with % which each interpolate one argument
# it repeats the format as needed to handle all the arguments, so here
# with one c.s. it repeats the format once for each argument = filename
# this 'works' even if a filename contains newline
# (but I don't think the result will work correctly as markdown)