我一直在努力:
find dev-other -name '*.flac' -type f -exec echo $(echo {} | sed 's,^[^/]*/,,') \;我希望看到.flac文件在dev-other中的路径列表,但没有预先添加的dev-other/,例如:
4515/11057/4515-11057-0095.flac
4515/11057/4515-11057-0083.flac
4515/11057/4515-11057-0040.flac
4515/11057/4515-11057-0105.flac
4515/11057/4515-11057-0017.flac
4515/11057/4515-11057-0001.flac相反我看到了
dev-other/4515/11057/4515-11057-0095.flac
dev-other/4515/11057/4515-11057-0083.flac
dev-other/4515/11057/4515-11057-0040.flac
dev-other/4515/11057/4515-11057-0105.flac
dev-other/4515/11057/4515-11057-0017.flac为什么sed替换在这里不能工作,即使它自己工作
$ echo $(echo dev-other/4515/11057/4515-11057-0047.flac | sed 's,^[^/]*/,,')
4515/11057/4515-11057-0047.flac我第一次尝试了扩展:
find dev-other -name '*.flac' -type f -exec a={} echo ${a#*/} \;但得到了错误:
find: a=dev-other/700/122866/700-122866-0001.flac: No such file or directory
find: a=dev-other/700/122866/700-122866-0030.flac: No such file or directory
find: a=dev-other/700/122866/700-122866-0026.flac: No such file or directory
find: a=dev-other/700/122866/700-122866-0006.flac: No such file or directory
find: a=dev-other/700/122866/700-122866-0010.flac: No such file or directory发布于 2017-10-19 07:28:11
在使用find和-exec选项时,您可以只对用例使用参数展开,
find dev-other -name '*.flac' -type f -exec bash -c 'x=$1; y="${x#*/}"; echo "$y"' bash {} \;我使用了一个单独的shell (使用bash或sh),使用bash -c,因为要涉及涉及参数展开的单独字符串操作。将find结果的每个输出作为参数传递给进行此操作的子shell。
当bash -c执行一个命令时,命令之后的下一个参数被用作$0 (脚本在进程列表中的“名称”),随后的参数成为位置参数($1、$2等)。这意味着由find传递的文件名(代替{})将成为脚本--的第一个参数,并由$1在迷你脚本中引用。
如果不想使用额外的bash,可以就地使用_。
find dev-other -name '*.flac' -type f -exec bash -c 'x=$1; y="${x#*/}"; echo "$y"' _ {} \;其中_ i是一个bash预定义变量(例如在dash中没有定义):“在shell启动时,设置为用于调用环境或参数列表中传递的shell或shell脚本的绝对路径名称”(参见man特殊参数部分)。
值得一看的使用查找-复杂操作
https://stackoverflow.com/questions/46824196
复制相似问题