列出目录中的文件而不列出当前目录中的文件

aec*_*iki 3 ls grep sed find

假设我*.txt在目录中有一个文件txtpath=/path/to/txt/

我想列出*.txt文件$txtpath从当前目录(不$txtpath)也没有列出当前目录下的文件,因为如果我执行情况ls *.txt $txtpath

我设法*.txt$txtpath使用此命令列出文件:find $txtpath -name '*.txt' | sed 's/\// /g' | grep -o '[^ ]*$'

但也许有更优雅的解决方案?

Ste*_*itt 5

只需指定完整路径和模式:

ls -d -- "${txtpath}"/*.txt
Run Code Online (Sandbox Code Playgroud)

虽然实际上列表是由 shell globbing 完成的,但ls最终只是打印了它从 shell 接收到的参数。你不妨printf在这里使用:

printf '%s\n' "${txtpath}"/*.txt
Run Code Online (Sandbox Code Playgroud)

txt在目录中没有非隐藏文件的情况下,这将给出相同的结果,除了(在不匹配的 glob 时不取消命令的 shell 中)。