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 \;
相关推荐
難釋懷10 分钟前
Shell脚本-while循环语法结构
linux·运维·服务器·bash
农民也会写代码11 分钟前
dedecms织梦arclist标签noflag属性过滤多个参数
开发语言·数据库·sql·php·dedecms
内网渗透29 分钟前
Python 虚拟环境管理:venv 与 conda 的选择与配置
开发语言·python·conda·虚拟环境·venv
每次的天空1 小时前
kotlin与MVVM结合使用总结(三)
开发语言·microsoft·kotlin
難釋懷1 小时前
Shell脚本-for循环应用案例
linux·运维·服务器·bash
keep intensify2 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法
ephemerals__2 小时前
【c++11】c++11新特性(下)(可变参数模板、default和delete、容器新设定、包装器)
开发语言·c++
先生沉默先2 小时前
c#接口_抽象类_多态学习
开发语言·学习·c#
沐土Arvin2 小时前
理解npm的工作原理:优化你的项目依赖管理流程
开发语言·前端·javascript·设计模式·npm·node.js
IT瘾君2 小时前
Java基础:认识注解,模拟junit框架
java·开发语言·junit