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 \;
相关推荐
网域小星球3 分钟前
C 语言从 0 入门(十五)|综合小项目:菜单交互与简易功能实现
c语言·开发语言·交互
网域小星球12 分钟前
C 语言从 0 入门(十六)|动态内存管理:malloc /free/calloc /realloc 精讲
c语言·开发语言·free·malloc·动态内存
雪的季节20 分钟前
qt信号槽跨线程使用时候的坑
java·开发语言·qt
AI应用实战 | RE24 分钟前
011、向量数据库入门:Embeddings原理与ChromaDB实战
开发语言·数据库·langchain·php
一直不明飞行35 分钟前
C++:string,写法s.find(‘@‘) != s.end()是否有问题
开发语言·c++·算法
沐知全栈开发1 小时前
C 预处理器
开发语言
daad7771 小时前
WSL2_wifi驱动安装
开发语言·前端·javascript
超级大只老咪1 小时前
一维度前缀和解题通用模板(java)
java·开发语言·算法
无限进步_1 小时前
【C++】重载、重写和重定义的区别详解
c语言·开发语言·c++·ide·windows·git·github
许杰小刀1 小时前
Python网络请求库,从 requests 到 httpx
开发语言·python·httpx