Linux Bash脚本:如何获取没有路径的文件?

m.s*_*tos 2 linux bash ls path

我试图在Linux中编写一个非常简单的脚本.
我先告诉你代码:

#!/bin/bash
# The shell program uses glob constructs and ls
# to list all entries in testfiles, that have 2
# or more dots "." in their name.

ls -l /path/to/file/*.*.*
Run Code Online (Sandbox Code Playgroud)

当我使用bash myscript命令运行此代码时,我得到类似:/ path/to/file/file.with.three.dots

但我不想要这个.我只想显示文件名,而不是路径.
然后我尝试了:

ls -l *.*.*
Run Code Online (Sandbox Code Playgroud)

但是这次只显示我在/ path/to/file /中的文件.
如何设置路径,所以当从任何地方运行脚本时,它会将/ path /中的文件名输出到/ file /?

谢谢!

Joh*_*136 7

basename path/to/file.b.c 应该给你file.bc

无论如何重新阅读这个问题,我认为这是一个暂时cd的路径然后ls可能会更好:

(cd /path/to/file; ls -l *.*.*)
Run Code Online (Sandbox Code Playgroud)

  • @AaronDigulla,使用`()`而不是`{}`没有任何副作用:当子shell结束时,`cd`的效果消失了. (2认同)