仅显示文件名而不显示整个目录路径

发布于 2024-12-21 08:59:45 字数 465 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

冰火雁神 2024-12-28 08:59:45

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)

很糊涂小朋友 2024-12-28 08:59:45

不需要 Xargs 和 all , ls 就足够了。

ls -1 *.txt

按行显示

No need for Xargs and all , ls is more than enough.

ls -1 *.txt

displays row wise

抹茶夏天i‖ 2024-12-28 08:59:45

有多种方法可以实现这一目标。其中之一是这样的:

for filepath in /path/to/dir/*
do
    filename=$(basename $filepath)

    ... whatever you want to do with the file here
done

There are several ways you can achieve this. One would be something like:

for filepath in /path/to/dir/*
do
    filename=$(basename $filepath)

    ... whatever you want to do with the file here
done
悸初 2024-12-28 08:59:45

使用basename命令:

basename /home/user/new/*.txt

Use the basename command:

basename /home/user/new/*.txt
高跟鞋的旋律 2024-12-28 08:59:45

(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).

若有似无的小暗淡 2024-12-28 08:59:45

您可以将 sed 脚本添加到命令行:

ls /home/user/new/*.txt | sed -r 's/^.+\///'

you could add an sed script to your commandline:

ls /home/user/new/*.txt | sed -r 's/^.+\///'
擦肩而过的背影 2024-12-28 08:59:45

解决这个问题的一个奇特方法是使用两次“rev”和“cut”:

find ./ -name "*.txt" | rev | cut -d '/' -f1 | rev

A fancy way to solve it is by using twice "rev" and "cut":

find ./ -name "*.txt" | rev | cut -d '/' -f1 | rev
╄→承喏 2024-12-28 08:59:45

所选的答案对我不起作用,因为我的文件名中有空格、引号和其他奇怪的字符。要引用 basename 的输入,您应该使用:

ls /path/to/my/directory | xargs -n1 -I{} 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:

ls /path/to/my/directory | xargs -n1 -I{} basename "{}"

This is guaranteed to work, regardless of what the files are called.

好倦 2024-12-28 08:59:45

我更喜欢 fge 已经回答的基本名称。
另一种方法是:

ls /home/user/new/*.txt|awk -F"/" '{print $NF}'

一种更丑陋的方法是:

ls /home/user/new/*.txt| perl -pe 's/\//\n/g'|tail -1

I prefer the base name which is already answered by fge.
Another way is :

ls /home/user/new/*.txt|awk -F"/" '{print $NF}'

one more ugly way is :

ls /home/user/new/*.txt| perl -pe 's/\//\n/g'|tail -1
[旋木] 2024-12-28 08:59:45

只是希望对某人有所帮助,因为老问题似乎时不时地出现,我总是在这里找到好的建议。

我的问题是在文本文件中列出某个目录中“*.txt”文件的所有名称,不带路径,也不带 Datastage 7.5 序列的扩展名。

我们使用的解决方案是:

ls /home/user/new/*.txt | xargs -n 1 basename | cut -d '.' -f1 > name_list.txt

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:

ls /home/user/new/*.txt | xargs -n 1 basename | cut -d '.' -f1 > name_list.txt
故笙诉离歌 2024-12-28 08:59:45

这是另一种方法:

ls -1 /home/user/new/*.txt|rev|cut -d'/' -f1|rev

Here is another way:

ls -1 /home/user/new/*.txt|rev|cut -d'/' -f1|rev
再浓的妆也掩不了殇 2024-12-28 08:59:45

我们可以通过很多方法来做到这一点,您可以尝试遵循。

ls /home/user/new | tr '\n' '\n' | grep .txt

另一种方法:

cd /home/user/new && ls *.txt

There are lots of way we can do that and simply you can try following.

ls /home/user/new | tr '\n' '\n' | grep .txt

Another method:

cd /home/user/new && ls *.txt
浅忆 2024-12-28 08:59:45

您还可以通过管道连接到 grep 并提取最后一个正斜杠之后的所有内容。它看起来很愚蠢,但我认为防御性 grep 应该没问题,除非(像某种疯子)你的文件名中有正斜杠。

ls folderpathwithcriteria | grep -P -o -e "[^/]*$"

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.

ls folderpathwithcriteria | grep -P -o -e "[^/]*
quot;
未央 2024-12-28 08:59:45

带有 basenamefinds -exec 参数非常适合此任务,不需要 xargs 或子 shell:

find "$path" -maxdepth 1 -mindepth 1 -exec basename {} \;

您可以使用 -type d-type f 仅过滤文件或文件夹。

finds -exec argument with basename is perfect for this task, no need for xargs or subshells:

find "$path" -maxdepth 1 -mindepth 1 -exec basename {} \;

You can use -type d or -type f to filter only files or folders.

少年亿悲伤 2024-12-28 08:59:45

当您想要列出路径中的名称但它们具有不同的文件扩展名时。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文