Linux / Cygwin递归地复制文件变更扩展名 [英] Linux/Cygwin recursively copy file change extension

查看:240
本文介绍了Linux / Cygwin递归地复制文件变更扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找递归地找到扩展名为X( .js)的文件,并在扩展名为Y( .ts)的同一目录中创建一个文件的副本。



例如/foo/bar/foobar.js - > /foo/bar/foobar.js和/foo/bar/foobar.ts

/foo/bar.js - - > /foo/bar.js和/foo/bar.ts等等等等。

我的尽职调查:
我正在考虑使用find& xargs& cp和大括号扩展( cp foobar。{js,ts} ),但xargs使用大括号来表示从xargs传递的文件列表。这让我感到难过,因为我刚刚发现了真棒酱,是支撑扩张/替代。



我觉得必须有一个单线解决方案,我挣扎着想出一个。



我已经找到了执行这个任务的想法:把想要的复制到一个新目录,然后把这个目录和新目录合并。递归地在每个目录中运行一个重命名脚本;使用rsync复制;使用find,xargs和cpio。



现在看来,运行重命名脚本脚本,如这个是我最终要做的。

解决方案

  find。 -name* .js-exec bash -c'name ={}; cp$ name$ {name%.js} .ts'\; 

使用 find ,您可以直接在您找到的文件上执行一个命令,使用 exec 选项;你不需要通过xargs管道。它接受命令后接命令的参数,后跟一个参数; ,为了避免shell解释它,你必须转义。 find 将会在命令名或参数中找到 {} 。 b
$ b

为了调用一个代替适当的结尾的命令,你可以采取多种方法,但一个简单的方法是使用Bash的 parameter expansion 。您需要定义一个包含名称的shell参数(在这种情况下,我创造性地选择了 name = {} ),然后您可以使用参数扩展。 $ {variable%suffix} $变量后缀 C $ C>;然后添加 .ts 到最后,并找到我正在寻找的名称。


I'm looking for a way to recursively find files with extension X (.js) and make a copy of the file in the same directory with extension Y (.ts).

e.g. /foo/bar/foobar.js --> /foo/bar/foobar.js and /foo/bar/foobar.ts

/foo/bar.js --> /foo/bar.js and /foo/bar.ts etc etc

My due diligence: I was thinking of using find & xargs & cp and brace expansion (cp foobar.{js,ts}) but xargs uses the braces to denote the list of files passed from xargs. This makes me sad as I just recently discovered the awesome-sauce that is brace expansion/substitution.

I feel like there has to be a one-line solution but I'm struggling to come up with one.

I've found ideas for performing the task: copying the desired to a new directory and then merging this directory with the new one; recursively run a renaming script in each directory; copy using rsync; use find, xargs and cpio.

As it stands it appears that running a renaming script script like this is what I'll end up doing.

解决方案

find . -name "*.js" -exec bash -c 'name="{}"; cp "$name" "${name%.js}.ts"' \;

Using find, you can execute a command directly on a file that you've found, by using the -exec option; you don't need to pipe it through xargs. It takes the command name followed by arguments to the command, followed by a single argument ;, which you have to escape to avoid the shell interpreting it. find will replace any occurrence of {} in the command name or arguments with the file found.

In order call a command with the appropriate ending substituted, there are multiple approaches you can take, but a simple one is to use Bash's parameter expansion. You need to define a shell parameter that contains the name (in this case, I creatively chose name={}), and then you can use parameter expansion on it. ${variable%suffix} strips off suffix from the value of $variable; I then add on .ts to the end, and have the name I'm looking for.

这篇关于Linux / Cygwin递归地复制文件变更扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆