bash 使用ls,如何列出文件而不打印扩展名(点后面的部分)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13676108/
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
Using ls, how to list files without printing the extension (the part after the dot)?
提问by Yishu Fang
Suppose I have a directory with some files:
假设我有一个包含一些文件的目录:
$ ls
a.c b.c e.c k.cpp s.java
How can I display the result without the file extension(the part following the dot, including that dot)? Like this:
如何在没有文件扩展名的情况下显示结果(点后面的部分,包括那个点)?像这样:
$ <some command>
a
b
e
k
s
回答by Dyno Fu
using sed?
使用sed?
ls -1 | sed -e 's/\..*$//'
回答by jim mcnamara
ls | while read fname
do
echo ${fname%%.*}
done
Try that.
试试那个。
回答by runlevel0
ls -a | cut -d "." -f 1
man (1) cut
男人 (1) 切
Very handy, the -d switch defines the delimiter and the -f which field you want.
非常方便, -d 开关定义了分隔符和 -f 您想要的字段。
EDIT: Include riverfall'sscenario is also piece of cake as cut can start also from the end, though the logic is somewhat different. Here an example with a bunch of files with random names, some with two dots, some with a single dot and some without extension:
编辑:包括Riverfall 的场景也是小菜一碟,因为 cut 也可以从最后开始,尽管逻辑有些不同。这是一个带有随机名称的一堆文件的示例,一些带有两个点,一些带有一个点,一些没有扩展名:
runlevel0@ubuntu:~/test$ ls
test.001.rpx test.003.rpx test.005.rpx test.007.rpx test.009.rpx testxxx
test.002.rpx test.004.rpx test.006.rpx test.008.rpx test_nonum test_xxx.rtv
runlevel0@ubuntu:~/test$ ls | cut -d "." -f -2
test.001
test.002
test.003
test.004
test.005
test.006
test.007
test.008
test.009
test_nonum
testxxx
test_xxx.rtv
Using the minus beforethe field number makes it eliminate all BUT the indicated fields (1,2 in this case) and putting it behind makes it start counting from the end.
在字段编号之前使用减号可以消除所有 BUT 指示的字段(在本例中为 1,2)并将其放在后面使其从末尾开始计数。
This same notation can be used for offset and characters besides of fields (see the man page)
除了字段之外,这个相同的符号还可以用于偏移量和字符(请参阅手册页)
回答by Paul Parker
for f in *; do printf "%s\n" ${f%%.*}; done
${string%%substring}
Deletes longest match of $substring from back of $string.
${string%%substring}
从 $string 后面删除 $substring 的最长匹配项。
This would handle mypackage.pkg.tar.xz
--> mypackage
for instance.
这将处理mypackage.pkg.tar.xz
-->mypackage
例如。
In contrast:
相比之下:
${string%substring} Deletes shortest match of $substring from back of $string.
${string%substring} 从 $string 的后面删除 $substring 的最短匹配项。
That is ${string%substring}
would only delete the final extension, i.e.
mypackage.pkg.tar.xz
--> mypackage.pkg.tar
即${string%substring}
只会删除最后的扩展名,即
mypackage.pkg.tar.xz
-->mypackage.pkg.tar
On a side note, use printf
preferentially to echo
. The syntax is a little more complex, but it will work on a wider variety of systems.
另外,请printf
优先使用to echo
。语法稍微复杂一些,但它适用于更广泛的系统。
If you only want to see files, not directories:
如果您只想查看文件而不是目录:
for f in *; do if [[ -f ${f} ]]; then printf "%s\n" ${f%%.*}; fi; done