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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14721264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:28:51  来源:igfitidea点击:

Linux Bash Script: How to get file without path?

linuxbashpathls

提问by m.spyratos

I am trying to write a very very simple script in Linux.
Let me show you the code first:

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

When I run this code with bash myscriptcommand, I get something like: /path/to/file/file.with.three.dots

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

But I don't want this. I want to show only the file name, not the path.
Then I tried:

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

ls -l *.*.*

But this time is shows me the files, only if I am inside the /path/to/file/.
How can I set the path, so when running the script from any place, it will output the name of the files in the /path/to/file/?

但这次是向我展示文件,只有当我在 /path/to/file/ 中时。
如何设置路径,以便从任何地方运行脚本时,它都会输出 /path/to/file/ 中的文件名?

Thank you!

谢谢!

回答by John3136

basename path/to/file.b.cshould give you file.b.c

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

However re-reading the question, I think a temporary cdto the path and then an lsmay be better:

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

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

回答by Rastislav Hasicek

Code first:

先码:

ls -l /path/to/file/*.*.* | awk -F '/' '{print $NF}'

Now explanation: You list file of your choice and then use awk on it. Switch -F will determine character you use for split (/ in this case). Then you print with awk the value of "$NF", which means "the last one". So you have: /path/to/file/file.with.three.dots. Split it, take the last one (file.with.three.dots) and print it (regardless how long/deep is your path) and without any need of changing your current position on file system.

现在解释:您列出您选择的文件,然后在其上使用 awk。开关 -F 将确定您用于拆分的字符(在本例中为 /)。然后用 awk 打印“$NF”的值,意思是“最后一个”。所以你有:/path/to/file/file.with.three.dots。拆分它,取最后一个(file.with.three.dots)并打印它(无论您的路径有多长/多深),无需更改您在文件系统上的当前位置。

I really hope, I've helped.

我真的希望,我已经有所帮助。

回答by Andy Jones

Alternatively use the find command.

或者使用 find 命令。

$> find /path/to/file -printf %f\n\r

回答by zapatero

I suggest sticking with basename.

我建议坚持使用 basename。

ls -1 /path/to/file/*.*.* | while read path
do
    basename "$path"
done

Its best to use while readrather than for path in $(ls /path/)* only because if your paths happen to have a space in them the for loop will segment the path.

最好在 $(ls /path/)* 中使用while read而不是for path ,因为如果您的路径碰巧在其中有一个空格,则 for 循环将分割路径。

回答by pbnelson

I used a variation on Zapatero's technique to capture the base file name in an environment variable. $1is the command-line argument that, by definition in my script, is the full path to the filename in question.

我使用了 Zapatero 技术的变体来捕获环境变量中的基本文件名。$1是命令行参数,根据我的脚本中的定义,它是相关文件名的完整路径。

Note that if the full path contains wildcards and returns multiple results, the basename variable will be set to the "last" matching filename, in whatever order lsreturns them.

请注意,如果完整路径包含通配符并返回多个结果,则 basename 变量将设置为“最后一个”匹配文件名,无论ls返回它们的顺序如何。

basename=$(ls -1 "" | while read path; do basename "$path"; done)