使用'find'返回不带扩展名的文件名 [英] Using 'find' to return filenames without extension

查看:507
本文介绍了使用'find'返回不带扩展名的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录(包含子目录),我想在其中查找所有扩展名为".ipynb"的文件.但是我希望查找"命令只将这些文件名返回我,而不带扩展名.

I have a directory (with subdirectories), of which I want to find all files that have a ".ipynb" extension. But I want the 'find' command to just return me these filenames without the extension.

我知道第一部分:

find . -type f -iname "*.ipynb" -print    

但是,如何获得不带"ipynb"扩展名的名称呢? 任何回复都非常感谢...

But how do I then get the names without the "ipynb" extension? Any replies greatly appreciated...

推荐答案

要仅返回不带扩展名的文件名,请尝试:

To return only filenames without the extension, try:

find . -name "*.ipynb" -execdir sh -c 'printf "%s\n" "${0%.*}"' {} ';'

或:

find "$PWD" -type f -iname "*.ipynb" -execdir basename {} .ipynb ';'

或:

find . -type f -iname "*.ipynb" -exec basename {} .ipynb ';'

但是在每个文件上调用basename效率低下,因此

however invoking basename on each file can be inefficient, so @CharlesDuffy suggestion is:

find . -name '*.ipynb' -exec bash -c 'printf "%s\n" "${@%.*}"' _ {} +

或:

find . -iname '*.ipynb' -execdir basename -s '.sh' {} +

使用 +意味着,我们正在将多个文件传递给每个bash实例,因此如果整个列表适合一个命令行,则我们仅调用bash一次.

要在同一行中打印完整路径和文件名(不带扩展名),请尝试:

To print full path and filename (without extension) in the same line, try:

find . -name "*.ipynb" -exec sh -c 'printf "%s\n" "${0%.*}"' {} ';'

或:

find "$PWD" -type f -iname "*.ipynb" -print | grep -o "[^\.]\+"


要在单独的行上打印完整路径和文件名:


To print full path and filename on separate lines:

find "$PWD" -type f -iname "*.ipynb" -exec dirname "{}" ';' -exec basename "{}" .ipynb ';'

这篇关于使用'find'返回不带扩展名的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆