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 \;
相关推荐
white-persist18 小时前
二进制movl及CTF逆向GDB解析:Python(env)环境下dbg从原理到实战
linux·服务器·开发语言·python·网络安全·信息可视化·系统安全
脏脏a18 小时前
类和对象(下):初始化列表、静态成员与友元深度解析
开发语言·c++
lkbhua莱克瓦2418 小时前
Java进阶——集合进阶(MAP)
java·开发语言·笔记·github·学习方法·map
代码狂想家18 小时前
Rust 命令行密码管理器工具开发
开发语言·rust·php
u01196082318 小时前
java 不可变集合讲解
java·开发语言
翔云 OCR API19 小时前
NFC护照鉴伪查验流程解析-ICAO9303护照真伪查验接口技术方案
开发语言·人工智能·python·计算机视觉·ocr
2501_9411116819 小时前
模板编译期哈希计算
开发语言·c++·算法
Creeper.exe19 小时前
【C语言】分支与循环(上)
c语言·开发语言
jllllyuz19 小时前
基于粒子群优化(PSO)的特征选择与支持向量机(SVM)分类
开发语言·算法·matlab