且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用'find'返回不带扩展名的文件名

更新时间:2022-05-31 08:45:03

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

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 ';'