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 \;
相关推荐
明道源码几秒前
Kotlin 控制流、函数、Lambda、高阶函数
android·开发语言·kotlin
橙子19911016几秒前
在 Kotlin 中,ViewModel 的获取
开发语言·vue.js·kotlin
脚踏实地的大梦想家3 分钟前
【Go】P8 Go 语言核心数据结构:深入解析切片 (Slice)
开发语言·数据结构·golang
hweiyu003 分钟前
Gradle 构建脚本迁移:从 Groovy DSL 到 Kotlin DSL,语法与技巧对比
开发语言·kotlin·gradle
Tony Bai18 分钟前
【Go 网络编程全解】13 从 HTTP/1.1 到 gRPC:Web API 与微服务的演进
开发语言·网络·http·微服务·golang
峥嵘life27 分钟前
Android EDLA开发认证说明和开发流程
开发语言·1024程序员节
刘新明19891 小时前
算法还原案例4-OLLVM_MD5
开发语言·前端·javascript·1024程序员节
wjs20241 小时前
空对象模式(Null Object Pattern)
开发语言
Cherry Zack1 小时前
FastAPI 入门指南 :基础概念与核心特性
开发语言·python·fastapi·1024程序员节
没有bug.的程序员2 小时前
Spring Boot 起步:自动装配的魔法
java·开发语言·spring boot·后端·spring·1024程序员节