bash find: get directory of found file

With GNU find:

复制代码
find . -name foo.mp4 -printf '%h\n'

With other finds, provided directory names don't contain newline characters:

复制代码
find . -name foo.mp4 |
  LC_ALL=C sed 's|/[^/]*$||'

Or:

复制代码
find . -name foo.mp4 -exec dirname {} \;

though that means forking a process and running one dirname command per file.

If you need to run a command on that path, you can do (standard syntax):

复制代码
find . -name "featured.mp4" -exec sh -c '
  for file do
    dir=${file%/*}
    ffmpeg -i "$file" -c:v libvpx -b:v 1M -c:a libvorbis "$dir" featured.webm
  done' sh {} +

Though in this case, you may be able to use -execdir (a BSD extension also available in GNU find), which chdir()s to the file's directory:

复制代码
find . -name "featured.mp4" -execdir \
  ffmpeg -i {} -c:v libvpx -b:v 1M -c:a libvorbis . featured.webm \;
相关推荐
_院长大人_30 分钟前
设计模式-工厂模式
java·开发语言·设计模式
MATLAB代码顾问33 分钟前
MATLAB实现决策树数值预测
开发语言·决策树·matlab
不染尘.2 小时前
2025_11_7_刷题
开发语言·c++·vscode·算法
似水এ᭄往昔2 小时前
【C++】--stack和queue
开发语言·c++
csbysj20202 小时前
R 绘图 - 散点图
开发语言
会跑的兔子3 小时前
Android 16 Kotlin协程 第一部分
android·开发语言·kotlin
Js_cold3 小时前
Verilog函数function
开发语言·fpga开发·verilog
我是苏苏3 小时前
C#基础:如何从现有类库复制一个新的类库,并且加入解决方案
开发语言·c#
算法与编程之美3 小时前
理解Java finalize函数
java·开发语言·jvm·算法
lkbhua莱克瓦243 小时前
Java基础——常用算法5
java·开发语言·笔记·github