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 \;
相关推荐
独行soc3 分钟前
2025年渗透测试面试题总结-安恒[实习]安全工程师(题目+回答)
运维·开发语言·经验分享·网络安全·面试·渗透测试·php
kokotao4 分钟前
使用Java实现Navicat密码的加密与解密
java·开发语言·数据库·mysql
c无序7 分钟前
【Go-2】基本语法与数据类型
开发语言·后端·golang
傻傻虎虎27 分钟前
【QT】ModbusTCP读写寄存器类封装
开发语言·数据库·qt
独行soc34 分钟前
2025年渗透测试面试题总结-华顺信安[实习]安全服务工程师(题目+回答)
运维·开发语言·学习·安全·面试·渗透测试·php
王RuaRua1 小时前
[数据结构]6. 队列-Queue
开发语言·数据结构·算法·leetcode
敲键盘的小夜猫1 小时前
如何理解大模型的幻觉输出及RAG技术的应用与实战案例
开发语言·python
恒者走天下1 小时前
c++学习方向选择说明
开发语言·c++·学习
zhangpeng4555479401 小时前
C++编程起步项目
开发语言·前端·c++
KeithTsui2 小时前
C语言之 比特(bit)、字节(Byte)、字(Word)、整数(Int)
linux·c语言·开发语言·c++·算法·word