gpt4 book ai didi

Linux Bash 脚本 : How to get file without path?

转载 作者:太空狗 更新时间:2023-10-29 11:25:20 25 4
gpt4 key购买 nike

我正在尝试在 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/*.*.*

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

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

ls -l *.*.*

但这次是向我显示文件,前提是我在/path/to/file/中。
我怎样才能设置路径,所以当从任何地方运行脚本时,它会输出/path/to/file/中的文件名?

谢谢!

最佳答案

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

但是重新阅读这个问题,我认为临时 cd 路径然后 ls 可能会更好:

(cd /path/to/file; ls -l *.*.*)

关于Linux Bash 脚本 : How to get file without path?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14721264/

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