[英]Linux Bash Script: How to get file without path?
I am trying to write a very very simple script in Linux. 我试图在Linux中编写一个非常简单的脚本。
Let me show you the code first: 我先告诉你代码:
#!/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 myscript
command, 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/. 但是这次只显示我在/ 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 /中的文件名输出到/ file /?
Thank you! 谢谢!
basename path/to/file.bc
should give you file.bc basename path/to/file.bc
应该给你file.bc
However re-reading the question, I think a temporary cd
to the path and then an ls
may be better: 无论如何重新阅读这个问题,我认为临时cd
到路径然后是ls
可能会更好:
(cd /path/to/file; ls -l *.*.*)
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. 现在说明:您列出您选择的文件,然后使用awk。 Switch -F will determine character you use for split (/ in this case). Switch -F将确定用于拆分的字符(在这种情况下为/)。 Then you print with awk the value of "$NF", which means "the last one". 然后用awk打印“$ NF”的值,这意味着“最后一个”。 So you have: /path/to/file/file.with.three.dots. 所以你有:/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. 拆分它,取最后一个(file.with.three.dots)并打印它(无论你的路径有多长/多深)并且无需改变你在文件系统上的当前位置。
I really hope, I've helped. 我真的希望,我帮了。
或者使用find命令。
$> find /path/to/file -printf %f\\n\\r
I suggest sticking with basename. 我建议坚持使用basename。
ls -1 /path/to/file/*.*.* | while read path
do
basename "$path"
done
Its best to use while read rather 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 / )*中的路径时使用,因为如果您的路径恰好在其中有空格,则for循环将对路径进行分段。
I used a variation on Zapatero's technique to capture the base file name in an environment variable. 我使用了Zapatero技术的变体来捕获环境变量中的基本文件名。 $1
is the command-line argument that, by definition in my script, is the full path to the filename in question. $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 ls
returns them. 请注意,如果完整路径包含通配符并返回多个结果,则basename变量将设置为“last”匹配文件名,无论ls
返回它们的顺序。
basename=$(ls -1 "$1" | while read path; do basename "$path"; done)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.