仅显示文件名而不显示整个目录路径
ls /home/user/new/*.txt
打印该目录中的所有 txt 文件。但是它打印输出如下:
[me@comp]$ ls /home/user/new/*.txt
/home/user/new/file1.txt /home/user/new/file2.txt /home/user/new/file3.txt
等等。
我想运行 ls 命令而不是从 /home/user/new/
目录运行,因此我必须提供完整的目录名称,但我希望输出只是因为
[me@comp]$ ls /home/user/new/*.txt
file1.txt file2.txt file3.txt
我不想要整个路径。只需要文件名。这个问题必须使用 ls 命令来解决,因为它的输出是针对另一个程序的。
ls /home/user/new/*.txt
prints all txt files in that directory. However it prints the output as follows:
[me@comp]$ ls /home/user/new/*.txt
/home/user/new/file1.txt /home/user/new/file2.txt /home/user/new/file3.txt
and so on.
I want to run the ls
command not from the /home/user/new/
directory thus I have to give the full directory name, yet I want the output to be only as
[me@comp]$ ls /home/user/new/*.txt
file1.txt file2.txt file3.txt
I don't want the entire path. Only filename is needed. This issues has to be solved using ls command, as its output is meant for another program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
ls 无论你想要什么 | xargs -n 1 basename
这对你有用吗?
否则你可以
(cd /the/directory && ls)
(是的,括号是有意的)ls whateveryouwant | xargs -n 1 basename
Does that work for you?
Otherwise you can
(cd /the/directory && ls)
(yes, parentheses intended)不需要 Xargs 和 all , ls 就足够了。
按行显示
No need for Xargs and all , ls is more than enough.
displays row wise
有多种方法可以实现这一目标。其中之一是这样的:
There are several ways you can achieve this. One would be something like:
使用
basename
命令:Use the
basename
command:(cd dir && ls)
只会输出 dir 中的文件名。如果您想要每行一个,请使用
ls -1
。(根据 Sactiw 的评论,将 ; 更改为 &&)。
(cd dir && ls)
will only output filenames in dir. Use
ls -1
if you want one per line.(Changed ; to && as per Sactiw's comment).
您可以将 sed 脚本添加到命令行:
you could add an sed script to your commandline:
解决这个问题的一个奇特方法是使用两次“rev”和“cut”:
A fancy way to solve it is by using twice "rev" and "cut":
所选的答案对我不起作用,因为我的文件名中有空格、引号和其他奇怪的字符。要引用
basename
的输入,您应该使用:无论文件被调用什么,这都保证可以工作。
The selected answer did not work for me, as I had spaces, quotes and other strange characters in my filenames. To quote the input for
basename
, you should use:This is guaranteed to work, regardless of what the files are called.
我更喜欢 fge 已经回答的基本名称。
另一种方法是:
一种更丑陋的方法是:
I prefer the base name which is already answered by fge.
Another way is :
one more ugly way is :
只是希望对某人有所帮助,因为老问题似乎时不时地出现,我总是在这里找到好的建议。
我的问题是在文本文件中列出某个目录中“*.txt”文件的所有名称,不带路径,也不带 Datastage 7.5 序列的扩展名。
我们使用的解决方案是:
just hoping to be helpful to someone as old problems seem to come back every now and again and I always find good tips here.
My problem was to list in a text file all the names of the "*.txt" files in a certain directory without path and without extension from a Datastage 7.5 sequence.
The solution we used is:
这是另一种方法:
Here is another way:
我们可以通过很多方法来做到这一点,您可以尝试遵循。
另一种方法:
There are lots of way we can do that and simply you can try following.
Another method:
您还可以通过管道连接到 grep 并提取最后一个正斜杠之后的所有内容。它看起来很愚蠢,但我认为防御性 grep 应该没问题,除非(像某种疯子)你的文件名中有正斜杠。
You could also pipe to grep and pull everything after the last forward slash. It looks goofy, but I think a defensive grep should be fine unless (like some kind of maniac) you have forward slashes within your filenames.
带有
basename
的find
s-exec
参数非常适合此任务,不需要xargs
或子 shell:您可以使用
-type d
或-type f
仅过滤文件或文件夹。find
s-exec
argument withbasename
is perfect for this task, no need forxargs
or subshells:You can use
-type d
or-type f
to filter only files or folders.当您想要列出路径中的名称但它们具有不同的文件扩展名时。
me@server:/var/backups$ ls -1 *.zip && ls -1 *.gz
When you want to list names in a path but they have different file extensions.
me@server:/var/backups$ ls -1 *.zip && ls -1 *.gz