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 \;
相关推荐
老华带你飞6 小时前
博物馆展览门户|基于Java博物馆展览门户系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端
liulilittle6 小时前
FileStream C++
开发语言·c++·cocoa
点PY6 小时前
C++ 中 std::async 和 std::future 的并发性
java·开发语言·c++
不会代码的小猴6 小时前
C++的第九天笔记
开发语言·c++·笔记
CoderYanger7 小时前
Java SE——12.异常(≠错误)《干货笔记》
java·开发语言
Data_agent7 小时前
1688获得1688店铺所有商品API,python请求示例
java·开发语言·python
一晌小贪欢7 小时前
【Python办公】-图片批量添加文字水印(附代码)
开发语言·python·图片水印·python水印·python添加水印·图片添加水印
Yeats_Liao7 小时前
CANN Samples(十三):Ascend C 算子开发入门
c语言·开发语言
越来越无动于衷7 小时前
Java 实现 WebService(SOAP)联网调用:从原理到实战
java·开发语言
悦悦子a啊7 小时前
将学生管理系统改造为C/S模式 - 开发过程报告
java·开发语言·算法