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 \;
相关推荐
C^h15 小时前
c++ string
开发语言·c++
raindayinrain15 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的16 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao16 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
学逆向的17 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的17 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”17 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
小王C语言17 小时前
Qt常用控件:容器类控件,标题分组框、标签页(浏览器标签)
开发语言·qt
我是唐青枫18 小时前
Kotlin Delegation 委托详解
开发语言·kotlin
qq_4069810318 小时前
共享变量一 关于extern与const
开发语言·c++