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 \;
相关推荐
大数据追光猿2 分钟前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风1 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead1 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
风与沙的较量丶2 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
水煮庄周鱼鱼2 小时前
C# 入门简介
开发语言·c#
编程星空3 小时前
css主题色修改后会多出一个css吗?css怎么定义变量?
开发语言·后端·rust
软件黑马王子3 小时前
Unity游戏制作中的C#基础(6)方法和类的知识点深度剖析
开发语言·游戏·unity·c#
Logintern093 小时前
使用VS Code进行Python编程的一些快捷方式
开发语言·python
Multiple-ji3 小时前
想学python进来看看把
开发语言·python
一个小白13 小时前
C++——list模拟实现
开发语言·c++