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 \;
相关推荐
ujainu小5 分钟前
原生性能优化:6变量实现高效桥接
开发语言·华为·性能优化·harmonyos
甄同学10 分钟前
第十七篇:Bash Executor命令执行器,安全运行Shell命令
开发语言·安全·bash
张32311 分钟前
Go语言基础 Map 函数值 闭包
开发语言·golang
杜子不疼.31 分钟前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++
有点。36 分钟前
C++深度优先搜索(DFS)的概念(一)
开发语言·c++·深度优先
时间的拾荒人41 分钟前
C语言编译与链接:从源码到可执行程序的完整解析
c语言·开发语言
人道领域43 分钟前
【0-1的agent进阶篇】Prompt 与上下文工程
java·开发语言·prompt·mcp
aaPIXa62244 分钟前
C++大型项目模块化拆分实战记录
开发语言·c++
z落落1 小时前
C# WinForm 线程池与事件等待+进度条暂停恢复实战案例
开发语言·c#
石山代码1 小时前
C++23 新特性在 CLion 中的实战体验
开发语言·c++·c++23